add hit normal to ray result (#6258)

* add hit normal to ray result

* tweaks
This commit is contained in:
jiaxin.lai
2020-03-11 15:59:33 +08:00
committed by GitHub
parent 50d6264086
commit 905716cecd
4 changed files with 34 additions and 16 deletions
+12 -7
View File
@@ -16,6 +16,8 @@ import { AmmoCollisionFilterGroups } from './ammo-enum';
const contactsPool = [] as any;
const v3_0 = new Vec3();
const v3_1 = new Vec3();
export class AmmoWorld implements IPhysicsWorld {
set allowSleep (v: boolean) { };
@@ -133,7 +135,11 @@ export class AmmoWorld implements IPhysicsWorld {
this.allHitsCB.m_shapeParts.clear();
this.allHitsCB.m_hitFractions.clear();
this.allHitsCB.m_collisionObjects.clear();
// TODO: typing
const hp = (this.allHitsCB.m_hitPointWorld as any);
const hn = (this.allHitsCB.m_hitNormalWorld as any);
hp.clear();
hn.clear();
this._world.rayTest(from, to, this.allHitsCB);
if (this.allHitsCB.hasHit()) {
for (let i = 0, n = this.allHitsCB.m_collisionObjects.size(); i < n; i++) {
@@ -143,13 +149,11 @@ export class AmmoWorld implements IPhysicsWorld {
const shared = AmmoInstance.bodyAndGhosts['KEY' + index];
// if (shared.wrappedShapes.length > shapeIndex) {
const shape = shared.wrappedShapes[shapeIndex];
const hitFraction = this.allHitsCB.m_hitFractions.at(i);
v3_0.x = from.x() + hitFraction * (to.x() - from.x());
v3_0.y = from.y() + hitFraction * (to.y() - from.y());
v3_0.z = from.z() + hitFraction * (to.z() - from.z());
ammo2CocosVec3(v3_0, hp.at(i));
ammo2CocosVec3(v3_1, hn.at(i));
const distance = Vec3.distance(worldRay.o, v3_0);
const r = pool.add();
r._assign(v3_0, distance, shape.collider);
r._assign(v3_0, distance, shape.collider, v3_1);
results.push(r);
// }
}
@@ -181,8 +185,9 @@ export class AmmoWorld implements IPhysicsWorld {
// if (shared.wrappedShapes.length > shapeIndex) {
const shape = shared.wrappedShapes[shapeIndex];
ammo2CocosVec3(v3_0, this.closeHitCB.m_hitPointWorld);
ammo2CocosVec3(v3_1, this.closeHitCB.m_hitNormalWorld);
const distance = Vec3.distance(worldRay.o, v3_0);
result._assign(v3_0, distance, shape.collider);
result._assign(v3_0, distance, shape.collider, v3_1);
return true;
// }
}
+3 -2
View File
@@ -14,9 +14,10 @@ export function toCannonRaycastOptions (out: CANNON.IRaycastOptions, options: IR
export function fillRaycastResult (result: PhysicsRayResult, cannonResult: CANNON.RaycastResult) {
result._assign(
Vec3.copy(new Vec3(), cannonResult.hitPointWorld),
cannonResult.hitPointWorld,
cannonResult.distance,
getWrap<IBaseShape>(cannonResult.shape).collider
getWrap<IBaseShape>(cannonResult.shape).collider,
cannonResult.hitNormalWorld
);
}
+2 -2
View File
@@ -94,7 +94,7 @@ export class BuiltInWorld implements IPhysicsWorld {
tmp_d = distance;
Vec3.normalize(hitPoint, worldRay.d)
Vec3.scaleAndAdd(hitPoint, worldRay.o, hitPoint, distance);
out._assign(hitPoint, distance, shape.collider);
out._assign(hitPoint, distance, shape.collider, Vec3.ZERO);
}
}
}
@@ -118,7 +118,7 @@ export class BuiltInWorld implements IPhysicsWorld {
} else {
const r = pool.add();
worldRay.computeHit(hitPoint, distance);
r._assign(hitPoint, distance, shape.collider);
r._assign(hitPoint, distance, shape.collider, Vec3.ZERO);
results.push(r);
}
}
+17 -5
View File
@@ -4,6 +4,7 @@
import { Vec3 } from '../../core/math';
import { ColliderComponent } from '../../../exports/physics-framework';
import { IVec3Like } from '../../core/math/type-define';
/**
* @zh
@@ -32,21 +33,31 @@ export class PhysicsRayResult {
* 击中的碰撞盒
*/
get collider (): ColliderComponent {
return this._collidier!;
return this._collider!;
}
/**
* @zh
* 击中面的法线
*/
get hitNormal (): Vec3 {
return this._hitNormal;
}
private _hitPoint: Vec3 = new Vec3();
private _hitNormal: Vec3 = new Vec3();
private _distance: number = 0;
private _collidier: ColliderComponent | null = null;
private _collider: ColliderComponent | null = null;
/**
* @zh
* 设置射线,此方法由引擎内部使用,请勿在外部脚本调用
*/
public _assign (hitPoint: Vec3, distance: number, collider: ColliderComponent) {
public _assign (hitPoint: IVec3Like, distance: number, collider: ColliderComponent, hitNormal: IVec3Like) {
Vec3.copy(this._hitPoint, hitPoint);
Vec3.copy(this._hitNormal, hitNormal);
this._distance = distance;
this._collidier = collider;
this._collider = collider;
}
/**
@@ -56,8 +67,9 @@ export class PhysicsRayResult {
public clone () {
const c = new PhysicsRayResult();
Vec3.copy(c._hitPoint, this._hitPoint);
Vec3.copy(c._hitNormal, this._hitNormal);
c._distance = this._distance;
c._collidier = this._collidier;
c._collider = this._collider;
return c;
}
}