C#: Object overshoots target
I am having trouble diagnosing the following issue.
There are 2 objects, a ring and a target. The ring represents a ship and the target represents the destination. When the user clicks anywhere in the window, the target is placed where开发者_如何学编程 you click and the ship then moves to that location. The problem that I am having is the further the ship has to move, the more velocity that ship will achieve, thus overshooting its target. The further the ship has to move translates to a slower deceleration. I am not sure where this is happening. I have provided the code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace ArtificialDumb
{
class PhysicsObject
{
public Vector2 Position;
public Vector2 OldPosition;
public float Mass;
public Vector2 Acceleration;
public float Drag = 0.01f;
public PhysicsObject(float x, float y)
{
Position = OldPosition = new Vector2(x, y);
}
public PhysicsObject(Vector2 pos)
{
Position = OldPosition = pos;
}
public virtual void Update()
{
Vector2 velocity = Position - OldPosition;
velocity *= (1 - Drag);
OldPosition = Position;
Position += velocity;
Position += Acceleration;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace ArtificialDumb
{
class Ship : PhysicsObject
{
public float MaxThrust;
public Vector2 Target;
public Ship(Vector2 pos)
: base(pos)
{
MaxThrust = 50f;
Mass = 100;
}
public override void Update()
{
Vector2 diff = Target - Position;
Vector2 Velocity = (Position - OldPosition);
// Dark Magic. Do Not Touch.
// This is the equation for projectile velocity. -ASR
// Edited for correct float value -ASR
// people keep touching! - AgH
Vector2 thrust = diff - (Velocity * Velocity.Length() * 0.75f);
thrust.Normalize();
// todo: Account for when we don't need to use maximum thrusters
thrust *= MaxThrust;
Acceleration = thrust / Mass;
base.Update();
}
}
}
This is less trivial than it seems. What makes it hard is you have to make your ship end up at the right position, with 0 velocity.
If you want to do it right and aren't afraid of maths, implement a PID controller. This is the standard way to solve these kinds of problems. It is uesd often in the industry for things such as autopilots.
However there is an easier way!
If you are already travelling straight towards your target, you can calulate where you will end up if you started decelerating now. Assuming your deceleration is constant:
endPos = Position + (Velocity * Velocity) / (2 * deceleration);
endPos is where you would end up if you started braking now. You can compare it to your target to see if you are currently overshooting or undershooting.
If you know that you need to brake, you can calculate exactly how much you need to brake to arrive at your target by rearranging the above formula:
deceleration = (Velocity * Velocity) / (2 * (endPos - Position));
The deceleration here is the 'thrust' that you need to give your ship to stop where you want it to.
I hope this helps, please tell me if you don't understand anything, or want an explanation to how I came up with the above formulas.
This is a common issue with physics simulations. What you'll want to do is, for every update, check the path from the last known position to the current position. If there's any geometry in the way, you know that you should have collided.
At that point you can choose how to react. Many physics libraries like Farseer will actually move the object back to the point of collision and then let the reaction (bounce, spin, etc.) happen from there
精彩评论