I'm having a tough time with my control sprite (ship) occasionally getting stuck on objects it should bounce off of. Now I've completely lost focus and can't see the wood for the trees!
The ship's Y position is fixed and objects in the playfield scroll vertically up and down creating Y movement. X movement is normal stuff.
I know that with the correct chain of events this problem shouldn't occur; I did this 20 years ago on a C64 but my code skills are very rusty.
Collision detection function is good, no problems there. Feel free to ask for that code snippet if it helps. Feel free to correct me anywhere else too!
Thanks,
Mark
///////////////////////////////////////////////////////////////////////////////////////////////////
// Control the ship sprite
///////////////////////////////////////////////////////////////////////////////////////////////////
bool collision = ship_collision(ship_X, ship_Y);
// Left & Right
// Neither left or right keys pressed
if (!App.GetInput().IsKeyDown(sf::Key::Left) && (!App.GetInput().IsKeyDown(sf::Key::Right)))
// Wait until speed has decreased to near-zero
if ((xspeed > -20) && (xspeed < 20))
{
xacceleration = 0; // Then cancel acceleration & speed
xspeed = 0;
}
else
// Not zero yet, so force it to slow down
if (xspeed > 0)
xacceleration = -xaccel_factor;
else
xacceleration = xaccel_factor;
// LEFT
// Check if left key pressed (but not right)
if (App.GetInput().IsKeyDown(sf::Key::Left) && (!App.GetInput().IsKeyDown(sf::Key::Right)))
// Need to set acceleration to LEFT but,
// before changing check we aren't over the side wall
{
if (ship_X >= 20)
// All clear, go left
xacceleration = -xaccel_factor;
else
//bounce off wall
xacceleration = xaccel_factor;
}
// RIGHT
// Check if right key pressed (but not left)
if (App.GetInput().IsKeyDown(sf::Key::Right) && (!App.GetInput().IsKeyDown(sf::Key::Left)))
// Need to set acceleration to RIGHT but,
// before changing check we aren't over the side wall
if (ship_X <= 770)
xacceleration = xaccel_factor;
else
xacceleration = -xaccel_factor;
// Check if ship has reached the side wall
if ((ship_X < 20) || (ship_X > 770))
{
// Invert xspeed
xspeed=-xspeed;
wobble_screen=true;
}
// UPDATE
// Add acceleration to current speed
xspeed += xacceleration;
// Limit speed
if (xspeed > xmaxspeed) xspeed = xmaxspeed;
else
if (xspeed < -xmaxspeed) xspeed = -xmaxspeed;
if (collision == true)
// Invert xspeed
xspeed=-xspeed;
// Up & Down
// Neither up or down keys pressed
if (!App.GetInput().IsKeyDown(sf::Key::Up) && (!App.GetInput().IsKeyDown(sf::Key::Down)))
// Wait until speed has decreased to near-zero
if ((yspeed > -20) && (yspeed < 20))
{
yacceleration = 0; // Then cancel acceleration & speed
yspeed =0;
}
else
// Not zero yet, so force it to slow down only by a smaller factor so it drifts longer
if (yspeed > 0)
yacceleration = -yaccel_factor +25;
else
yacceleration = yaccel_factor -25;
// UP
// Check if up key pressed (but not down)
if (App.GetInput().IsKeyDown(sf::Key::Up) && (!App.GetInput().IsKeyDown(sf::Key::Down)))
// Set acceleration UP
yacceleration = -yaccel_factor;
// DOWN
// Check if down key pressed (but not up)
if (App.GetInput().IsKeyDown(sf::Key::Down) && (!App.GetInput().IsKeyDown(sf::Key::Up)))
// Set acceleration DOWN
yacceleration = yaccel_factor;
// UPDATE
// Add acceleration to current speed
yspeed += yacceleration;
// Limit speed
if (yspeed > ymaxspeed) yspeed = ymaxspeed;
else
if (yspeed < -ymaxspeed) yspeed = -ymaxspeed;
if (collision == true)
// Invert yspeed
yspeed=-yspeed;
// Update ship position
ship_X += xspeed * ElapsedTime;
screen_pos += yspeed * ElapsedTime;
ship_Y = screen_pos + 300;
Ship.SetPosition(ship_X, 300);