0

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.

Derek Lawrence
  • 1,551
  • 2
  • 16
  • 36
  • 1
    I'm pretty sure that dividing `-9.8 / 60` is risky because not every frame will take exactly 1/60th of a second. If your users machine can only keep up, for example, at 50fps, then the physics will be all wrong. Doesn't Oimo.js have a `deltaTime` variable that gives you the fraction of a second elapsed since last frame? – M - Jan 16 '20 at 18:36
  • the /60 is only calculated once not every frame. – Derek Lawrence Jan 16 '20 at 18:49
  • Im not calulating this every frame Im just trying to get a initial velocity – Derek Lawrence Jan 16 '20 at 19:19
  • Just a bookkeeping observation: The `three.js` tag doesn't seem necessary to me, because it doesn't seem to be involved with the problem as described. It would be better to use `Oimo.js` as a tag (if you can't create one, let us know, I'm sure someone would be willing to do it). – TheJim01 Jan 16 '20 at 21:49
  • The ballistic motion should be independent of the framerate – Ghost Jan 19 '20 at 00:49
  • **(1)** Is `distanceToTarget` the *horizontal* distance to said target? If not then the relative elevation must be taken into account. **(2)** `velocity` is not correctly normalized, i.e. the actual vector will not necessarily be inclined at 30 degrees. **(3)** The formula in `calculateVelocity` is incorrect - have another think about it and check your derivation; in any case it should not depend on the simulation timestep. – meowgoesthedog Jan 24 '20 at 15:53

0 Answers0