[Gambas-user] more trigonometry fun (not)

Doriano Blengino doriano.blengino at ...1909...
Tue May 24 09:19:22 CEST 2011


Kevin Fishburne ha scritto:
> On 05/24/2011 12:57 AM, Bruce Bruen wrote:
>   
>>>>> I need to know the (x, y) offset of a point at a given orientation and
>>>>> velocity. For example if a point is moving at an angle of 45 degrees (or
>>>>> radians, take your pick), what would its x and y coordinate be
>>>>> increased/decreased by? The variables I can think of would be:
>>>>>
>>>>> x1 (point's current x coordinate)
>>>>> y1 (point's current y coordinate)
>>>>> a (point's angle/orientation in degrees/radians)
>>>>> v (point's velocity)
>>>>> x2 (x coordinate offset of point's new position)
>>>>> y2 (y coordinate offset of point's new position)
>>>>>
>>>>> The calculation would take x1, y1 a and v as inputs and produce x2 and
>>>>> y2 as offsets (x1 + x2, y1 + y2 = point's new position).
>>>>>
>>>>>           
Wally said:
> in cartesian coordinates:
> x2 = x1 + cos(a) * v * t
> y2 = y1 + sin(a) * v * t
which is correct; only, you specified that you want x2 and y2 "as 
offsets", i.e., the distance covered and not the new position. In this 
case, the formulas are:

    x2 = cos(a) * v
    y2 = sin(a) * v

"v" indicates the velocity of the object, expressed in space/deltaT 
(pixels for frame, for example). The angle "a" must be in radians.

Also, Bruce said:
> 1) The trig is always easier if you orient your framework so the 
> particle is moving (from the point of view of the trig math function) in 
> quadrant 0 i.e. in the "north east" quarter of the compass.  This done 
> by reflecting the angle into q0, then do the math and then de-reflect it 
> back.
On computer screens there is always this problem. Probably you can 
simply change the sign of y2: add x2 to x1 and subtract y2 from y1. But 
you could write the algorithm as is, test it, and see if you need to 
mirror the Y or to do a rotation (angle+90, angle+180, or whatever), or 
both.

Bruce added:
> 2) As Mr Newton said, a body will continue to move at a given rate until 
> acted on by an outside force.  It may be useful for the future if you do 
> the trig calcs ONLY when the particle is so affected.
If you are thinking about projectiles, they are shooted away, by a force 
applied to them, for a small amount of time; then the force ceases, and 
the projectile slows down. This can be simulated by decrementing "v" at 
every frame and, when "v" falls below some arbitrary value, set it to 
zero: the throw is terminated. May be that you don't want infinite 
distances, so this method gives you a way to terminate early a throw if 
it misses it target. Or may be that you terminate the throw when the 
missile hits a target, or goes out the screen. In this case, varying "v" 
only gives some more reality (pretty useless, may be, unless you throw 
boulders using catapults).

I remember that your player has a heading. If the player shoots always 
ahead of it, then you use, in the formula of the missile, the same angle 
as the player orientation. If the player shoots at its right, the you 
must subtract 90 degrees; to the left, you add 90 degrees.

Regards,
Doriano






More information about the User mailing list