Not logged inOpenClonk Forum
Up Topic Development / Developer's Corner / Projectile-Oriented Functions
- - By Ringwaul [ca] Date 2009-12-13 09:57 Edited 2009-12-16 04:04
I've recently created a SetLinearPos function and a Velocity function; this way weapons like the bow/musket or whatever else won't need their own trigonometrical functions.
Currently, it is as follows:

global func SetVelocity(int iAngle, int iSpeed)
{
  // Sets an object's velocity
  var iX= Sin(180-iAngle,iSpeed);
  var iY= Cos(180-iAngle,iSpeed);

  SetXDir(iX);
  SetYDir(iY);
}

global func SetLinearPos(int iAngle, int iDistance, int iX, int iY, bool iRelativeX)
{
  // iDistance: Distance object travels on angle. Offset from calling object.
  // iX: X offset from container's center
  // iY: Y offset from container's center
  // iRelativeX: if true, makes the X offset relative to container direction. (iX=+30 will become iX=-30 when Clonk turns left. This way offset always stays in front of a Clonk.)

  var iXOffset=Sin(180-iAngle, iDistance);
  var iYOffset=Cos(180-iAngle, iDistance);

  if(Contained()!=nil && iRelativeX==true) { if(Contained()->GetDir()==0) iX=-(iX); }
  SetPosition(GetX()+iXOffset+iX,GetY()+iYOffset+iY) && SetR(iAngle);
}


The nifty thing about SetLinearPos() is that it can calculate the position of where a bullet/arrow/etc. should appear before launched. For example, this makes it easy for bullets to actually appear out of the end of a gun's barrel, instead of appearing from the Clonk's centre.
Reply
Parent - - By Sven2 [de] Date 2009-12-13 12:12
I don't like the names. They are somehow arbitrary and don't fit in with other names.

We already have "SetSpeed", so a more logical name might be something like "SetSpeedByAngle" or "SetPolarSpeed", "SetSpeedPolar" or whatever. SetSpeed for catesian coordinates and SetVelocity for polar coordinates seems arbitrary to me.

Next: Your angles start at positive y for zero degrees and go counter-clockwise. Iirc, the engine uses clockwise angles and sets zero at egative y (see function "Angle"). Please use the same convention.

SetLinearPos is also weird. First, there's "Pos" for "Position". We had the same with "Plr" vs "Player" and "Obj" for "Object" - bottom line is, as a scripter, you never know when to shortcut and when not to. We should stick with either "Pos" or "Position", but we shouldn't mix both. Second, what's "linear" about this?

If the function is supposed to be used for projectiles, why not call it ExitAngle(int angle, int distance, int speed, bool flip_angle_by_dir) and let it do Exit at correct speed and position instead of simple SetPosition? Right now, the function does almost nothing, but as a scripter you'd have to know it and memorize parameters.

Btw: Contained()!=nil - so, is nil the new C4Object(0)?
Parent - - By Maikel Date 2009-12-13 16:21 Edited 2009-12-13 16:26
They are not chosen arbitrary I think, but the one we had was named wrongly. The original function should be named SetVelocity, because velocity is the indication for the vector i.e. (iXDir, iYDir). On the other hand speed is used to indicate the Euclidian norm of the velocity, which would intuitively suit polar coordinates, since one of the parameters is linked directly to the speed. So you have something like(where the angle is optional):

global func SetSpeed(int iSpeed, int iAngle)
{
  if(!iAngle)
  {
     // Calculate angle with which it was moving
  }
  var dx = Sin(iAngle, iSpeed);
  var dy = -Cos(iAngle, iSpeed);

  SetXDir(dx);
  SetYDir(dy);
  return;
}


Regarding the Plr,Clr,Obj,Pos naming your are right, a convention for this would be nice.
Parent - - By Sven2 [de] Date 2009-12-13 18:32
A vector in polar coordinates is still a vector. Also, I have never felt the need to "change speed but keep the angle". When would that function be used?
Parent - By Maikel Date 2009-12-13 19:01
No idea for any usage. Then it would be SetVelocityCartesian and SetVelocityPolar.
Parent - By Ringwaul [ca] Date 2009-12-13 20:09 Edited 2009-12-13 20:30
I chose SetVelocity because in physics Velocity is a measure of displacement at a given angle; therefore velocity factors in direction (iAngle) and speed (iSpeed).

Are you sure about the counter clock-wise angles? I've tested it various times, and SetLinearPos(45,100) moves an object 70px up and 70px to the right. Of course, to set 0 degrees to y-negative the '180-' would be deleted from the trig functions. I just thought it more simple for 0 degrees to be 'up'. I believe the current CastPXS() function uses 0 degrees as up (it used to be directly right 0_o), so if 0 degrees should be down that must be changed as well.

I shorthanded Pos for writing speed, but I agree with what you say. I titled it 'set linear position' because it moves an object a certain distance along a line at an angle. I guess have my physics terms messed up here?

Sure, it's always subject to change if it doesn't fit the required needs of scripters.
edit-note:
Should it be like such:

if(Contained()!=nil) Exit(iXOffset+iX,iYOffset+iY, iAngle);
else SetPosition(GetX()+iXOffset+iX,GetY()+iYOffset+iY) && SetR(iAngle);


or like such:

if(Contained()!=nil) Exit(iXOffset+iX,iYOffset+iY, iAngle);
else return 0;
Reply
Parent - - By MimmoO Date 2009-12-13 16:23
good idea. in addition to this, i think a nice ricochet-script would be nice. if you have muskets or so, or bouncing projectiles from spells.
Parent - By Ringwaul [ca] Date 2009-12-13 20:18
I'm guessing ricochets would be better implemented into the physics engine. To properly script a ricochet, I think you'd have to continually log variables that record velocity (speed & angle).
Reply
Parent - - By Ringwaul [ca] Date 2009-12-16 04:02
I've added greater functionality to my second function (now Projectile() instead of SetLinearPos() ) so that it can also set the velocity of the calling object.
Reply
Parent - - By Zapper [de] Date 2009-12-16 13:57
Would be good to keep the naming with Set* for functions that set stuff imo
Parent - - By Ringwaul [ca] Date 2009-12-16 17:15
It doesn't set just one thing; Projectile() launches an object, with angle, speed, and a couple position parameters.
Reply
Parent - - By Zapper [de] Date 2009-12-16 18:10
LaunchProjectile!
Parent - By Ringwaul [ca] Date 2009-12-16 20:20 Edited 2009-12-17 01:30
Sounds good. I'll make that change when I get back from school.

Also, I wanted to mention I've been working on a gun (or bow) system for OpenClonk using LaunchProjectile(). Once I get back home I'll explain some more details.

Edit: I'll push the musket to PlrCtrl; it's not quite finished, but the basics are in place. One thing I'd like to add are ammo-count displays on the inventory slots. I heard about attaching objects to armature-bones; I think that would be the preferred way to display weapons in use.
Reply
Parent - By Isilkor Date 2009-12-17 01:34 Edited 2009-12-17 01:37
Related: The names of all functions that do something should contain a verb.
Reply
Up Topic Development / Developer's Corner / Projectile-Oriented Functions

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill