# MDX - Sprites

This topic is 4386 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im creating a simple sprite based engine. I've created a method which takes a source point : (_moveStart) and a target point : (_moveTarget) A sprite then moves long the line (or diagonal) between these two points. However, at times, the sprite's movement becomes very jagged. I dont understand why this happends. Does anyone have a better way of moving a sprite between two points? My code: ================================================== //finds the difference between the two points Vector3 diff = _moveTarget - _moveStart; //nomralizes the difference vector diff.Normalize(); //increases the x and y values with the values //from the normalized vector _moveCurrent += diff * _speedMove * 10; //updates the position of the sprite _position.X = _moveCurrent.X; _position.Y = _moveCurrent.Y; ==================================================

##### Share on other sites

I guess you just need to make the movement time-frame based, that is, store the time it takes to complete one frame (and the prev frame) and use the difference between the two to calculate a new _speedMove each frame.

##### Share on other sites
What Mille said [wink] In code, this comes down to:

[source lang=c#]private void MoveOurPoint( float lastFrameTime ){	Vector3 start = ourPoint.Position;	Vector3 end = target.Position;	Vector3 distance = end - start;	Vector3 direction = Vector3.Normalize(distance);	ourPoint.Position += speed * lastFrameTime * direction;}

If you are using the SDK's SampleFramework, you can use its timer to easily obtain the last frame time, like in this sample project. If you don't want to use the entire framework, you can still reuse their high-resolution timer by adding the framework's dxmutmisc.cs file to your project. System.Environment.TickCount most likely will not cut it, because this is typically only updated once every 15 milliseconds, so at high framerates your animation still might not be smooth.

Hope this helps :)

##### Share on other sites
Thanks for the replies.

Both of the methods work well. However, i still get a small jagged movement when i set the moving speed to small numbers. It is as if the sprite first moves down, then moves sideways, then moves down, then moves sideways, and so on... (in a zigzag movement).

However, at high speeds it works well. If might just have something to do with the precision errors. I guess I'll have to live with it.

I also added some code so that the sprite doesnt shake when it has reached its target (this has to do with the float point precision i think).

// If the distance between the target and the current position is
// less than 0.1 then i just reduce the distance to zero.
// Otherwise it keeps moving slightly between these to points.
if (distance.X < 0.1f && distance.Y < 0.1f)
direction = new Vector3();

If anyone has a different method for moving sprites, please post it :)

##### Share on other sites
If you still receive some jagged movement and you are using Visual Studio 2005, make sure that you test it outside of visual studio meaning that you run the executable it produced in your /Debug or /Release directories. It seems that the host process that visual studio 2005 runs alongside your project will cause a bit of jerkiness where your final executable will run much more smoother :)

I hope this helps.
Take care.

##### Share on other sites
thanks for the tip!

##### Share on other sites
Quote:
 Original post by phb5000It is as if the sprite first moves down, then moves sideways, then moves down, then moves sideways, and so on... (in a zigzag movement).However, at high speeds it works well. If might just have something to do with the precision errors. I guess I'll have to live with it.

Do you use the Draw2D() method of the Sprite?

I am currently running a game on my C#/MDX sprite based game engine, and I had the same problem. I fixed it by using the Draw() method instead, because Draw() allows coordinates like (95.64f,26.45f).

I checked the Draw2D() method using IntelliSense, and it seemed like Draw2D() now also supports floating points coordinates, but if you use an earlier version of MDX, the problem might be that Draw2D() only support integer coordinates, since AFAIK that method is supposed to be used for HUD-like graphics only.

##### Share on other sites
Im using the Draw method and everything seems to be working fine now ;)

By the way, is your C#/MDX sprite based game engine open source?

I'd like to get some development tips :).

(I can't really get my animation working with a set "animation speed")

Any tips?

##### Share on other sites
It is not by definition open source, but when the game is finnished I will release the source with an 'educational' licence that allows anyone to see how the game and it's engine works, but not derive any games from it (at least not without letting me know).

And basicaly, the game is finnished, I am just cleaning up code and making the last adjustement.

However, since polishing a game is boring, and the fact that I am studying maths at LTH, this takes time.

In a not too far distant feature, I will post a beta of the game here on GD with two goals:

1. Getting someone to do the graphics
2. Straighten out bugs and improving gameplay.

When that is done, I will release the game and it's full source at my website:
chromecode.com.

Anyway, I sent you a PM with my MSN mail, so if you want we can discuss things over there.