# [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.

## 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){
Sprite.moveTo(x,y);
}

The Sprite should now move a bit at every update:
SPRITE:
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 on other sites
deltaX = x - posX;
deltaY = y - posY;

##### Share on other sites
Thanks!

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 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 YmoveX = 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 Yif(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;while(GAME_IS_RUNNIG{  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   Render();}

##### 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 on other sites
If you're only dealing with x and y your sprite should be moving in a straight line.

What kind of curve are you talking about?

##### 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 o o o o o o (Sprites Destination)
(posY = y && posX != x)

What I want is something like that:

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

##### 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 classclass vec2{	public float x;	public float y;};// normalize it, means makeing the lenght of it to 1vec2 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;	else		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 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 on other sites
Thanks

but what do you mean with:

vec2 direction = sprite_pos - mouse_pos;

I cannot subtract one object from another!

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 12
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
633349
• Total Posts
3011464
• ### Who's Online (See full list)

There are no registered users currently online

×