Note that there are some explanatory texts on larger screens.

plurals
  1. POKeep Rotating sprite from going off screen, but bounce back.
    text
    copied!<p>Im using a technique to control a sprite by rotating left/right and then accelerating forward. I have 2 questions regarding it. (The code it pasted together from different classes due to polymorphism. If it doesn't make sense, let me know. The movement works well and the off screen detection as well.)</p> <blockquote> <ol> <li><p>When player moves off screen i call the Bounce method. I want the player not to be able to move off screen but to change direction and go back. This works on top and bottom but left and right edge very seldom. Mostly it does a wierd bounce and leaves the screen. </p></li> <li><p>I would like to modify the accelerate algorithm so that i can set a max speed AND a acceleration speed. Atm the TangentalVelocity does both. </p></li> </ol> </blockquote> <pre><code>float TangentalVelocity = 8f; //Called when up arrow is down private void Accelerate() { Velocity.X = (float)Math.Cos(Rotation) * TangentalVelocity; Velocity.Y = (float)Math.Sin(Rotation) * TangentalVelocity; } //Called once per update private void Deccelerate() { Velocity.X = Velocity.X -= Friction * Velocity.X; Velocity.Y = Velocity.Y -= Friction * Velocity.Y; } // Called when player hits screen edge private void Bounce() { Rotation = Rotation * -1; Velocity = Velocity * -1; SoundManager.Vulture.Play(); } //screen edge detection public void CheckForOutOfScreen() { //Check if ABOVE screen if (Position.Y - Origin.Y / 2 &lt; GameEngine.Viewport.Y) { OnExitScreen(); } else //Check if BELOW screen if (Position.Y + Origin.Y / 2 &gt; GameEngine.Viewport.Height) { OnExitScreen(); } else //Check if RIGHT of screen if (this.Position.X + Origin.X / 2 &gt; GameEngine.Viewport.Width) { OnExitScreen(); } else //Check if LEFT of screen if (this.Position.X - Origin.X / 2 &lt; GameEngine.Viewport.X) { OnExitScreen(); } else { if (OnScreen == false) OnScreen = true; } } virtual public void OnExitScreen() { OnScreen = false; Bounce(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload