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
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:
updateMove(delta){
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