# Need some help to move sprite in XNA

when a sprite, like a space ship, reach the window border I call a method to "turn off" the engine by setting the speed to 0. But then it's impossible to move the sprite back, because the speed is set to 0! I would preciate some help to solve this! Perhaps there is another way to do this? Thanks!

Some of the code

if (position.X > 800)
EngineOff();

direction = new Vector2((float)Math.Cos(rotation - 1.62), (float)Math.Sin(rotation - 1.62));
position += direction * speed;


And the method that controls the engine

 // Engine on
public void EngineOn()
{
speed = 1;
}

// engine off
public void EngineOff()
{
speed = 0;
}


EDIT: Would it be possible to rotate the ship, so it's point inwards the game area? Would it be possible to rotate it 1/2 of a turn or 1/4 of a turn? My thought was that when the spaceship hit the border, it just turn around and continue moving inwards the game area.

You would need to re-position your ship back so that if (position.X > 800) would equal false.

if (position.X > 800)
{
EngineOff();
position.X = 800;
}


Similar to a collision, once the collision occurs you would typically reset your ship's position to the exact point of impact.

Considered that you are already storing the 2D position and the velocity in 2D vectors, what you could easily do is:

public void update(dt)
{
// If needed, depending on user input
Position.x += Direction.x * speed;
Position.y += Direction.y * speed;

checkCollisions();
}

private void checkCollisions()
{
// if it collides with border
if (Position.x + this.width >= 800)
{
Position.x = 800 - this.width - 2 // -2 to give some more separation
Direction.x *= -1;
}
if (Position.y + this.height >= 800)  // 800 Y border
{
Position.y = 800 - this.height - 2 // -2 to give some more separation
Direction.y *= -1;
}
// Similar for X left border, Similar for Y bottom border
}


This way, you will mirror the moving direction depending on the collision side pong-style, to speak. You don't really need to set your speed to 0 unless you don't want to turn on/off every time there is an user input. To block your sprite, you can add a bool blocked property to it and update/process user input only when the sprite is not blocked. If you want to simulate acceleration-style of moving, use the properties Velocity and Acceleration (2D vectors) as they need to be updated following the standard simple physics equations of accelerated motion. The additional -2 is added to make sure that a collision is not detected two times consequently by mistake (can happen). Anyway, if the scenario becomes more complicated, consider adopting an easy 2D physics engine.