Sign in to follow this  
andyjndm

Problem with rounded movement (XNA)

Recommended Posts

I'm trying to get an object to move smoothly (with a rounded motion) toward a point, but it's not going incredibly well.
I can get it to move straight toward the initial destination (The hump of the curve in the motion, in most cases), but when I try to update the angle constantly, to get it to round back to the final destination (The end of the curve), it moves erratically.
Here is my code for moving my object. Both of these are called every Update.
Let me know if you need any more of the code, to help determine the problem.

initdest is the "hump of the curve", which moves toward the final destination,
because I'm too lazy to actually make the object change targets.
finaldest is the final destination of the object.

public void getAngle()
{
//These statements are cut out,when not rounding to the second
//point, and this method is only run while initializing the object.
//This is not the problem, as I get (basically) The same effect
//without this chunk of code here
if (initdest.Y < finaldest.Y)
initdest.Y += gravity; //Gravity being the pull of "finaldest"
if (initdest.Y > finaldest.Y)
initdest.Y -= gravity;
if (initdest.X < finaldest.X)
initdest.X += gravity;
if (initdest.X > finaldest.X)
initdest.X -= gravity;

//Find the length of all of the sides of the triangle made with the
//position, and destination
double Lx = position.X - initdest.X;
double Ly = position.Y - initdest.Y;
double Lh = Math.Sqrt((Ly * Ly) + (Lx * Lx));
//Find the angle to fall at, using those lengths.
angle = (float)Math.Acos(((Ly * Ly) + (Lh * Lh) - (Lx * Lx)) / (2 * Ly * Lh));
}

public void move()
{
float slope = (float)Math.Tan(angle);
velocity.Y = -1 * slope; // Move the object Vertically
velocity.X = 1; // Move the object Horizontally
velocity *= speed; //Speed is generally just 1 for testing, anyway
position += velocity;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this