Problems with controlling a space-ship
hi there,
I''m working on a 2D game in which you control a space ship.
Just think about the games asteroids or space invaders.
I''m use OpenGL for the grafics output. And here is may
problem:
void LOOP()
{
// some code...
glRotatef(xrotation,0.0f,1.0f);
glTranslatef(0.0f,ymove,0.0f);
ship(); //->paints ther space ship
// some other code...
xrotation=xrotation+xspeed;
ymove=ymove+yspeed;
// some other code...
}
That was the code for controlling the ship. i wrote a
routine in which i check for keyboard input. I increment
or decrement the variable yspeed if the keys
are the up or the down key. So i can move the ship
forward or backward. But the problem is the rotation of
the ship...
If the keys are the left or the right I don''t no how
to change the variable for having no strange effects.
The ship has a very strange movement and is not realistic.
The rotation is very strange and I do not know what to do.
Can anybody help me out? Has anybody a solution for
controlling the ship? Cos or Sin-functions?
Please shoe me some code.
Thx in advance!!!
First I need a little clarification, you are doing a 2D over the top space shooter. Up and down arrows speed up and slow down the ship, left and right turn the nose so the ship flies in that direction at a particular speed.
If this is not what you want, clarify better or else do this. Think of the ships movement as a vector. The vectors direction is the direction the ship is pointing, and the magnitude is the speed. You'll need to variables, one for the speed, and a second one for the angle. Then you convert the vector to (x,y) coordinates and add then vector's x,y to the ship's (x,y)
vx = cos(angle) * speed;
vy = sin(angle) * speed;
cos and sin in math.h wants everything in radians. I'm sure you know what a radian is, but if not,
rad = deg * (pi / 180);
Is that what you want?
Domini
Edited by - Domini on 2/14/00 3:00:11 PM
Edited by - Domini on 2/14/00 3:02:32 PM
If this is not what you want, clarify better or else do this. Think of the ships movement as a vector. The vectors direction is the direction the ship is pointing, and the magnitude is the speed. You'll need to variables, one for the speed, and a second one for the angle. Then you convert the vector to (x,y) coordinates and add then vector's x,y to the ship's (x,y)
vx = cos(angle) * speed;
vy = sin(angle) * speed;
cos and sin in math.h wants everything in radians. I'm sure you know what a radian is, but if not,
rad = deg * (pi / 180);
Is that what you want?
Domini
Edited by - Domini on 2/14/00 3:00:11 PM
Edited by - Domini on 2/14/00 3:02:32 PM
Domini Miracle Man Studios
Yeah !!!
That is exactly what I wanted to know.
I will try this out.
Thx !!!
That is exactly what I wanted to know.
I will try this out.
Thx !!!
Also make sure to prcalculate all the angels in a table other wise using Cos, Sin directly in the game loop will slow you down.
Hardcore Until The End.
When implementing it, make sure that you''re rotating around the right point of the ship. You''ll probably want to rotate around the middle back-section of the ship (assuming it''s symmetric).
That is just for the actual movement of the ship. I would pre-rotate the ship in the magic 8 directions and put the pics in a bmp so you don''t have to do sprite rotations in in real-time.
Domini
Domini
Domini Miracle Man Studios
February 14, 2000 03:56 PM
I saw your post and thought you might like this. Just some code from an ol'' asteroids type game I made. You''re interested in the results, the position vector and pitch, roll & yaw. You just need to provide these static values...
thrust force (force)
ship mass (mass)
maximum attainable speed (max_speed)
rate at which the ship can rotate (turn_rate)
maximum ship rolling speed (roll_limit_speed)
maximum angle the ship rolls to when turning (max_roll_angle)
length of time a burst of force lasts (force_length)
The variable "turn_direct" is either 0, 1 or -1, depening on which direction the ship is turning or if it''s turning at all (0).
void Ship:
rocessPhysics(void)
{
float t, d, s, limit;
// Get current time
t = g_Timer.GetTime();
d = t - last_t;
// Calculate acceleration directly from force (ms^-2)
acceleration.x = force.x / mass;
acceleration.y = force.y / mass;
acceleration.z = force.z / mass;
// Calculate how much acceleration to apply to the velocity for this time slice (m/s)
velocity.x += acceleration.x * d;
velocity.y += acceleration.y * d;
velocity.z += acceleration.z * d;
// Get the magnitude of the velocity
speed = (float)sqrt(velocity.x * velocity.x + velocity.y * velocity.y + velocity.z * velocity.z);
// Limit the velocity if the magnitude is too much (not realistic but aids gameplay)
if (speed > max_speed)
{
float pc;
pc = max_speed / speed;
velocity.x *= pc;
velocity.y *= pc;
velocity.z *= pc;
}
// Calculate how much velocity to apply to the position for this time slice
position.x += velocity.x * d * METER;
position.y += velocity.y * d * METER;
position.z += velocity.z * d * METER;
// Update the yaw
yaw += turn_direct * turn_rate * d;
// Calculate the maximum roll angle
s = min(speed, roll_limit_speed);
limit = s / roll_limit_speed;
// Roll the ship
if (roll > -max_roll_angle * limit && roll < max_roll_angle * limit)
roll += turn_direct * roll_speed * d;
// If the player isn''t turning and the ship has roll, counter-act it
if (roll && !turn_direct)
{
// Get the current roll-back increment
s = roll_back_speed * d;
// If the roll is going to over-compensate, just set it to 0
if (fabs((float)roll) - s <= 0)
{
roll = 0;
}
else
{
if (roll < 0) roll += s;
if (roll > 0) roll -= s;
}
}
// Needed for dt
last_t = g_Timer.GetTime();
// Has the force expired?
if (last_t - last_force_apply >= force_length)
{
force.x = 0;
force.y = 0;
force.z = 0;
}
}
Seeya
thrust force (force)
ship mass (mass)
maximum attainable speed (max_speed)
rate at which the ship can rotate (turn_rate)
maximum ship rolling speed (roll_limit_speed)
maximum angle the ship rolls to when turning (max_roll_angle)
length of time a burst of force lasts (force_length)
The variable "turn_direct" is either 0, 1 or -1, depening on which direction the ship is turning or if it''s turning at all (0).
void Ship:

