Permalink
Browse files

add hit test obb and obb

  • Loading branch information...
hikipuro committed Sep 14, 2018
1 parent 5368861 commit 5795e13984b74b45c2221348cdf8499b3e232dec
Showing with 202 additions and 14 deletions.
  1. +21 −7 src/Main.ts
  2. +14 −2 src/TestScript.ts
  3. +160 −0 src/tea/components/BoxCollider.ts
  4. +4 −2 src/tea/math/Vector3.ts
  5. +3 −3 src/tea/objects/Object3D.ts
View
@@ -79,18 +79,24 @@ export class Main {
var t = 2;
var loop = 100;
var count = 200;
var test2 = Tea.benchmark(() => {
/*
var m1 = new Tea.Matrix4x4();
var m2 = Tea.Matrix4x4.identity;
var test1 = Tea.benchmark(() => {
for (var i = 0; i < loop; i++) {
t = Math.pow(t, 2);
m1.copy2(m2);
}
}, count);
var test1 = Tea.benchmark(() => {
m1 = new Tea.Matrix4x4();
m2 = Tea.Matrix4x4.identity;
var test2 = Tea.benchmark(() => {
for (var i = 0; i < loop; i++) {
t = t ** 2;
m1.copy(m2);
}
}, count);
console.log("test1", test1);
console.log("test2", test2);
*/
}, 1000);
//*/
@@ -168,7 +174,6 @@ export class Main {
//var script = new TestScript();
/*
var cube = Tea.Object3D.createPrimitive(this.app, Tea.PrimitiveType.Cube);
//cube.name = "cube";
cube.position = Tea.vec3(0, 2, 0);
@@ -179,8 +184,11 @@ export class Main {
//cube.rotation.y = Tea.radians(20);
//cube.addScript(new Rotate());
//cube.getComponent(Tea.Renderer).material.mainTexture = renderTexture;
var box2 = cube.addComponent(Tea.BoxCollider);
box2.size = cube.getComponent(Tea.MeshFilter).mesh.bounds.size.clone();
scene.appendChild(cube);
/*
var cube2 = Tea.Object3D.createPrimitive(this.app, Tea.PrimitiveType.Cube);
cube2.position.x = 2;
cube2.position.y = 2;
@@ -286,7 +294,7 @@ export class Main {
//capsule.position.y = 2;
//capsule.position.z = 2;
//capsule.scale.x = 2;
//capsule.rotation = Tea.Quaternion.euler(-45, 0, 0);
capsule.rotation = Tea.Quaternion.euler(0, 0, 45);
/*
console.log("extension", this.app.isExtensionSupported(Tea.GLExtensions.OES_standard_derivatives));
this.app.getExtension(Tea.GLExtensions.OES_standard_derivatives);
@@ -297,14 +305,18 @@ export class Main {
);
capsule.getComponent(Tea.Renderer).material.shader = s;
*/
/*
var cs = capsule.getComponent(Tea.Renderer).material.shader;
cs.settings.enableStencilTest = false;
cs.settings.stencil.func = Tea.ShaderTestFunc.Equal;
cs.settings.stencil.ref = 1;
cs.settings.stencil.fail = Tea.ShaderStencilOp.Keep;
cs.settings.stencil.zfail = Tea.ShaderStencilOp.Keep;
cs.settings.stencil.zpass = Tea.ShaderStencilOp.Keep;
//capsule.addComponent(TestScript);
*/
var box1 = capsule.addComponent(Tea.BoxCollider);
box1.size = capsule.getComponent(Tea.MeshFilter).mesh.bounds.size.clone();
capsule.addComponent(TestScript).cube = cube;
scene.appendChild(capsule);
//console.log("capsule", capsule.localToWorldMatrix.toString());
@@ -385,13 +397,15 @@ export class Main {
}
];
var objIndex = 2;
/*
this.app.readObjFile(objList[objIndex].path, (object3d) => {
var scale = objList[objIndex].scale;
object3d.localScale = new Tea.Vector3(scale, scale, scale);
object3d.localPosition.z = -4;
object3d.addComponent(Rotate);
scene.appendChild(object3d);
});
//*/
Tea.File.readImage("../models/texture.png", (err, image) => {
var texture = this.app.createTexture(image);
View
@@ -3,6 +3,7 @@ import * as Tea from "./tea/Tea";
export class TestScript extends Tea.Script {
count: number = 0;
speedY: number = 0;
cube: Tea.Object3D;
start(): void {
console.log("TestScript.start");
@@ -16,7 +17,6 @@ export class TestScript extends Tea.Script {
//this.position.z = Math.sin(Tea.radians(this.count)) * 5;
//console.log(this.count, this.position);
//this.rotation.x = Tea.radians(this.count / 3);
this.rotate(0., 3., 0.);
var r = this.object3d.getComponent(Tea.Renderer);
var i = Math.sin(this.count / 40);
@@ -43,7 +43,7 @@ export class TestScript extends Tea.Script {
//this.position.x = -0.5 + mouse.x / this.app.width;
//this.position.y = 0.5 + mouse.y / this.app.height;
var p = mouse.position;
p.z = 3;
p.z = 10;
p = this.object3d.scene.mainCamera.screenToWorldPoint(p);
this.localPosition = p;
//console.log(p.toString());
@@ -59,6 +59,18 @@ export class TestScript extends Tea.Script {
//console.log("mouse up");
}
var box1 = this.object3d.getComponent(Tea.BoxCollider);
var box2 = this.cube.getComponent(Tea.BoxCollider);
var r = this.cube.getComponent(Tea.Renderer);
//console.log(box1.toString());
if (box1.testBoxCollider(box2)) {
//console.log("true");
r.material.color = Tea.Color.red;
} else {
//console.log("false");
r.material.color = Tea.Color.white;
}
//console.log(this.app.keyboard._keys);
}
}
@@ -21,6 +21,14 @@ export class BoxCollider extends Collider {
return false;
}
toString(): string {
return Tea.StringUtil.format(
"{ center: {0}, size: {1} }",
this.center.toString(),
this.size.toString()
);
}
testRay(ray: Tea.Ray): boolean {
var r = this.object3d.rotation.inversed;
var p = ray.origin.clone();
@@ -68,4 +76,156 @@ export class BoxCollider extends Collider {
*/
return true;
}
testBoxCollider(box: BoxCollider): boolean {
var ao = this.object3d;
var bo = box.object3d;
var ae = this.size.mul(0.5);
var be = box.size.mul(0.5);
var ax = ao.right, axl = ax.mul(ae.x);
var ay = ao.up, ayl = ay.mul(ae.y);
var az = ao.forward, azl = az.mul(ae.z);
var bx = bo.right, bxl = bx.mul(be.x);
var by = bo.up, byl = by.mul(be.y);
var bz = bo.forward, bzl = bz.mul(be.z);
var distance = ao.position.sub(bo.position);
// separating axis: ax
var a = axl.magnitude;
var b = this.lenSegOnSeparateAxis(ax, bxl, byl, bzl);
var d = Math.abs(distance.dot(ax));
if (d > a + b) {
return false;
}
// separating axis: ay
a = ayl.magnitude;
b = this.lenSegOnSeparateAxis(ay, bxl, byl, bzl);
d = Math.abs(distance.dot(ay));
if (d > a + b) {
return false;
}
// separating axis: az
a = azl.magnitude;
b = this.lenSegOnSeparateAxis(az, bxl, byl, bzl);
d = Math.abs(distance.dot(az));
if (d > a + b) {
return false;
}
// separating axis: bx
a = bxl.magnitude;
b = this.lenSegOnSeparateAxis(bx, axl, ayl, azl);
d = Math.abs(distance.dot(bx));
if (d > a + b) {
return false;
}
// separating axis: by
a = byl.magnitude;
b = this.lenSegOnSeparateAxis(by, axl, ayl, azl);
d = Math.abs(distance.dot(by));
if (d > a + b) {
return false;
}
// separating axis: bz
a = bzl.magnitude;
b = this.lenSegOnSeparateAxis(bz, axl, ayl, azl);
d = Math.abs(distance.dot(bz));
if (d > a + b) {
return false;
}
// separating axis: ax, bx
var cross = ax.cross(bx);
a = this.lenSegOnSeparateAxis(cross, ayl, azl);
b = this.lenSegOnSeparateAxis(cross, byl, bzl);
d = Math.abs(distance.dot(cross));
if (d > a + b) {
return false;
}
// separating axis: ax, by
cross = ax.cross(by);
a = this.lenSegOnSeparateAxis(cross, ayl, azl);
b = this.lenSegOnSeparateAxis(cross, bxl, bzl);
d = Math.abs(distance.dot(cross));
if (d > a + b) {
return false;
}
// separating axis: ax, bz
cross = ax.cross(bz);
a = this.lenSegOnSeparateAxis(cross, ayl, azl);
b = this.lenSegOnSeparateAxis(cross, bxl, byl);
d = Math.abs(distance.dot(cross));
if (d > a + b) {
return false;
}
// separating axis: ay, bx
cross = ay.cross(bx);
a = this.lenSegOnSeparateAxis(cross, axl, azl);
b = this.lenSegOnSeparateAxis(cross, byl, bzl);
d = Math.abs(distance.dot(cross));
if (d > a + b) {
return false;
}
// separating axis: ay, by
cross = ay.cross(by);
a = this.lenSegOnSeparateAxis(cross, axl, azl);
b = this.lenSegOnSeparateAxis(cross, bxl, bzl);
d = Math.abs(distance.dot(cross));
if (d > a + b) {
return false;
}
// separating axis: ay, bz
cross = ay.cross(bz);
a = this.lenSegOnSeparateAxis(cross, axl, azl);
b = this.lenSegOnSeparateAxis(cross, bxl, byl);
d = Math.abs(distance.dot(cross));
if (d > a + b) {
return false;
}
// separating axis: az, bx
cross = az.cross(bx);
a = this.lenSegOnSeparateAxis(cross, axl, ayl);
b = this.lenSegOnSeparateAxis(cross, byl, bzl);
d = Math.abs(distance.dot(cross));
if (d > a + b) {
return false;
}
// separating axis: az, by
cross = az.cross(by);
a = this.lenSegOnSeparateAxis(cross, axl, ayl);
b = this.lenSegOnSeparateAxis(cross, bxl, bzl);
d = Math.abs(distance.dot(cross));
if (d > a + b) {
return false;
}
// separating axis: az, bz
cross = az.cross(bz);
a = this.lenSegOnSeparateAxis(cross, axl, ayl);
b = this.lenSegOnSeparateAxis(cross, bxl, byl);
d = Math.abs(distance.dot(cross));
if (d > a + b) {
return false;
}
return true;
}
protected lenSegOnSeparateAxis(sep: Tea.Vector3, e1: Tea.Vector3, e2: Tea.Vector3, e3?: Tea.Vector3): number {
var r1 = Math.abs(sep.dot(e1));
var r2 = Math.abs(sep.dot(e2));
var r3 = e3 != null ? Math.abs(sep.dot(e3)) : 0.0;
return r1 + r2 + r3;
}
}
View
@@ -174,16 +174,18 @@ export class Vector3 extends Array<number> {
);
}
set(x: number, y: number, z: number): void {
set(x: number, y: number, z: number): Tea.Vector3 {
this[0] = x;
this[1] = y;
this[2] = z;
return this;
}
copy(value: Vector3): void {
copy(value: Vector3): Tea.Vector3 {
this[0] = value[0];
this[1] = value[1];
this[2] = value[2];
return this;
}
equals(value: Vector3): boolean {
@@ -487,9 +487,9 @@ export class Object3D {
}
protected reverseScale$(scale: Tea.Vector3): Tea.Vector3 {
scale[0] = scale[0] !== 0 ? 1 / scale[0] : 0;
scale[1] = scale[1] !== 0 ? 1 / scale[1] : 0;
scale[2] = scale[2] !== 0 ? 1 / scale[2] : 0;
scale[0] = scale[0] !== 0.0 ? 1.0 / scale[0] : 0.0;
scale[1] = scale[1] !== 0.0 ? 1.0 / scale[1] : 0.0;
scale[2] = scale[2] !== 0.0 ? 1.0 / scale[2] : 0.0;
return scale;
}

0 comments on commit 5795e13

Please sign in to comment.