## new position with vector

pander007
Unregistered

Post: #1
im trying to work out the new x,y position for my space ship..
ive been looking on the forum and tried using atan2..

float angle = atan2(vecx, vecy); //this is the angle

double distance=sqrt(pow(vecx, 2)+pow(vecy, 2));

int resulta = acos( x /distance)* 3.14/180;
int resultb = asin( y /distance)* 3.14/180;

x += cos(resulta) ;
y += sin(resultb) ;

but ive tried different combinations.. i have the original X,Y of the spaceship and 2 vectors vx and vy but im trying to make them move...
they seem to move too fast :S
Oldtimer
Posts: 834
Joined: 2002.09
Post: #2
1) atan2 wants the y component first: atan2f (vecy, vecx)
2) I'm really, really not sure what you're doing here.

You already have the direction vector for the ship, right? So what you want to do is to normalize that vector (divide each component with "distance"), multiply it with its speed, and then add that new vector to the ship's position.

There are several threads on here about vector movement, so just search the forums.
Moderator
Posts: 1,563
Joined: 2003.10
Post: #3
Member
Posts: 100
Joined: 2006.05
Post: #4
Well, what your basically doing is:

x = x + cos(acos(blah))

so you may as well just do

x = x + blah

Also, cos expects arguments to be in radians and acos returns angles in radians. What your doing by multiplying by (3.14/180) is using the formula for converting degrees to radians, and applying it to something that is already in radians (It's like taking a measurement in inches and multiplying it by twelve). What you want is something like this.

Code:
```float magnitude = sqrt(x*x + y*y); float dx = x/magnitude*SPEED*delta_t; // or you could just insert your own float dy = y/magnitude*SPEED*delta_t; // velocity vector components x += dx; y += dy;```

This makes it easy to change your physics system later because you can specify what the velocity vectors are (you can make them exhibit acceleration and inertia).