Commit 2cff6957 by 李楚霏

调整移动;调整触碰逻辑

parent b8bf8c22
...@@ -21,7 +21,8 @@ export default class NewClass extends cc.Component { ...@@ -21,7 +21,8 @@ export default class NewClass extends cc.Component {
points:cc.Vec3; points:cc.Vec3;
startCollision: boolean =false; startCollision: boolean =false;
relativeVelocity: any; relativeVelocity: any;
dir: cc.Vec3;
startMove: boolean = false;
// LIFE-CYCLE CALLBACKS: // LIFE-CYCLE CALLBACKS:
onBeginContact(contact, selfCollider, otherCollider) { onBeginContact(contact, selfCollider, otherCollider) {
...@@ -31,7 +32,6 @@ export default class NewClass extends cc.Component { ...@@ -31,7 +32,6 @@ export default class NewClass extends cc.Component {
} }
onPreSolve(contact, selfCollider, otherCollider) { onPreSolve(contact, selfCollider, otherCollider) {
if (otherCollider.node.name.includes('staticGear')) { if (otherCollider.node.name.includes('staticGear')) {
this.rigidBody.angularVelocity = - otherCollider.node.getComponent(cc.RigidBody).angularVelocity;
// this.node.position = otherCollider.node.position; // this.node.position = otherCollider.node.position;
var worldManifold = contact.getWorldManifold(); var worldManifold = contact.getWorldManifold();
var vel1 = selfCollider.node.getComponent(cc.RigidBody).getLinearVelocityFromWorldPoint(worldManifold.points[0]); var vel1 = selfCollider.node.getComponent(cc.RigidBody).getLinearVelocityFromWorldPoint(worldManifold.points[0]);
...@@ -41,7 +41,8 @@ export default class NewClass extends cc.Component { ...@@ -41,7 +41,8 @@ export default class NewClass extends cc.Component {
this.rigidBody.linearVelocity = this.relativeVelocity; this.rigidBody.linearVelocity = this.relativeVelocity;
setTimeout(() => { setTimeout(() => {
this.rigidBody.linearVelocity = cc.v2(0,0); this.rigidBody.linearVelocity = cc.v2(0,0);
}, 300); this.rigidBody.angularVelocity = - otherCollider.node.getComponent(cc.RigidBody).angularVelocity;
}, 100);
}, 0); }, 0);
} }
} }
...@@ -70,48 +71,38 @@ export default class NewClass extends cc.Component { ...@@ -70,48 +71,38 @@ export default class NewClass extends cc.Component {
this.startPoint = event.getStartLocation(); this.startPoint = event.getStartLocation();
} }
_touchMoveEvent(event: cc.Event.EventTouch) { _touchMoveEvent(event: cc.Event.EventTouch) {
// this.points.push(event.getLocation()); var touches = event.getTouches();
// if (this.points.length > 20) { let newPos = this.node.parent.convertToNodeSpaceAR(touches[0].getLocation());
// this.points.shift(); // let subPos = oldPos.sub(newPos);
// } // let dir = cc.v3(this.nodePos.x - subPos.x, this.nodePos.y - subPos.y, 0);
// const averagePoint = new cc.Vec2(0, 0); this.dir = cc.v3(newPos.x, newPos.y, 0);
// this.points.forEach((point) => { this.startMove = true;
// 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);
} }
_touchEndEvent(event: cc.Event.EventTouch) { _touchEndEvent(event: cc.Event.EventTouch) {
// this.points = [];
// clientEvent.dispatchEvent('moveEnd');
} }
_touchCancelEvent(event: cc.Event.EventTouch) { _touchCancelEvent(event: cc.Event.EventTouch) {
// clientEvent.dispatchEvent('moveEnd'); this.startMove = false;
this.moveEnd();
}
move(dir) {
this.node.position = dir;
// this.rigidBody.awake = true;
cc.director.getPhysicsManager().enabled = false;
} }
moveEnd() {
cc.director.getPhysicsManager().enabled = true;
}
start () { start () {
} }
update (dt) { 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; ...@@ -9,37 +9,24 @@ const {ccclass, property} = cc._decorator;
@ccclass @ccclass
export default class NewClass extends cc.Component { export default class NewClass extends cc.Component {
@property(cc.Label)
label: cc.Label = null;
@property
text: string = 'hello';
rigidBody: cc.RigidBody; rigidBody: cc.RigidBody;
// LIFE-CYCLE CALLBACKS: // LIFE-CYCLE CALLBACKS:
onPreSolve(contact, selfCollider, otherCollider) { onPreSolve(contact, selfCollider, otherCollider) {
// console.error(otherCollider.node.name); this.scheduleOnce(() => {
// if (otherCollider && otherCollider.node.getComponent(cc.MotorJoint)) { this.rigidBody.type = cc.RigidBodyType.Kinematic;
// // this.rigidBody.awake = false; }, 0);
// console.error(selfCollider.name); this.rigidBody.linearVelocity = cc.v2(0, -otherCollider.node.getComponent(cc.RigidBody).angularVelocity);
// otherCollider.node.addComponent(cc.MotorJoint);
// const motor = otherCollider.node.getComponent(cc.MotorJoint); }
// motor.connectedBody = this.node; onPostSolve(contact, selfCollider, otherCollider) {
// motor.maxTorque = -200; // this.rigidBody.linearVelocity = cc.v2(0, 0);
// } }
// 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);
}
onLoad () { onLoad () {
this.rigidBody = this.node.getComponent(cc.RigidBody); this.rigidBody = this.node.getComponent(cc.RigidBody);
this.rigidBody.type = cc.RigidBodyType.Dynamic;
} }
start () { 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