445

I'm kinda new to this whole XNA C# programming stuff so sorry if this is an insanely easy question. I have recently tried to play around with some physics for a 2-d game that I am making. I tried to make an object go towards a point that the mouse clicks on. Here the code i used:

private void PointPhysics(Vector2 pos, Vector2 force)

{

totalpos = (Position - pos);

mulpos.X = totalpos.Y * force.X - totalpos.X * force.Y;

mulpos.Y = totalpos.X * force.Y - totalpos.Y * force.X;

Position += mulpos;

}

Now the mathematics in this aren't entirely worked out because when I use this kind function the object moves only on a diagonal path, from the top left to the bottom right. Any help on this would be useful.



Re: XNA Game Studio Express Help with moving towards a given point

Fluxtah

maybe get the direction you want it to travel first

direction = targetPos - currentPos;
direction.Normalize()

then move it in that direction until currentPos = targetPos

currentPos.X += direction.X * dt;
currentPos.Y += direction.Y * dt;

Its just off the top of my head and I havent tested it but it should work, you might need to add some speed also to each component, which essentially would end up being velocity

velocity = direction * 5.0f for instance;





Re: XNA Game Studio Express Help with moving towards a given point

445

sry, but what do you mean by dt



Re: XNA Game Studio Express Help with moving towards a given point

Fluxtah

sorry that will be the time since the last frame, you can use gameTime.ElapsedGameTime.TotalMilliseconds for that.

dont forget to add a little speed to the normalized direction vector or it will probably crawl along really slowly :)

 

 





Re: XNA Game Studio Express Help with moving towards a given point

jbizzler

'dt' is commonly used in game development. It is short for DeltaTime, or change in time. Since your PointPhysics isn't called exactly the same time every time, you need to multiply your change in position by the change in time to keep the movement consistent. Otherwise, a fast computer would make the move fast and a slow one would make it slow.