# moving acording to a direction

This topic is 3659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys. i am trying to make something samilir to a 3D astroids game. i have my 'spaceship' , it can turn left,right,up and down) now i am trying to make it move according to the direction i've set it. i know how to do it in 2D(every frame add cos and sin to x and y with the left\right angle). but since its 3D, i also want it to move up and down acording to the angle of up\down but it just doesnt seem to work :here's the way i am trying to do it
                 velx=-(float)cos(MainPlayer->rotz+rad_90) *2.5;
velz=-(float)tan(MainPlayer->rotx) *2.5;
MainPlayer->y+=vely;
MainPlayer->x+=velx;
MainPlayer->z+=velz;

rotz = the rotation of the ship to the right and left rad_90 = variable containing 90 degree in radian rotx = the rotation of the ship for up and down using this i am getting problems like when i look stright up the ship would suddenly increase speed by way too much. also when velz is possetive, i start moving backwards(i can always manipulate it with -1 when it does, but its probably not the good way to fix it is it?) i'll appreciate help:) thanks in advance.

##### Share on other sites
Just by looking at your code, I notice that neither velx nor vely depend on the vertical angle (meaning that the vertical speed will always be independent of the vertical speed, which is unlikely). I also notice that velz is a tangent, which means it can reach infinity (which probably explains why you've been having very high speeds).

Either way, I suggest that you use a tried-and-true mathematical formula, such as spherical coordinates, instead of trying to invent your own.

##### Share on other sites
ouch, that page has too many formulas! which one am i actually suppoused to pick?
and well i think i almost reached the my objective by making it this way :
                 velx=-(float)cos(MainPlayer->rotz+rad_90) *2.5+(float)sin(MainPlayer->rotx) *2.5;		 vely=-(float)sin(MainPlayer->rotz+rad_90) *2.5+(float)sin(MainPlayer->rotx) *2.5;		 velz=-(float)sin(MainPlayer->rotx) *2.5;	     MainPlayer->y+=vely;	     MainPlayer->x+=velx;	     MainPlayer->z+=velz;

if i remove velx, then it seems to work almost awesomly(though i still didnt find the right formula for velx).
what do you think?

##### Share on other sites
and now when i check it agian, i find out its not working so good as i thought!
oh! what should i do?

##### Share on other sites
x	=	r cos theta sin phi	y	=	r sin theta sin phi	z	=	r cos phi

##### Share on other sites
thanks :) though i probably should have chosen 5 unit points in math, i find it hard to figure how to turn this into code(what's theta? whats phi? and what is actually the radius considering i didnt use any radius here up until now).
eh, sorry for not understanding that, dont realy want to bother you guys.

##### Share on other sites
Quote:
 Original post by Bru(what's theta? whats phi?

Explained in the first paragraph.

Quote:
 and what is actually the radius considering i didnt use any radius here up until now).
What is 2.5, if not a radius?

##### Share on other sites
oh, now i get it!
and it seems to be working! hey thanks alot :)

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013441
×