# How can I make a sprite jump at a particular angle?

I have a sprite that is moving horizontally across the screen. I want it to jump when the user touches the screen. The jump needs to be at the angle of the touch and the sprite’s current position.

I found out that I could use:

Math.atan(deltay,deltax) * 180 / PI


to get the angle between the current position and the touch on the screen.

I tried doing this and then calculated the velocity of jump using:

velocity += 1.5f * Math.cos(angleInDegrees);


where I used 1.5f as a random number. Can I do that?

The sprite doesn't jump properly. What am I doing wrong?

• Instead of what you expect, what happens? What's the velocity variable; a vector or a scalar? In your angle calculation, the atan bit is right, but you seem to be trying to convert it from radians into degrees. Math.cos likely expects a parameter in radians.
– Anko
Feb 17, 2014 at 8:59
• velocity is the velocity by which the character jumps. Its a scalar. I was doing degrees and i saw that the character was not even jumping properly when i touched the screen. It sometimes jumped and sometimes did not but definetely not at the angle needed. I tried radians and it jumps but just the same amount everytime.. even if the angle is smaller. Feb 17, 2014 at 9:39
• shouldn't y velocity be Math.sin(angle) and x velocity be Math.cos(angle) ? Feb 17, 2014 at 14:03

I'm assuming you want the sprite to jump towards the touch point, so instead of calculating and using the angle, rather just create a vector from the current position to the touch position:

dir.x = touch.x - current.x;
dir.y = touch.y - current.y;


Next, you need to normalize the vector (making it a unit/direction vector):

length = sqrt( (dir.x * dir.x) + (dir.y * dir.y) );
dir.x = dir.x / length;
dir.y = dir.y / length;


Finally, you can scale this vector by your jump velocity - the 1.5 you used in your example, or whatever value you want (you may want to experiment with this):

vel.x = dir.x * 1.5f;
vel.y = dir.y * 1.5f;


Note: You may also need to add your existing horizontal velocity to vel.x if needed.

This gives you a velocity vector in the direction of the jump, with the specified magnitude. Then you simply use it to adjust your sprite's position:

position.x = position.x + vel.x;
position.y = position.y + vel.y;


Then of course you will need some gravity (define it to some value based on what you need) to bring the sprite back down again, as well as some code to ensure when the ground is reached the jump ends:

position.y -= gravity;
if ( position.y <= ground.y )  {
vel.x = (original x velocity for movement only)
vel.y = 0;
}


These examples did not include any time values (for the sake of simplicity), but you will probably need to include it in the calculations.

• hi.. thanks for answering. I am sorry but i don't know how to define a direction vector. How should i define dir ? Can you help? Feb 20, 2014 at 8:55
• What do you mean by define? The second step creates the direction vector for you. If you mean how to declare the dir member in the example then you can use the PointF class. Or you could just use float values for x and y (they need not be in a vector class). Feb 20, 2014 at 10:10