1.需求: 提前绘制物理系统的抛物线。
2.前提: 使用物理系统 (不要自己用加速度去算,很low。而且自己搞一套重力看起来很爽,万一其他的物体也要用,那就又写一套重力系统) ; 预设好想要的速度。
3.关键:我希望物体按照我定义好的速度轨迹运行,自己运算的结果是否与物理系统的计算吻合。先不要自己去设置重力,用系统默认的,后面可以自己再设计重力反推。
4.步骤:一共3个球,bulletNode bulletNode2 都是一开始静止的且重合。bulletNode3 一开始就有速度。
5.结论:用3种方法,对应三个球,达到我需要的轨迹。
6.设置重力 : cc.director.getPhysicsManager().gravity = cc.v2(200, -640);
private spx: number = 200;
private spy: number = 300;
cc.game.setFrameRate(30);
cc.director.getPhysicsManager().enabled = true;
cc.director.getPhysicsManager().debugDrawFlags =
cc.PhysicsManager.DrawBits.e_aabbBit |
cc.PhysicsManager.DrawBits.e_jointBit |
cc.PhysicsManager.DrawBits.e_shapeBit;
//star中开个定时器运行。
let tween = cc.tween(this.node)
// 同时执行两个 cc.tween
.delay(2)
.call(() => {
//改變速度
this.bulletNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(this.spx, this.spy)
this.bulletNode.isAwake = true;//可有可无
this.bulletNode.active = true; //激活刚体 这样写的.
//使用力
let CV: cc.Vec2 = this.bulletNode2.getComponent(cc.RigidBody).linearVelocity; //當前的速度
let EV: cc.Vec2 = cc.v2(this.spx, this.spy);//第一幀需要達到的速度 我需要的
let DV: cc.Vec2 = EV.subtract(CV);// 速度差
let t = 1 / cc.game.getFrameRate(); //一幀需要的速度
let a = DV.divide(t); //加速度
let force:cc.Vec2 = a.mul(this.bulletNode2.getComponent(cc.RigidBody).getMass()); //力
this.bulletNode2.getComponent(cc.RigidBody).applyForceToCenter(force, true); //
this.bulletNode2.isAwake = true;//可有可无
this.bulletNode2.active = true; //激活刚体 这样写的
//預先計算的軌跡
let count = 0
let num = 0;
for (num = 0; num < 1000; num++) {
count = count + 1 / 60;
let y = this.spy * count + count * count * cc.director.getPhysicsManager().gravity.y / 2
let x = this.spx * count + count * count *cc.director.getPhysicsManager().gravity.x/ 2;
this.getComponent('ArrowTailPool').getparticleTail((newNode: any) => {
newNode.setPosition(cc.v2(x, y));
this.node.addChild(newNode);
}, this);
}
})
.delay(1.5)
.call(() => {
//疊加衝量
this.bulletNode3.setPosition(cc.v2(0,0)); //先同步一下計算方式
let currentV: cc.Vec2 = this.bulletNode3.getComponent(cc.RigidBody).linearVelocity; //當前的速度
let myV: cc.Vec2 = cc.v2(this.spx, this.spy);//我需要的速度
let desV: cc.Vec2 = myV.subtract(currentV);// 速度差
let inputVcc: cc.Vec2= desV.mul(this.bulletNode3.getComponent(cc.RigidBody).getMass());
this.bulletNode3.getComponent(cc.RigidBody).applyLinearImpulse( inputVcc,cc.v2(0,0),true);
})
.delay(10)
.call(() => {
});
cc.tween(this.node).repeat(10, tween).start();