# 2d top-down movement

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

## Recommended Posts

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 on other sites
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 on other sites
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 on other sites
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 on other sites
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 on other sites
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?

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631424
• Total Posts
3000008
×