我使用带有 3 个 js 的 Oimo.js 物理库。
我向一个目标射击,但我的数学似乎不正确,而且我无法准确记住所有运动学公式的工作原理。
我有一个攻击函数,它创建一个弹丸并用 3d 矢量发射它。但它并没有像我想象的那样表现,最终需要硬编码一个也不起作用的值。有人可以指出我正确的方向吗?我还希望箭头在其轨迹上有一个轻微的弧度。
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 );
}
由于 oimo.js 时间步长,我除以 60。