Sign in to follow this  
Gokhan

2d top-down movement

Recommended Posts

Gokhan    122
I have a small quad textured with a triangle shape that I move around with the arrow keys (up for 'forward', down for 'backward', left and right rotates it). I managed to get it to rotate around it's centre no matter where it is onscreen, but I would like to know how I get it to move in the direction it is facing. Currently it only moves up and down the screen, I'd like it to move in the direction the apex is facing when I press up and the opposite way when I press down. This is standard OpenGL by the way :)

Share this post


Link to post
Share on other sites
jyk    2094
Vector2 direction(cos(angle), sin(angle));
position += direction * speed * time_step;
Don't forget to convert from degrees to radians if necessary. You also may have to make some minor adjustments depending on what default direction your ship is drawn in, and which way you want positive rotation to be.

Share this post


Link to post
Share on other sites
Gokhan    122
I hate to be a bother, but I don't quite understand how I use that code. If it's not too much trouble could I get an explanation please? If it helps I'll show you some code I have:


//********************
// Player ship sprite
//********************
// set values for velocity and turn angle when keys are pressed
if(upKey || downKey)
{
shipPos = 2.5f;
}
if(leftKey || rightKey)
{
shipAngle = 120.0f;
if (shipAngle >= 360.0f)
shipAngle = 0.0f;
}

// move ship around
if(upKey)
{
player_ship->set_ypos(player_ship->get_ypos() + shipPos * deltaSeconds);
}
if(downKey)
{
player_ship->set_ypos(player_ship->get_ypos() - shipPos * deltaSeconds);
}
if(leftKey)
{
player_ship->set_angle(player_ship->get_angle() + shipAngle * deltaSeconds);
}
if(rightKey)
{
player_ship->set_angle(player_ship->get_angle() - shipAngle * deltaSeconds);
}

//Vector2 direction(cos(shipAngle), sin(shipAngle));
//position += direction * speed * time_step;

// draw player ship
glPushMatrix();
glTranslatef (player_ship->get_xpos(), player_ship->get_ypos(), -5.0f);
glRotatef(player_ship->get_angle(), 0.0f, 0.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, texture[SPRITES]);
glBegin(GL_QUADS);
glTexCoord2f(tx, 1 - ty); // top left
glVertex2f(-player_ship->get_width() / 2.0f, player_ship->get_height() / 2.0f);

glTexCoord2f(tx + 0.0625f, 1 - ty); // top right
glVertex2f(player_ship->get_width() / 2.0f, player_ship->get_height() / 2.0f);

glTexCoord2f(tx + 0.0625f, 1 - ty - 0.0625f); // bottom right
glVertex2f(player_ship->get_width() / 2.0f, -player_ship->get_height() / 2.0f);

glTexCoord2f(tx, 1 - ty - 0.0625f); // bottom left
glVertex2f(-player_ship->get_width() / 2.0f, -player_ship->get_height() / 2.0f);
glEnd();
glPopMatrix();

glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);

Share this post


Link to post
Share on other sites
jyk    2094
I should be something like this:
float speed = 0.0f;
float deltaAngle = 0.0f;

if(upKey) {
speed += 2.5f;
}
if (downKey) {
speed = -2.5f;
}
if (leftKey) {
deltaAngle += 120.0f;
}
if (rightKey) {
deltaAngle -= 120.0f;
}
// I assume this function wraps the resulting angle so it's in the range [0, 360]
player_ship->set_angle(player_ship->get_angle() + deltaAngle * deltaSeconds);

Vector2 direction(
cos(deg_to_rad(player_ship->get_angle())),
sin(deg_to_rad(player_ship->get_angle()))
);

player_ship->set_position(player_ship->get_position() + direction * speed * deltaSeconds);
Can't guarantee I got all the details right, but that's the general idea.

Share this post


Link to post
Share on other sites
deadimp    310
I'd suggest either letting the user have access to the x, y, and angle coordinates (if there isn't anything that needs to be updated when they are changed [and some say that this is bad practice]) or simply have a "offset_[x, y, angle]( ... )" method.
Plus, you might just want to make step method in the ship instead of having to do all of that out of its scope.

Share this post


Link to post
Share on other sites
Gokhan    122
Sorry I haven't replied lately, been a bit busy and the boards have been playing up for me a bit (not adding my reply and stuff). Anyway I actually managed to get it working on the day I originally posted, so thanks for all your help guys ;) Just one small problem, when I press up or down, the ship 'jumps' slightly, and when I let go it jumps back, what might the problem be?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this