#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
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