Jump to content
  • Advertisement
Sign in to follow this  

[java] Move a Sprite by Mouse clicking

This topic is 4693 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

Hello I'd like to move a Sprite by clicken the mouse Somewere at the screen: somhow like this
mouseClicked(int x, int y){
The Sprite should now move a bit at every update:
moveTo(int x, int y){
   int moveX = 0;
   int moveY = 0;
   //SOME CODE TO CALCULATE, how far the Sprite moves at  X an d how far at Y-Axxis (THIS IS WHAT Im ASKING FOR!)

posX = posX+moveX;
posY = posY+moveY;
The difficult is, that the sprite should move a straight Line! can someone help me?

Share this post

Link to post
Share on other sites

This is, how far the destiantion point is away.

BUT how can I get moveX and moveY to calculate the next step of the Sprite?

Share this post

Link to post
Share on other sites
Distance that you have moved between one frame and the next depends on how much time have elapsed, and how fast the object is moving.

const int SPRITE_SPEED = 10;

moveTo(int x, int y, int delta_time){
int moveX = 0;
int moveY = 0;

// calculate moveX and Y
moveX = SPRITE_SPEED * delta_time;
moveY = SPRITE_SPEED * delta_time;

// move twoards target coordinates. This is very simple method called tracking.
// It isn't so nice, but it works and maybe its enough for your needs.

if(posX < x) // still less than x? If so increase by moveX
posX = posX+moveX;
if(posX > x) // just check so that it dosnt move TOO far. Add the same for the other cases.
posX = x;
if(posX > x)
posX = posX-moveX;

// same for Y
if(posX > y)
posY = posY+moveY;
if(posX < y)
posY = posY-moveY;

Where delta_time is is the number of seconds elapsed since last frame.
If you are programming on windows, you could calculate that like this:

// at beginning of main game loop:
int time_last_frame = 0;
int time_this_frame = 0;
time_last_frame = time_this_frame; // save old time
time_this_frame = timeGetTime(); // link to winmm.lib
int delta_time = time_this_frame - time_last_frame; // how much time have elapsed between this frame and last one?

if(time_last_frame == 0) // this is just so we dont get a huge jump in time on the very first frame.
delta_time = 0;

moveTo(mouseClickX, MouseClickY, delta_time); // your func

Share this post

Link to post
Share on other sites
Thank you very much. It's working so far!

But one question is left:
Is there a way, to make the Sprite move mithout any curves.
I mean in one line.

Share this post

Link to post
Share on other sites
I didnt mean curve (sorry my english it not the best!)

I meant an angle.
The sprite is moving diagonal until (x = posX) or (y = posY).
Then its moving horizantal or vertikal.

I'll try in ASCII ;)

(Sprites Startpoint)
---------o o o o o o o o o (Sprites Destination)
(posY = y && posX != x)

What I want is something like that:

(Sprites Startpoint)
------------------ o(Sprites Destination)

Share this post

Link to post
Share on other sites
Yes it is. You will want to be using floats to do that tough.
What you need to do is to calculate the direction vector. And then normalize that vector and multiply it by speed and delta time.

//vector class
class vec2
public float x;
public float y;

// normalize it, means makeing the lenght of it to 1
vec2 normalize(vec2 v)
// calc the lenght of a vector
float m = sqrt(v.x*v.x + v.y*v.y);
if(m > 0.0f)
m = 1.0f / m;
m = 0.0f;

vec2 out
out.x = v.x * m;
out.y = v.y * m;
out.z = v.z * m;

return out;

to get the the direction vector, you take the pos of where the object is right now. And where it wants to go.

vec2 sprite_pos;
sprite_pos.x = sprite.posX;
sprite_pos.y = sprite.posY;

vec2 mouse_pos; // where you have clicked and want to go to.
mouse_pos.x = mouseposX;
mouse_pos.y = mouseposY;

vec2 direction = sprite_pos - mouse_pos; // the direction.

// normalize it, important.
direction = normalize(direction);

// now, in your moveTo function. You add the direction into the calculaton of moveX and Y. Like so:
moveX = SPRITE_SPEED * delta_time * direction.x;
moveY = SPRITE_SPEED * delta_time * direction.y;

I wrote this code from memory, so im not 100% sure normalization func is correct. But i think so. Otherwise just google for vector normalization.

Share this post

Link to post
Share on other sites
Don't forget to check to see how far your sprite is from the destination every frame. If the sprite moves 10px/frame and the destination is 5px away, the sprite is going to constantly jump around the destination. Make the sprite's position equal to the destination if the distance is less than the speed.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster

but what do you mean with:

vec2 direction = sprite_pos - mouse_pos;

I cannot subtract one object from another!

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!