gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Code for moving sprite (left, right, up, and down)

#149696 - Cave Johnson - Thu Jan 24, 2008 3:33 am

I've been trying to code a simple game, including a fish. I have all of the code written, except i cannot figure out how to program the fish to move in simple directions with a press of the corresponding Dpad button. I want the fish to move up, down, left, and right, and to reverse directions right and left. This is what i have so far for moving the fish in my fish.cpp file:

void fish::up() {
float incX = thrust * sin(angle);
float incY = -(thrust * cos(angle));
velocity.x += incX;
if (velocity.x > maxSpeed) {
velocity.x = maxSpeed;
}
if (velocity.x < -maxSpeed) {
velocity.x = -maxSpeed;
}

velocity.y += incY;
if (velocity.y > maxSpeed) {
velocity.y = maxSpeed;
}
if (velocity.y < -maxSpeed) {
velocity.y = -maxSpeed;
}
}

void fish::movefish() {
position.x += velocity.x;
position.y += velocity.y;
}

void fish::reverseTurn() {
angle = (2 * PI) - atan2(velocity.x, velocity.y);
}

And here is the controls in my main.cpp:

void updateInput(touchPosition * touch) {
scanKeys();

*touch = touchReadXY();
}

void handleInput(fish * fish, MathVector2D<int> SpriteInfo, touchPosition * touch) {

if (keysHeld() & KEY_UP) {
fish->up();
} else if (keysHeld() & KEY_DOWN) {
ship->down();
}

if (keysHeld() & KEY_LEFT) {
ship->reverseTurnLeft();
} else if (keysHeld() & KEY_RIGHT) {
ship->reverseTurnRight();
}

Im pretty sure i have the code to move the fish up, that the reverse is not what i want it to be, and i know i have no left, right, or down code. If anyone could help me write this code, that would be great. Thanks.


Last edited by Cave Johnson on Thu Jan 24, 2008 11:35 pm; edited 1 time in total

#149742 - Miked0801 - Thu Jan 24, 2008 7:11 pm

Ok, let me see if I can understand your code:

First of all: Comments please! Without comments, we have to guess at functionality.

Press Up is accelerate in the current direction (angle)

reverseTurn doesn't reverse a turn. To reverse direction, just flip your angle 180 degrees. Your solution is flipping your velocity around the 0 degree axis. If that is along the Y (up/down) axis, then it might be doing what you want, but the name of the function is misleading.

You also have no way to change the angle of your fish. Perhaps increment and decrement the angle based on Shoulder Buttons or something.


Or, are you trying to just move your fish in simple Up/Down/Left/Right movements without acceleration and such?

#149836 - Cave Johnson - Fri Jan 25, 2008 9:44 pm

Thanks for all the help, i fixed the problem, but i now have a new one. In the tutorial i followed, the sprite continued to coast along after the button was lifted. How do i fix this to make the sprite stop exactly when the button is lifted? I'm not sure what file this would be located in (main, the general sprite.cpp , or the fish.cpp for my fish sprite) so i wont post the file unless anyone has any idea. Thanks.

#149844 - gauauu - Fri Jan 25, 2008 10:50 pm

If you want the fish to stop after you stop pressing Up, you need to check and see if the user is NOT pressing up, or pressing down, then set it's Y velocity to 0.

Something like:

Code:

if (keysHeld() & KEY_UP) {
     fish->up();
} else if (keysHeld() & KEY_DOWN) {
     fish->down();
}  else
{
     fish->stopYMovement();
}

....

void fish::stopYMovement() {
   velocity.y = 0;
}




Of course, this doesn't address inertia or other external forces on your fish, which your velocity system seems to suggest might exist. (You could alternatively decrement the y velocity down to 0 to make it slow down and stop).

For a simpler model, to get you started, it might be easier if you remove the angular stuff, and remove the variable velocity, and just update the fish's position directly:

Code:

void fish::up() {
   position.y--;
}


Then, when you stop pushing up, it stops going up. Although this doesn't let you have as much flexibility with inertia, acceleration, angles, outside forces, etc, it is much easier for a beginner to get something working that makes sense.

#151260 - mrhaboobi - Fri Feb 22, 2008 2:10 am

HI, whats the tutorial your using for the movement? I'm interested to know i've also got plenty of questions regarding simple movement. Thanks