Jump to content
  • Advertisement
Sign in to follow this  
n00bz0rd2

D3D Sprites and Movement

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having a bit of trouble with a game that I am making. It is a very basic setup. I have a sprite but I want him to move towards the place I click. Second problem, how would I go about creating many of the same sprite deriving from one class, and when you click on the sprite it will select it allowing you to move it with the mouse click. I have attached my project as I do not know where the exact problem lies. Thanks. P.S there are no .exe's in the zip file http://www.mediafire.com/?kbcvxgqci11

Share this post


Link to post
Share on other sites
Advertisement
the basic movement algorithm goes something like this..

spritePos += (mouseClickPos - spritePos).normalize * timeStep;

that will move your sprite along the vector that is between your mouse and it.

If you want it to be more natural though with a 'turn to' the algorithm would be different.

For your second issue, you will want to have the d3dxSprite object common in your class using something like the keyword static. This most likely isnt the best way to do it.

The better way would be to have a sprite manager that loads sprites for you and returns a index number. That way you can reference the same memory to draw the sprite and only change the modifiers (position, color saturation, size, etc..)

Hope that helped :-D

Share this post


Link to post
Share on other sites
Thanks for the help. Where do I add that algorithm? I figured out the the man isn't being updated with the mouse coordinates but I can't figure out how to update it.

Share this post


Link to post
Share on other sites
I am not sure how you have your objects set up but if you have an object manager with the character in there.. im assuming you have an update function being called. The update would be a good place to put it, something like

if (myPos != mouseClickPos)
{
//Enter movement here
}

to get the current mouse click pos there are many ways im sure.. I believe there is a GetCursorPos() or something like that build into the windows header

Share this post


Link to post
Share on other sites
Quote:
Second problem, how would I go about creating many of the same sprite deriving from one class

This problem is easily solved by thinking about it logically.
You have a class that is the skeleton for a renderable game object. The game object's properties will probably have something like the following.

ID - To uniquely identify this object.
Position - The object's position in the game
Width
Height - Width and Height of the object, be it for rendering and even for collision detection.
and so on...

When you create your object, you can either generate a GUID or anything to uniquely identify the object.

When you click on the object, you can iterate through your collection of game objects and check your mouse position against the collection of objects.
The check will do a check on whether your mouse cursor is in the boundaries of the object. Because this is a sprite based engine, you only need to check 2 axis.

Quote:
and when you click on the sprite it will select it allowing you to move it with the mouse click.

Moving a sprite, you can do this a number of ways and the easiest way is to use the parameters in the Sprite.Draw* methods, this is the position that you are rendering the sprite at.

When you click and identify the game object, you can mark a flag as "mouseHasBeenClicked" and then when you click again, you can check if this variable is true, which will tell you that your next click will be the destination and thus, you can move the sprite along a path to your new mouse position.

So, take the speed of your object and multiply that by your ElapsedGameTime and you can either move your object along a gradient which you can calculate using Trig or a method of your choosing.

I hope this has helped you a bit ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!