mirror of
https://github.com/RandomityGuy/MBHaxe.git
synced 2025-12-25 17:32:35 +00:00
Add bounds collision handlers
This commit is contained in:
parent
c2811867ff
commit
f69a899c8a
9 changed files with 121 additions and 7 deletions
|
|
@ -81,7 +81,7 @@ class DifBuilder {
|
|||
var triangles = [];
|
||||
var textures = [];
|
||||
|
||||
var collider = new CollisionEntity();
|
||||
var collider = new CollisionEntity(itr);
|
||||
|
||||
function stripTexName(tex:String) {
|
||||
var dotpos = tex.lastIndexOf(".");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package src;
|
||||
|
||||
import collision.BoxCollisionEntity;
|
||||
import shaders.DtsTexture;
|
||||
import h3d.shader.AlphaMult;
|
||||
import sys.io.File;
|
||||
|
|
@ -94,6 +95,7 @@ class DtsObject extends GameObject {
|
|||
|
||||
var rootObject:Object;
|
||||
var colliders:Array<CollisionEntity>;
|
||||
var boundingCollider:BoxCollisionEntity;
|
||||
|
||||
var mountPointNodes:Array<Int>;
|
||||
var alphaShader:AlphaMult;
|
||||
|
|
@ -197,7 +199,7 @@ class DtsObject extends GameObject {
|
|||
for (i in 0...dts.nodes.length) {
|
||||
var objects = dts.objects.filter(object -> object.node == i);
|
||||
var meshSurfaces = [];
|
||||
var collider = new CollisionHull();
|
||||
var collider = new CollisionHull(cast this);
|
||||
|
||||
for (object in objects) {
|
||||
var isCollisionObject = dts.names[object.name].substr(0, 3).toLowerCase() == "col";
|
||||
|
|
@ -297,6 +299,8 @@ class DtsObject extends GameObject {
|
|||
}
|
||||
|
||||
rootObject.scaleX = -1;
|
||||
|
||||
this.boundingCollider = new BoxCollisionEntity(this.getBounds().clone(), cast this);
|
||||
}
|
||||
|
||||
function computeMaterials() {
|
||||
|
|
@ -533,6 +537,11 @@ class DtsObject extends GameObject {
|
|||
return geo;
|
||||
}
|
||||
|
||||
public override function setTransform(mat:Matrix) {
|
||||
super.setTransform(mat);
|
||||
this.boundingCollider.setTransform(mat);
|
||||
}
|
||||
|
||||
public function update(currentTime:Float, dt:Float) {
|
||||
for (sequence in this.dts.sequences) {
|
||||
if (!this.showSequences)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,17 @@
|
|||
package src;
|
||||
|
||||
import collision.CollisionInfo;
|
||||
import h3d.scene.Object;
|
||||
|
||||
class GameObject extends Object {
|
||||
public var identifier:String;
|
||||
public var currentOpacity:Float = 1;
|
||||
|
||||
public function onMarbleContact(time:Float, ?contact:CollisionInfo) {}
|
||||
|
||||
public function onMarbleInside(time:Float) {}
|
||||
|
||||
public function onMarbleEnter(time:Float) {}
|
||||
|
||||
public function onMarbleLeave(time:Float) {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package src;
|
||||
|
||||
import src.GameObject;
|
||||
import src.ForceObject;
|
||||
import src.MarbleWorld;
|
||||
import h3d.Quat;
|
||||
|
|
@ -31,7 +32,7 @@ class Move {
|
|||
public function new() {}
|
||||
}
|
||||
|
||||
class Marble extends Object {
|
||||
class Marble extends GameObject {
|
||||
public var camera:CameraController;
|
||||
public var cameraObject:Object;
|
||||
public var controllable:Bool = false;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package src;
|
||||
|
||||
import collision.SphereCollisionEntity;
|
||||
import src.Sky;
|
||||
import h3d.scene.Mesh;
|
||||
import src.InstanceManager;
|
||||
|
|
@ -23,11 +24,16 @@ class MarbleWorld {
|
|||
public var marbles:Array<Marble> = [];
|
||||
public var dtsObjects:Array<DtsObject> = [];
|
||||
|
||||
var shapeImmunity:Array<DtsObject> = [];
|
||||
var shapeOrTriggerInside:Array<DtsObject> = [];
|
||||
|
||||
public var currentTime:Float = 0;
|
||||
public var sky:Sky;
|
||||
|
||||
public var scene:Scene;
|
||||
|
||||
var marble:Marble;
|
||||
|
||||
public function new(scene:Scene) {
|
||||
this.collisionWorld = new CollisionWorld();
|
||||
this.scene = scene;
|
||||
|
|
@ -69,6 +75,7 @@ class MarbleWorld {
|
|||
if (collider != null)
|
||||
this.collisionWorld.addEntity(collider);
|
||||
}
|
||||
this.collisionWorld.addEntity(obj.boundingCollider);
|
||||
}
|
||||
|
||||
public function addMarble(marble:Marble) {
|
||||
|
|
@ -77,6 +84,7 @@ class MarbleWorld {
|
|||
if (marble.controllable) {
|
||||
marble.camera.init(cast this);
|
||||
this.scene.addChild(marble.camera);
|
||||
this.marble = marble;
|
||||
// Ugly hack
|
||||
sky.follow = marble;
|
||||
}
|
||||
|
|
@ -93,5 +101,54 @@ class MarbleWorld {
|
|||
}
|
||||
this.instanceManager.update(dt);
|
||||
currentTime += dt;
|
||||
if (this.marble != null) {
|
||||
callCollisionHandlers(marble);
|
||||
}
|
||||
}
|
||||
|
||||
function callCollisionHandlers(marble:Marble) {
|
||||
var contacts = this.collisionWorld.radiusSearch(marble.getAbsPos().getPosition(), marble._radius);
|
||||
var newImmunity = [];
|
||||
var calledShapes = [];
|
||||
var inside = [];
|
||||
|
||||
var contactsphere = new SphereCollisionEntity(marble);
|
||||
contactsphere.velocity = new Vector();
|
||||
|
||||
for (contact in contacts) {
|
||||
if (contact.go != marble) {
|
||||
if (contact.go is DtsObject) {
|
||||
var shape:DtsObject = cast contact.go;
|
||||
|
||||
var contacttest = shape.colliders.map(x -> x.sphereIntersection(contactsphere, 0));
|
||||
var contactlist:Array<collision.CollisionInfo> = [];
|
||||
for (l in contacttest) {
|
||||
contactlist = contactlist.concat(l);
|
||||
}
|
||||
|
||||
if (!calledShapes.contains(shape) && !this.shapeImmunity.contains(shape) && contactlist.length != 0) {
|
||||
calledShapes.push(shape);
|
||||
newImmunity.push(shape);
|
||||
shape.onMarbleContact(currentTime);
|
||||
}
|
||||
|
||||
shape.onMarbleInside(currentTime);
|
||||
if (!this.shapeOrTriggerInside.contains(shape)) {
|
||||
this.shapeOrTriggerInside.push(shape);
|
||||
shape.onMarbleEnter(currentTime);
|
||||
}
|
||||
inside.push(shape);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (object in shapeOrTriggerInside) {
|
||||
if (!inside.contains(object)) {
|
||||
this.shapeOrTriggerInside.remove(object);
|
||||
object.onMarbleLeave(currentTime);
|
||||
}
|
||||
}
|
||||
|
||||
this.shapeImmunity = newImmunity;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
33
src/collision/BoxCollisionEntity.hx
Normal file
33
src/collision/BoxCollisionEntity.hx
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package collision;
|
||||
|
||||
import h3d.Matrix;
|
||||
import src.GameObject;
|
||||
import src.Marble;
|
||||
import h3d.col.Ray;
|
||||
import h3d.Vector;
|
||||
import h3d.col.Sphere;
|
||||
import h3d.col.Bounds;
|
||||
|
||||
class BoxCollisionEntity extends CollisionEntity {
|
||||
var bounds:Bounds;
|
||||
|
||||
public function new(bounds:Bounds, go:GameObject) {
|
||||
super(go);
|
||||
this.bounds = bounds;
|
||||
this.generateBoundingBox();
|
||||
}
|
||||
|
||||
public override function generateBoundingBox() {
|
||||
this.boundingBox = bounds.clone();
|
||||
this.boundingBox.transform(this.transform);
|
||||
}
|
||||
|
||||
public override function isIntersectedByRay(rayOrigin:Vector, rayDirection:Vector, intersectionPoint:Vector):Bool {
|
||||
// TEMP cause bruh
|
||||
return boundingBox.rayIntersection(Ray.fromValues(rayOrigin.x, rayOrigin.y, rayOrigin.z, rayDirection.x, rayDirection.y, rayDirection.z), true) != -1;
|
||||
}
|
||||
|
||||
public override function sphereIntersection(collisionEntity:SphereCollisionEntity, dt:Float) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package collision;
|
||||
|
||||
import src.GameObject;
|
||||
import dif.math.Point3F;
|
||||
import dif.math.PlaneF;
|
||||
import h3d.col.Plane;
|
||||
|
|
@ -22,8 +23,10 @@ class CollisionEntity implements IOctreeObject {
|
|||
public var velocity:Vector = new Vector();
|
||||
|
||||
public var transform:Matrix;
|
||||
public var go:GameObject;
|
||||
|
||||
public function new() {
|
||||
public function new(go:GameObject) {
|
||||
this.go = go;
|
||||
this.octree = new Octree();
|
||||
this.surfaces = [];
|
||||
this.transform = Matrix.I();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package collision;
|
||||
|
||||
import src.GameObject;
|
||||
import h3d.col.Bounds;
|
||||
import collision.gjk.GJK;
|
||||
import collision.gjk.ConvexHull;
|
||||
|
|
@ -11,8 +12,8 @@ class CollisionHull extends CollisionEntity {
|
|||
var restitution = 1.0;
|
||||
var force = 0.0;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
public function new(go:GameObject) {
|
||||
super(go);
|
||||
}
|
||||
|
||||
public override function sphereIntersection(collisionEntity:SphereCollisionEntity, dt:Float):Array<CollisionInfo> {
|
||||
|
|
@ -45,6 +46,7 @@ class CollisionHull extends CollisionEntity {
|
|||
cinfo.restitution = restitution;
|
||||
cinfo.friction = friction;
|
||||
cinfo.force = force;
|
||||
this.go.onMarbleContact(dt, cinfo);
|
||||
return [cinfo];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ class SphereCollisionEntity extends CollisionEntity {
|
|||
public var marble:Marble;
|
||||
|
||||
public function new(marble:Marble) {
|
||||
super();
|
||||
super(cast marble);
|
||||
this.marble = marble;
|
||||
this.velocity = marble.velocity;
|
||||
this.radius = marble._radius;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue