new position with vector

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
Quote this message in a reply
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. Wink

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. Smile
Quote this message in a reply
Posts: 1,563
Joined: 2003.10
Post: #3
Quote this message in a reply
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.

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).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Calculate mouse position in opengl MACnus 1 4,785 Nov 8, 2006 01:02 PM
Last Post: akb825
  Using a matrix to generate a new vector position imikedaman 6 7,365 Jul 16, 2006 01:17 PM
Last Post: imikedaman
  geting window position marko 2 4,137 Jul 24, 2005 02:32 AM
Last Post: marko