Commit 2cff6957 by 李楚霏

调整移动;调整触碰逻辑

parent b8bf8c22
......@@ -21,7 +21,8 @@ export default class NewClass extends cc.Component {
points:cc.Vec3;
startCollision: boolean =false;
relativeVelocity: any;
dir: cc.Vec3;
startMove: boolean = false;
// LIFE-CYCLE CALLBACKS:
onBeginContact(contact, selfCollider, otherCollider) {
......@@ -31,7 +32,6 @@ export default class NewClass extends cc.Component {
}
onPreSolve(contact, selfCollider, otherCollider) {
if (otherCollider.node.name.includes('staticGear')) {
this.rigidBody.angularVelocity = - otherCollider.node.getComponent(cc.RigidBody).angularVelocity;
// this.node.position = otherCollider.node.position;
var worldManifold = contact.getWorldManifold();
var vel1 = selfCollider.node.getComponent(cc.RigidBody).getLinearVelocityFromWorldPoint(worldManifold.points[0]);
......@@ -41,7 +41,8 @@ export default class NewClass extends cc.Component {
this.rigidBody.linearVelocity = this.relativeVelocity;
setTimeout(() => {
this.rigidBody.linearVelocity = cc.v2(0,0);
}, 300);
this.rigidBody.angularVelocity = - otherCollider.node.getComponent(cc.RigidBody).angularVelocity;
}, 100);
}, 0);
}
}
......@@ -70,48 +71,38 @@ export default class NewClass extends cc.Component {
this.startPoint = event.getStartLocation();
}
_touchMoveEvent(event: cc.Event.EventTouch) {
// this.points.push(event.getLocation());
// if (this.points.length > 20) {
// this.points.shift();
// }
// const averagePoint = new cc.Vec2(0, 0);
// this.points.forEach((point) => {
// averagePoint.x += point.x;
// averagePoint.y += point.y;
// })
// averagePoint.multiplyScalar(1 / this.points.length);
// const delta = new cc.Vec2(event.getLocation().x - averagePoint.x, event.getLocation().y - averagePoint.y);
// let dir = delta;
let dir = cc.v2(event.getLocation().x - this.startPoint.x, event.getLocation().y - this.startPoint.y);
// clientEvent.dispatchEvent('move', dir);
this.move(dir);
var touches = event.getTouches();
let newPos = this.node.parent.convertToNodeSpaceAR(touches[0].getLocation());
// let subPos = oldPos.sub(newPos);
// let dir = cc.v3(this.nodePos.x - subPos.x, this.nodePos.y - subPos.y, 0);
this.dir = cc.v3(newPos.x, newPos.y, 0);
this.startMove = true;
}
_touchEndEvent(event: cc.Event.EventTouch) {
// this.points = [];
// clientEvent.dispatchEvent('moveEnd');
}
_touchCancelEvent(event: cc.Event.EventTouch) {
// clientEvent.dispatchEvent('moveEnd');
this.moveEnd();
}
move(dir) {
this.node.position = dir;
// this.rigidBody.awake = true;
cc.director.getPhysicsManager().enabled = false;
this.startMove = false;
}
moveEnd() {
cc.director.getPhysicsManager().enabled = true;
}
start () {
}
update (dt) {
if(!this.startMove){
return;
}
const oldPos = cc.v3 (this.node.position.x, this.node.position.y, 0);
let targetPos = this.dir.clone();
let subPos = targetPos.sub(oldPos);
let distance = subPos.mag();
if (distance > 100) {
targetPos = oldPos.clone().add(targetPos.sub(oldPos.clone()).normalizeSelf().mul(100));
}
this.node.position = this.node.position.clone().lerp(targetPos, 4*dt);
}
}
......@@ -9,37 +9,24 @@ const {ccclass, property} = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
@property(cc.Label)
label: cc.Label = null;
@property
text: string = 'hello';
rigidBody: cc.RigidBody;
// LIFE-CYCLE CALLBACKS:
onPreSolve(contact, selfCollider, otherCollider) {
// console.error(otherCollider.node.name);
// if (otherCollider && otherCollider.node.getComponent(cc.MotorJoint)) {
// // this.rigidBody.awake = false;
// console.error(selfCollider.name);
// otherCollider.node.addComponent(cc.MotorJoint);
// const motor = otherCollider.node.getComponent(cc.MotorJoint);
// motor.connectedBody = this.node;
// motor.maxTorque = -200;
// }
// const motorJoint =this.node.getComponent(cc.MotorJoint);
// motorJoint.connectedBody = otherCollider.node;
// console.error(motorJoint.connectedBody.name);
this.rigidBody.linearVelocity = cc.v2(0, -otherCollider.node.getComponent(cc.RigidBody).angularVelocity * 3);
}
onPostSolve(contact, selfCollider, otherCollider) {
this.rigidBody.linearVelocity = cc.v2(0, 0);
}
this.scheduleOnce(() => {
this.rigidBody.type = cc.RigidBodyType.Kinematic;
}, 0);
this.rigidBody.linearVelocity = cc.v2(0, -otherCollider.node.getComponent(cc.RigidBody).angularVelocity);
}
onPostSolve(contact, selfCollider, otherCollider) {
// this.rigidBody.linearVelocity = cc.v2(0, 0);
}
onLoad () {
this.rigidBody = this.node.getComponent(cc.RigidBody);
this.rigidBody.type = cc.RigidBodyType.Dynamic;
}
start () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment