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?

  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Anko
    Feb 17, 2014 at 8:59
  • \$\begingroup\$ 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. \$\endgroup\$ Feb 17, 2014 at 9:39
  • \$\begingroup\$ shouldn't y velocity be Math.sin(angle) and x velocity be Math.cos(angle) ? \$\endgroup\$
    – Terje
    Feb 17, 2014 at 14:03

1 Answer 1


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.

  • \$\begingroup\$ 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? \$\endgroup\$ Feb 20, 2014 at 8:55
  • \$\begingroup\$ 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). \$\endgroup\$
    – free3dom
    Feb 20, 2014 at 10:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .