private static void BounceSphereInWorld(IMovable3 s, GameTime gameTime) { // vy = ay * dt + v0; Vector3 velocity = s.Velocity; Vector3 position = s.Position; if (velocity.Y > 0) { velocity.Y = velocity.Y - (float)(2f * 9.8f * gameTime.ElapsedGameTime.TotalSeconds); } // First test along the X axis, flipping the velocity if a collision occurs. if (s.Position.X < -worldSize + s.Radius) { position.X = -worldSize + s.Radius; if (s.Velocity.X < 0f) { velocity.X *= -1f; } } else if (s.Position.X > worldSize - s.Radius) { position.X = worldSize - s.Radius; if (s.Velocity.X > 0f) { velocity.X *= -1f; } } // Then we test the Y axis if (s.Position.Y < s.Radius) { position.Y = s.Radius; if (s.Velocity.Y < 0f) { velocity.Y *= -1f; } } else if (s.Position.Y > worldSize - s.Radius) { position.Y = worldSize - s.Radius; if (s.Velocity.Y > 0f) { velocity.Y *= -1f; } } // And lastly the Z axis if (s.Position.Z < -worldSize + s.Radius) { position.Z = -worldSize + s.Radius; if (s.Velocity.Z < 0f) { velocity.Z *= -1f; } } else if (s.Position.Z > worldSize - s.Radius) { position.Z = worldSize - s.Radius; if (s.Velocity.Z > 0f) { velocity.Z *= -1f; } } s.Position = position; s.Velocity = velocity; s.Rotation = new Vector3(0, PI2 * (float)(gameTime.TotalGameTime.TotalSeconds % 1), 0); }
private static void BounceSphereInWorld(IMovable3 s, GameTime gameTime) { // vy = ay * dt + v0; Vector3 velocity = s.Velocity; Vector3 position = s.Position; if (velocity.Y > 0) velocity.Y = velocity.Y - (float)(2f * 9.8f * gameTime.ElapsedGameTime.TotalSeconds); // First test along the X axis, flipping the velocity if a collision occurs. if (s.Position.X < -worldSize + s.Radius) { position.X = -worldSize + s.Radius; if (s.Velocity.X < 0f) velocity.X *= -1f; } else if (s.Position.X > worldSize - s.Radius) { position.X = worldSize - s.Radius; if (s.Velocity.X > 0f) velocity.X *= -1f; } // Then we test the Y axis if (s.Position.Y < s.Radius) { position.Y = s.Radius; if (s.Velocity.Y < 0f) velocity.Y *= -1f; } else if (s.Position.Y > worldSize - s.Radius) { position.Y = worldSize - s.Radius; if (s.Velocity.Y > 0f) velocity.Y *= -1f; } // And lastly the Z axis if (s.Position.Z < -worldSize + s.Radius) { position.Z = -worldSize + s.Radius; if (s.Velocity.Z < 0f) velocity.Z *= -1f; } else if (s.Position.Z > worldSize - s.Radius) { position.Z = worldSize - s.Radius; if (s.Velocity.Z > 0f) velocity.Z *= -1f; } s.Position = position; s.Velocity = velocity; s.Rotation = new Vector3(0, PI2 * (float)(gameTime.TotalGameTime.TotalSeconds % 1), 0); }