private void keepInBounds(Actor a) { Rectangle aBounds = a.getBufferedRectangleBounds(0); if (!gameplayBoundaries.Contains(aBounds)) { // 1.) determine the wall(s) with which we are colliding // 3.) ensure position within bounds // 2.) zero velocity and acceleration along those axes if (aBounds.Left < gameplayBoundaries.Left) { a.move((float)Math.Abs(gameplayBoundaries.Left - aBounds.Left) + EPSILON, 0.0f); if (a.velocity.X < 0.0f) a.velocity.X = 0.0f; //if (a.acceleration.X < 0.0f) // a.acceleration.X = 0.0f; a.acceleration.X = -(a.acceleration.X); } if (aBounds.Right > gameplayBoundaries.Right) { a.move(-((float)Math.Abs(gameplayBoundaries.Right - aBounds.Right) + EPSILON), 0.0f); if (a.velocity.X > 0.0f) a.velocity.X = 0.0f; //if (a.acceleration.X > 0.0f) // a.acceleration.X = 0.0f; a.acceleration.X = -(a.acceleration.X); } if (aBounds.Top < gameplayBoundaries.Top) { a.move(0.0f, (float)Math.Abs(gameplayBoundaries.Top - aBounds.Top) + EPSILON); if (a.velocity.Y < 0.0f) a.velocity.Y = 0.0f; //if (a.acceleration.Y < 0.0f) // a.acceleration.Y = 0.0f; a.acceleration.Y = -(a.acceleration.Y); } if (aBounds.Bottom > gameplayBoundaries.Bottom) { a.move(0.0f, -((float)Math.Abs(gameplayBoundaries.Bottom - aBounds.Bottom) + EPSILON)); if (a.velocity.Y > 0.0f) a.velocity.Y = 0.0f; //if (a.acceleration.Y > 0.0f) // a.acceleration.Y = 0.0f; a.acceleration.Y = -(a.acceleration.Y); } } }
public static void applyMovement(Actor a, float delta, Boolean applyFric ) { Vector2 acc = a.Acceleration; Vector2 vel = a.Velocity; Vector2 pos = a.Position; //this is not used currently /* Vector2 accFric = -1 * vel; Vector2 accFricNormalize = accFric; if (!accFricNormalize.Equals(Vector2.Zero)) accFricNormalize.Normalize(); else { accFricNormalize.X = (float) Math.Cos(a.Rotation); accFricNormalize.Y = (float)Math.Sin(a.Rotation); accFricNormalize.Normalize(); accFricNormalize = -1 * accFricNormalize; } */ a.Velocity = acc * delta + a.MiscAcceleration * delta + vel; // cap the max speed for each state if (a.CurrState != Actor.state.Dashing && Math.Sqrt(magnitudeSquared(a.Velocity)) > a.MaxVel && !a.IsBeingKnockedBack) a.Velocity = vel = a.MaxVel * Vector2.Normalize(a.Velocity); else if(a.CurrState == Actor.state.Dashing && Math.Sqrt(magnitudeSquared(a.Velocity)) > a.MaxVelDash && !a.IsBeingKnockedBack) a.Velocity = a.MaxVelDash * Vector2.Normalize(a.Velocity); // cap total max speed if (Math.Sqrt(magnitudeSquared(a.Velocity)) > Actor.MAX_POSSIBLE_SPEED) a.velocity = Actor.MAX_POSSIBLE_SPEED * Vector2.Normalize(a.Velocity); //relative friction if (applyFric) a.Velocity = vel = uk * a.Velocity; //a.Position = pos = vel * delta + pos; Vector2 dV = vel * delta; a.move(dV.X, dV.Y); //Rotations //if ( !finalVelNormalize.Equals(Vector2.Zero)) if (!acc.Equals(Vector2.Zero)) // turn direction to mirror controller { a.Rotation = VectorToAngle(a.Acceleration); } //update the bounds float s = (float)Math.Sin(a.Rotation); float c = (float)Math.Cos(a.Rotation); //rotate about the origin then add to position a.bounds.center.X = /*c * (a.Offset.X) - s * (a.Offset.Y) +*/ a.Position.X; a.bounds.center.Y = /* s * (a.Offset.X) + c * (a.Offset.Y) +*/ a.Position.Y; if (a is Shark) { Shark sh = (Shark)a; sh.mouthPoint = sh.Bounds.center; sh.mouthPoint.X += 100; RotatePoint(sh.Bounds.center.X, sh.Bounds.center.Y, sh.Rotation, ref sh.mouthPoint); sh.mouthCircle.center = sh.mouthPoint; } if (a is Penguin) { Penguin p = (Penguin)a; p.spearPoint = p.Bounds.center; p.spearPoint.X += p.spearLength; RotatePoint(p.Bounds.center.X, p.Bounds.center.Y, p.Rotation, ref p.spearPoint); p.spearCircle.center = p.spearPoint; p.spearDeflectorAura.center = p.Position; } a.acceleration *= accDecay; a.MiscAcceleration *= accDecay; //zero if too small if ( Math.Abs(a.Velocity.X) < eps) a.velocity.X = 0; if ( Math.Abs(a.Velocity.Y) < eps) a.velocity.Y = 0; if ( Math.Abs(a.Acceleration.X) < eps) a.acceleration.X = 0; if ( Math.Abs(a.Acceleration.Y) < eps) a.acceleration.Y = 0; }
private void wrapAround(Actor a) { Rectangle aBounds = a.getBufferedRectangleBounds(0); if (!gameplayBoundaries.Contains(aBounds)) { // 1.) determine the wall(s) with which we are colliding // 2.) if we are completely outside on that wall, then // reflect to the other side of the stage. // off the right side if (aBounds.Left >= gameplayBoundaries.Right) { a.move(-(float)(gameplayBoundaries.Width + (aBounds.Left - gameplayBoundaries.Right)), 0.0f); } // off the left side if (aBounds.Right <= gameplayBoundaries.Left) { a.move((float)(gameplayBoundaries.Width + (gameplayBoundaries.Left - aBounds.Right)), 0.0f); } // off the bottom side if (aBounds.Top >= gameplayBoundaries.Bottom) { a.move(0.0f, -(float)((aBounds.Top - gameplayBoundaries.Bottom) + gameplayBoundaries.Height)); } // off the top side if (aBounds.Bottom <= gameplayBoundaries.Top) { a.move(0.0f, (float)((gameplayBoundaries.Top - aBounds.Bottom) + gameplayBoundaries.Height)); } } }