Created
January 10, 2022 19:09
-
-
Save dested/e70b3779dd666f481e2d8687d53e5c7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let result = new Vector3(0, 0, 0); | |
world.contactPair(myCollider!.handle, collider.handle, (overlap, flipped) => { | |
const contacts = overlap.numContacts(); | |
if (contacts === 0) { | |
// strange | |
return; | |
} | |
const contactResults: {direction: Vector3; distance: number}[] = []; | |
const resultAvg = new Vector3(0, 0, 0); | |
for (let contactIndex = 0; contactIndex < contacts; contactIndex++) { | |
let contact1 = Vec3.from( | |
flipped ? overlap.localContactPoint2(contactIndex)! : overlap.localContactPoint1(contactIndex)! | |
); | |
if (contact1.magnitude < 0.0001) { | |
// idk why but sometimes it gives you a basically zero contact so just ignore it, typically index 1 of 3 | |
continue; | |
} | |
let worldPointOfCollision1 = Quaternion.from(myRotation).vmult(contact1).vadd(transformPosition); | |
let contact2 = Vec3.from( | |
flipped ? overlap.localContactPoint1(contactIndex)! : overlap.localContactPoint2(contactIndex)! | |
); | |
let worldPointOfCollision2 = Quaternion.from(colliderRotation) | |
.vmult(contact2) | |
.vadd(Vec3.from(colliderTranslation)); | |
let fullDirection = worldPointOfCollision1.vsub(worldPointOfCollision2); | |
if (fullDirection.length() === 0) { | |
// this fixes normalizeVector returning nan | |
continue; | |
} | |
const direction = fullDirection.normalizeVector(); | |
contactResults.push({distance: overlap.contactDist(contactIndex), direction}); | |
} | |
if (contactResults.length === 0) return; | |
for (const contactResult of contactResults) { | |
const distPush = contactResult.distance + epsilon; | |
const push = Vec3.from(contactResult.direction).normalizeVector().scale(distPush); | |
resultAvg.x += push.x; | |
resultAvg.y += push.y; | |
resultAvg.z += push.z; | |
} | |
result.x += resultAvg.x / contactResults.length; | |
result.y += resultAvg.y / contactResults.length; | |
result.z += resultAvg.z / contactResults.length; | |
}) | |
const epsilon=0.001 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment