🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Help Me!!!! Vector velocity problem

Started by
8 comments, last by han---solo 24 years, 5 months ago
If anyone could me, please!! I want to know how to make a ship move in a specific direction, sort of like an astroids game. You point your ship in a direction in a specific direction and press accelerate and then it goes. In my book, Tricks of the windows game programming gurus, it talks about it but i don''t really get it. Help me please!! "sorry 'bout the mess..."
-Han Solo

Hansolo99_99@hotmail.com

"sorry 'bout the mess..."
-Han Solo

Hansolo99_99@hotmail.com

Advertisement
um, i''d suggest reading harder.. and reading harder again, and again... and again???
if u need, this is the basic math:
keep an angle A from 0 to 359 and the velocity of the ship. every cycle calc the direction vector using x = sin(A), y = cos(A). this gives u the unit vector (velocity of 1... whatever units u use). then multiply this vector by the velocity, so x = Velocity * cos(A).. etc. then just add the vector to ur current ship pos. obviously there must be a better way to do this.. but u get the idea.
Ok, i get it sort of, but how do you keep the angle?
"sorry 'bout the mess..."
-Han Solo

Hansolo99_99@hotmail.com


Well first off, I''m assuming you''re keeping track of your ship in a class or structure, something like

class Ship {
public:
update();
draw();
turn_left();
turn_right();
thrust();
...
private:
float x,y;
float vx,vy;
int angle;
}

(If you''re not using C++, then just pretend all those public member functions are regular functions, and make the private data a public structure)

What AlexM said is right, but it sounds to me like he''s saying to take the sin and cos every frame. You don''t need to do that. Try something like :

turn_left() { angle--; } // Also want to check for wrap-around in both of these
turn_right() { angle++ };

thrust() {
vx += cos(angle) * THRUST_AMOUNT;
vy += sin(angle) * THRUST_AMOUNT;
}

then when you''re updating where your ship is every frame,

update() {
x += vx;
y += vy;
// Might want to check if it goes off screen, etc.
}


So basically, this code will give you a newtonian movement system, where you thrust once in a direction, and will continue to float in that direction until you apply a counterthrust.
Shoot, above anonymous person ^^^^^^^^ is me.

- Remnant
- (Steve Schmitt)
- Remnant- (Steve Schmitt)
All right i did that, but the ship only moves along the axes. I still don''t really get it. I am posting the code so that anyone can help me. go here ->

han-_-solo.tripod.com/help/aster.cpp

You need to include the Directx libraries.


P.S. to Andre LaMothe, yes this is coming almost directly from your book. hope this dosen''t count as copyright infringment




"sorry 'bout the mess..."
-Han Solo

Hansolo99_99@hotmail.com

"sorry 'bout the mess..."
-Han Solo

Hansolo99_99@hotmail.com

Wait!


The aster.cpp file is actually

www.geocities.com/hollywood/land/9837/asteroids.txt

(Geocities dosn''t allow .cpp files because they think that they are there for weird reasons.)

Any way, you might want to rename the file to .cpp and then compile

Thanx in advance!

"sorry 'bout the mess..."
-Han Solo

Hansolo99_99@hotmail.com

"sorry 'bout the mess..."
-Han Solo

Hansolo99_99@hotmail.com

Probably the reason the ship is only moving on the axis is because you haven''t converted your angle from degrees to radians. sin and cos take radians.

radians = degrees*PI/180

--Shannon Schlomer, BLAZE Technologies, Inc.
--Shannon Schlomer, BLAZE Technologies, Inc.
Hi! I''m having some sort of the same problem, only that I''m doing it in 3D. How do you determine the direction for it to move in 3D? I''m keeping track of the roll, yaw and pitch angles for the object, but can''t figure out a proper equation that will make it move correctly. Any help?
No actually, I am using look up tables for that exact reason. Any other ideas?

"sorry 'bout the mess..."
-Han Solo

Hansolo99_99@hotmail.com

"sorry 'bout the mess..."
-Han Solo

Hansolo99_99@hotmail.com

This topic is closed to new replies.

Advertisement