[java] Move a Sprite by Mouse clicking

Started by
13 comments, last by Zahlman 18 years, 3 months ago
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?
Advertisement
deltaX = x - posX;
deltaY = y - posY;
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?
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();}
Shields up! Rrrrred alert!
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.

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?
----------[Development Journal]
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)





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.
Shields up! Rrrrred alert!
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.
Thanks

but what do you mean with:


vec2 direction = sprite_pos - mouse_pos;

I cannot subtract one object from another!

This topic is closed to new replies.

Advertisement