{
float t, d, s, limit;
// Get current time
t = g_Timer.GetTime();
d = t - last_t;
// Calculate acceleration directly from force (ms^-2)
acceleration.x = force.x / mass;
acceleration.y = force.y / mass;
acceleration.z = force.z / mass;
// Calculate how much acceleration to apply to the velocity for this time slice (m/s)
velocity.x += acceleration.x * d;
velocity.y += acceleration.y * d;
velocity.z += acceleration.z * d;
// Get the magnitude of the velocity
speed = (float)sqrt(velocity.x * velocity.x + velocity.y * velocity.y + velocity.z * velocity.z);
// Limit the velocity if the magnitude is too much (not realistic but aids gameplay)
if (speed > max_speed)
{
float pc;
pc = max_speed / speed;
velocity.x *= pc;
velocity.y *= pc;
velocity.z *= pc;
}
// Calculate how much velocity to apply to the position for this time slice
position.x += velocity.x * d * METER;
position.y += velocity.y * d * METER;
position.z += velocity.z * d * METER;
// Update the yaw
yaw += turn_direct * turn_rate * d;
// Calculate the maximum roll angle
s = min(speed, roll_limit_speed);
limit = s / roll_limit_speed;
// Roll the ship
if (roll > -max_roll_angle * limit && roll < max_roll_angle * limit)
roll += turn_direct * roll_speed * d;
// If the player isn''t turning and the ship has roll, counter-act it
if (roll && !turn_direct)
{
// Get the current roll-back increment
s = roll_back_speed * d;
// If the roll is going to over-compensate, just set it to 0
if (fabs((float)roll) - s <= 0)
{
roll = 0;
}
else
{
if (roll < 0) roll += s;
if (roll > 0) roll -= s;
}
}
// Needed for dt
last_t = g_Timer.GetTime();
// Has the force expired?
if (last_t - last_force_apply >= force_length)
{
force.x = 0;
force.y = 0;
force.z = 0;
}
}
Seeya
Wow,
that was hard to understand but very useful if you wanna
have a VERY realistic game. But for the moment i think
it will be better to take the stuff what Domini said.
And then, if i''m better I will take your stuff.
Because it is really hard to understand for first time.
Thx to all messages!!!
They were really good and helped me a lot.
that was hard to understand but very useful if you wanna
have a VERY realistic game. But for the moment i think
it will be better to take the stuff what Domini said.
And then, if i''m better I will take your stuff.
Because it is really hard to understand for first time.
Thx to all messages!!!
They were really good and helped me a lot.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement