[Gambas-user] translating planar coordinates relative to point's angle

Doriano Blengino doriano.blengino at ...1909...
Mon Mar 14 09:46:51 CET 2011


Kevin Fishburne ha scritto:
> I'm disturbed that I couldn't find an answer via Google that wasn't too
> complex for my feeble mind to understand (my fault; I suck at math), so
> I'm asking here.
>
> I'm trying to move a point with an angle/direction/vector relative to
> its angle. Let's say I have these variables:
>
> Dim a as Single ' Point's angle in degrees.
> Dim x as Single ' Point's x coordinate.
> Dim y as Single ' Point's y coordinate.
> Dim xvel as Single ' Point's right/left velocity (+/-).
> Dim yvel as Single ' Point's backward/forward velocity (+/-).
>
> What's a good algorithm to figure out the new values of x and y when
> adjusted by xvel and yvel relative to a?
>
> I've looked through a bunch of vector math "tutorials" but they are very
> broad and difficult for me to understand. I'm good at many things, but
> this isn't one of them. Any help will be greatly appreciated.
>
>    
I think that you have too many variables involved... to define a speed 
in a plane you need only two coordinates - you should choose between two 
methods (or coordinate systems).

The first one, which seems to fit best, is to choose a direction in 
degrees (or radians) and a velocity. At ever"step", the x,y position 
varies by cos(direction) and sin(direction), both multiplied first by 
the velocity of the object.

In the second method is to keep two velocities, left/right and top/down. 
At every step, you add the velocities to the current position.

As you see, both methods only involve two variables that specify the 
direction and speed of the object, and of course its current position, 
which get upgraded at every step (or frame).

The first method, polar coordinates, is probably nearer to the idea of a 
moving object. If the object changes direction, you simply change the 
"direction" variable. If the object changes speed, you simply change the 
"speed variable".

The second method is the same as the former, where horizontal 
(left/right) speed is cos(direction) and vertical is sin(direction). The 
problem is that if you don't keep track of the "direction", but instead 
you use its components, it is slightly difficult to change direction; 
anyway, the two coordinate systems are the same, and from one you can 
pass to the other at will (not counting math rounding problems).

I think that you could choose by looking at what your joystick means to 
you. Does it rotate the object, like the famous game Asteroids? Then 
choose polar coordinates. Or does it mean left/right and up/down? Then 
choose cartesian coordinates (but you talked about speed and direction...).

Hope this can help,
regards,
Doriano








More information about the User mailing list