Im using Oimo.js Physics library with 3 js.
I fire my arrow at a target but my math doesn't seem to be right and I'm having trouble remembering exactly how all the kinematic formulas works.
I have an attack function which creates a projectile and fires it with a 3d vector. But its not behaving how I thought it would and ended up needing to hard code a y value which doesn't really work either. Can someone point me in the correct direction? I also want the arrow to have a slight arc in its trajectory.
public attack( target: Unit, isPlayer: boolean ): void {
let collisionGroup = isPlayer ? CollisionGroup.PLAYER_PROJECTILES : CollisionGroup.ENEMY_PROJECTILES;
let collidesWithGroup = isPlayer ? CollidesWith.PLAYER_PROJECTILES : CollidesWith.ENEMY_PROJECTILES;
this.model.lookAt( target.position );
let direction: Vector3 = new Vector3( 0, 0, 0 );
direction = this.model.getWorldDirection( direction );
let value = this.calculateVelocity();
let velocity = new Vector3( direction.x * value, Math.sin( _Math.degToRad( 30 ) ) * value, direction.z * value );
let arrow = this.gameWorld.addProjectile( 'arrow3d', 'box', false, new Vector3( this.model.position.x, 5, this.model.position.z ), new Vector3( 0, 0, 0 ), false, collisionGroup, collidesWithGroup );
arrow.scale = new Vector3( 10, 10, 5 );
arrow.setVelocity( velocity );
this.playAnimation( 'attack', false );
}
protected calculateVelocity(): number {
return Math.sqrt( -2 * ( -9.8 / 60 ) * this.distanceToTarget );
}
Im dividing by 60 because of the oimo.js timestep.