I tried for several days to solve my problem , but in the end I just gave up and I decided to ask , since the solution is beyond my knowledge

I'm writing a 2d top-down game, in order to simulate the heigth of an object i use a third axis (the image below make a better idea) , and i stucked while writing an AI for an enemy

This enemy, which is located over an A point, must jump over a B point, and the duration of the jump must be T seconds, my problem is how to find the velocity to apply in order to obtain the jump that i want

enter image description here

The image may be not clear but the 'height' axis is the z, while the x and the y are the standard axis of a cartesian diagram

This is the part of the code of the enemy where the jump start, then where forces are computed:

var p1 = new Point(this.xCenter(), this.yCenter());
var p2 = new Point(this.target.xCenter(), this.target.yCenter());
var direction = Math.atan2(p1.y - p2.y, p1.x - p2.x);

var time = 0.3;             //Jump time

var g = gravity;            //Gravity

var distance = distanceBeetweenTwoPoints(p1, p2);       //Distance

var xForce = (-(Math.cos(direction)) * ???;

var yForce = (-(Math.sin(direction)) * ???;

var zForce = ???;

this.xSpeed = xForce;
this.ySpeed = yForce;
this.zSpeed = zForce;

I obtain the direction in radians, if the enemy has simply to walk towards the point b this works

The following method is called in order to start the jump, then every frame the movement of the enemy is updated with this:

  this.x += this.xSpeed * delta;
  this.y += this.ySpeed * delta;
  this.z += this.zSpeed * delta;
  if (this.z > 0) this.zSpeed -= gravity * delta; //Gravity has global scope
  if (this.z < 0) { //When floor is touched again
    this.z = 0;
    this.zSpeed = 0;

I omit several parts of code, for example the part where i stop the forces where floor is again touched

So the main question are:

  • how to get the z (height) force to apply at the jump in order to make it last T seconds?
  • how to get the x and y forces needed to move to point b in T seconds?

This answer isn't my case since it describes in a different context of 2D, my jump should affect 3 axis

  • 4
    \$\begingroup\$ gamedev.stackexchange.com/questions/68559/… \$\endgroup\$ – MickLH May 3 '16 at 13:52
  • \$\begingroup\$ I don't think there is anything dimension specific in the answer @MickLH linked, and the answer there describes the math involved in your question too. \$\endgroup\$ – Tyyppi_77 May 3 '16 at 19:26
  • \$\begingroup\$ That answer is exactly your case, it's exactly the math to relate gravity, time, height, and impulse strength as variables. Gravity is only in one "dimension" in either case. (Don't expect me to work so hard to make a nice post, when you won't work hard enough to move a variable) \$\endgroup\$ – MickLH May 3 '16 at 23:20

You can try some some physic's projectile motion:

From the horizontal displacement the maximum distance of projectile:

d = v0 *  t * cos(theta) 

where d is ab distance , v0 = start velocity, t= time , theta= trow angle (usualy Pi/4 for max trow distance)


v0 = d /(  t * cos(theta) )

then you can get the x y and z component of V0 using theta and the ab vector.

| improve this answer | |
  • \$\begingroup\$ I'm not sure how about use this, how should i get the x y and z components? I'm trying to study the page but so far i just obtained poor results... \$\endgroup\$ – Tizio Fittizio May 3 '16 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.