// ----------------------------- Update ----------------------------------- // // updates the ball physics, tests for any collisions and adjusts // the ball's velocity accordingly // ------------------------------------------------------------------------ public void update(float tpf) { // keep a record of the old position so the goal::scored method // can utilize it for goal testing if (position != null) { oldPos.cloneVec(position); } // Test for collisions testCollisionWithWalls(pitchBoundary); // Simulate Params.Instance().Friction. Make sure the speed is positive // first though double friction = Params.Instance().Friction; // println (velocity) // println("LengthSq" + velocity.LengthSq() +" friction " + friction) if (velocity.LengthSq() > friction * friction) { velocity = velocity.plus(Vec2DNormalize(velocity).multiply(friction)); // println (velocity) position = position.plus(velocity.multiply(tpf)); // println (position) // update heading heading = Vec2DNormalize(velocity); } super.update(tpf); }
// ----------------------- PlaceAtLocation ------------------------------------- // // positions the ball at the desired location and sets the ball's velocity to // zero // ----------------------------------------------------------------------------- public void placeAtPosition(Vector2D NewPos) { position = NewPos; oldPos.cloneVec(position); velocity.setZero(); }
// -------------------------- Kick ---------------------------------------- // // applys a force to the ball in the direction of heading. Truncates // the new velocity to make sure it doesn't exceed the max allowable. // ------------------------------------------------------------------------ public void kick(Vector2D direction, double force) { // ensure direction is normalized Vector2D d = direction.cloneVec(); d.Normalize(); // calculate the acceleration Vector2D acceleration = d.multiply(force).divide(mass); // update the velocity velocity.cloneVec(acceleration); }