diff --git a/assets/Data/creatures/human.json b/assets/Data/creatures/human.json index 034cadc7..7d648dab 100644 --- a/assets/Data/creatures/human.json +++ b/assets/Data/creatures/human.json @@ -239,7 +239,7 @@ "maxHealth" : 100, "onDamageIFrames" : 30 }, - "modelPath" : "Models/baseman.fbx" + "modelPath" : "Models/baseman2.fbx" } ], "files" : [] diff --git a/assets/Models/baseman2.fbx b/assets/Models/baseman2.fbx new file mode 100644 index 00000000..9c6b38ba Binary files /dev/null and b/assets/Models/baseman2.fbx differ diff --git a/src/main/java/electrosphere/engine/LoadingThread.java b/src/main/java/electrosphere/engine/LoadingThread.java index 32c42342..e0700553 100644 --- a/src/main/java/electrosphere/engine/LoadingThread.java +++ b/src/main/java/electrosphere/engine/LoadingThread.java @@ -10,6 +10,7 @@ import electrosphere.entity.state.collidable.Impulse; import electrosphere.entity.state.movement.ApplyRotationTree; import electrosphere.game.collision.CollisionEngine; import electrosphere.entity.types.creature.CreatureUtils; +import electrosphere.entity.types.debug.DebugVisualizerUtils; import electrosphere.entity.types.item.ItemUtils; import electrosphere.game.client.cells.DrawCellManager; import electrosphere.game.client.player.ClientPlayerData; @@ -680,7 +681,7 @@ public class LoadingThread extends Thread { //sword Entity sword = ItemUtils.spawnBasicItem("Katana"); EntityUtils.getPosition(sword).set(new Vector3f(1,0.4f,2)); - EntityUtils.getRotation(sword).set(new Quaternionf().rotationY(0.6f)); + EntityUtils.getRotation(sword).set(new Quaternionf().rotationY((float)(Math.PI/2.0))); // Globals.entityManager.registerBehaviorTree(new BehaviorTree() { // int i = 0; // public void simulate(){ @@ -691,6 +692,8 @@ public class LoadingThread extends Thread { // } // }}); + // DebugVisualizerUtils.spawnVectorVisualizer(new Vector3d(0,0,0), new Vector3d(-1,-1,-1)); + // Entity shorts = ItemUtils.spawnBasicItem("boots1"); // EntityUtils.getPosition(shorts).set(new Vector3f(2,0.1f,2)); // // Entity hair = ItemUtils.spawnBasicItem("hairshort1"); diff --git a/src/main/java/electrosphere/entity/state/collidable/CollidableTree.java b/src/main/java/electrosphere/entity/state/collidable/CollidableTree.java index d2fb5b31..ed7335d2 100644 --- a/src/main/java/electrosphere/entity/state/collidable/CollidableTree.java +++ b/src/main/java/electrosphere/entity/state/collidable/CollidableTree.java @@ -7,6 +7,7 @@ import electrosphere.entity.EntityUtils; import electrosphere.entity.state.gravity.GravityTree; import electrosphere.entity.types.collision.CollisionObjUtils; import electrosphere.entity.types.creature.CreatureUtils; +import electrosphere.entity.types.debug.DebugVisualizerUtils; import electrosphere.entity.types.item.ItemUtils; import electrosphere.game.collision.PhysicsUtils; import electrosphere.game.collision.collidable.Collidable; @@ -14,6 +15,7 @@ import electrosphere.game.data.creature.type.CollidableTemplate; import electrosphere.main.Globals; import org.joml.Matrix4f; +import org.joml.Quaterniond; import org.joml.Quaternionf; import org.joml.Vector3d; import org.joml.Vector3f; @@ -40,6 +42,7 @@ public class CollidableTree { this.body = body; } + static int incrementer = 0; public void simulate(){ Vector3d position = EntityUtils.getPosition(parent); @@ -80,13 +83,16 @@ public class CollidableTree { !Double.isNaN(impulse.getDirection().z) && ItemUtils.isItem(parent) ){ - Vector3d collisionPoint = new Vector3d(impulse.getCollisionPoint()).normalize(); - Vector3d forceDir = new Vector3d(impulse.getDirection().mul(-1.0f)).normalize(); + // Vector3d collisionPoint = new Vector3d(impulse.getCollisionPoint()).normalize(); + Vector3d collisionPoint = new Vector3d(impulse.getWorldPoint()).sub(position); + Vector3d forceDir = new Vector3d(impulse.getDirection()).normalize(); Vector3d torqueVec = new Vector3d(collisionPoint).cross(forceDir).normalize(); // double torqueMag = Math.abs(impulse.force); double torqueMag = Math.sqrt(impulse.getCollisionPoint().length()) * impulse.getForce(); if(!impulse.getType().equals(Collidable.TYPE_TERRAIN)){ torqueMag = torqueMag * 10; + } else { + torqueMag = 0; } // if(impulse.type.matches(Collidable.TYPE_ITEM) && ItemUtils.isItem(parent)){ // // System.out.println(rotation); @@ -98,8 +104,18 @@ public class CollidableTree { // // angularVelocity.add(new Vector3d(impulse.getCollisionPoint()).cross(impulse.getDirection()),1.0); // cumulativeTorque.add(new Vector3d(impulse.getCollisionPoint()).cross(impulse.getDirection())); // } - if(impulse.type.matches(Collidable.TYPE_ITEM)){ - System.out.println("Impulse: " + torqueVec + " " + torqueMag); + if(impulse.type.matches(Collidable.TYPE_CREATURE)){ + System.out.println("Impulse: " + impulse.getCollisionPoint() + " x " + impulse.getDirection() + " ->f " + impulse.force); + // incrementer++; + // if(incrementer > 5){ + // Globals.microSimulation.freeze(); + // } else if(incrementer <= 5){ + // // Globals.controlHandler.showMouse(); + // Vector3d pos = impulse.getWorldPoint(); + // // pos = new Vector3d(position).add(impulse.getCollisionPoint()).mul(1,0,1); + // DebugVisualizerUtils.spawnVectorVisualizer(pos, new Vector3d(torqueVec)); + // } + // System.out.println("Impulse: " + torqueVec + " " + torqueMag); } // if(CreatureUtils.isCreature(parent) && forceDir.y < 0.5){ // System.out.println(collisionPoint + " x " + forceDir + " => " + torqueVec + " " + torqueMag); diff --git a/src/main/java/electrosphere/entity/state/collidable/Impulse.java b/src/main/java/electrosphere/entity/state/collidable/Impulse.java index 4adae026..f98faea9 100644 --- a/src/main/java/electrosphere/entity/state/collidable/Impulse.java +++ b/src/main/java/electrosphere/entity/state/collidable/Impulse.java @@ -11,14 +11,16 @@ public class Impulse { Vector3d direction; Vector3d collisionPoint; + Vector3d worldPoint; double force; String type; - public Impulse(Vector3d dir, Vector3d collisionPoint, double force, String type){ + public Impulse(Vector3d forceDir, Vector3d collisionPoint, Vector3d worldPoint, double force, String type){ this.force = force; - this.direction = dir; + this.direction = forceDir; this.collisionPoint = collisionPoint; this.type = type; + this.worldPoint = worldPoint; } public Vector3d getDirection() { @@ -36,6 +38,10 @@ public class Impulse { public Vector3d getCollisionPoint() { return collisionPoint; } + + public Vector3d getWorldPoint(){ + return worldPoint; + } } diff --git a/src/main/java/electrosphere/entity/state/gravity/GravityTree.java b/src/main/java/electrosphere/entity/state/gravity/GravityTree.java index bda6aa6b..b5f63514 100644 --- a/src/main/java/electrosphere/entity/state/gravity/GravityTree.java +++ b/src/main/java/electrosphere/entity/state/gravity/GravityTree.java @@ -130,7 +130,7 @@ public class GravityTree { if(hitFraction < 0){ hitFraction = 1; } - collidable.addImpulse(new Impulse(new Vector3d(0,-1,0), new Vector3d(0,0,0), gravityDif * hitFraction,"gravity")); + collidable.addImpulse(new Impulse(new Vector3d(0,-1,0), new Vector3d(0,0,0), new Vector3d(0,0,0), gravityDif * hitFraction,"gravity")); // System.out.println(hitFraction); // bodyTransformMatrix = new javax.vecmath.Matrix4f(PhysicsUtils.jomlToVecmathQuaternionf(rotation),PhysicsUtils.jomlToVecmathVector3f(new Vector3f((float)position.x,(float)position.y,(float)position.z)),1.0f); // body.setWorldTransform(new electrosphere.linearmath.Transform(bodyTransformMatrix)); diff --git a/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java b/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java index 7597e149..95e17e93 100644 --- a/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java +++ b/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java @@ -243,7 +243,7 @@ public class GroundMovementTree { // newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition); // } // //actually update - collidable.addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), velocity * Main.deltaTime, "movement")); + collidable.addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), new Vector3d(0,0,0), velocity * Main.deltaTime, "movement")); // position.set(newPosition); rotation.set(movementQuaternion); @@ -319,7 +319,7 @@ public class GroundMovementTree { // if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){ // newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition); // } - collidable.addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), velocity * Main.deltaTime, "movement")); + collidable.addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), new Vector3d(0,0,0), velocity * Main.deltaTime, "movement")); // position.set(newPosition); rotation.set(movementQuaternion); @@ -403,7 +403,7 @@ public class GroundMovementTree { // if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){ // newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition); // } - collidable.addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), velocity * Main.deltaTime, "movement")); + collidable.addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), new Vector3d(0,0,0), velocity * Main.deltaTime, "movement")); // position.set(newPosition); rotation.set(movementQuaternion); diff --git a/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java b/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java index 0e3986c9..3b027d26 100644 --- a/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java +++ b/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java @@ -106,7 +106,7 @@ public class CreatureUtils { switch(physicsTemplate.getType()){ case "CYLINDER": rigidBody = PhysicsUtils.getCylinderObject(new Vector3f(physicsTemplate.getDimension1(),physicsTemplate.getDimension2(),physicsTemplate.getDimension3())); - collidable = new Collidable(rVal, Collidable.TYPE_ITEM); + collidable = new Collidable(rVal, Collidable.TYPE_CREATURE); rVal.putData(EntityDataStrings.PHYSICS_COLLISION_BODY, rigidBody); rVal.putData(EntityDataStrings.PHYSICS_COLLISION_BODY_OFFSET, new Vector3f(physicsTemplate.getOffsetX(),physicsTemplate.getOffsetY(),physicsTemplate.getOffsetZ())); rVal.putData(EntityDataStrings.PHYSICS_MODEL_TEMPLATE, physicsTemplate); @@ -130,7 +130,7 @@ public class CreatureUtils { break; case "CUBE": rigidBody = PhysicsUtils.getCubeObject(new Vector3f(physicsTemplate.getDimension1(),physicsTemplate.getDimension2(),physicsTemplate.getDimension3())); - collidable = new Collidable(rVal, Collidable.TYPE_ITEM); + collidable = new Collidable(rVal, Collidable.TYPE_CREATURE); rVal.putData(EntityDataStrings.PHYSICS_COLLISION_BODY, rigidBody); rVal.putData(EntityDataStrings.PHYSICS_COLLISION_BODY_OFFSET, new Vector3f(physicsTemplate.getOffsetX(),physicsTemplate.getOffsetY(),physicsTemplate.getOffsetZ())); rVal.putData(EntityDataStrings.PHYSICS_MODEL_TEMPLATE, physicsTemplate); diff --git a/src/main/java/electrosphere/entity/types/debug/DebugVisualizerUtils.java b/src/main/java/electrosphere/entity/types/debug/DebugVisualizerUtils.java new file mode 100644 index 00000000..ba2ffebc --- /dev/null +++ b/src/main/java/electrosphere/entity/types/debug/DebugVisualizerUtils.java @@ -0,0 +1,21 @@ +package electrosphere.entity.types.debug; + +import org.joml.Vector3d; +import org.joml.Vector3f; + +import electrosphere.entity.Entity; +import electrosphere.entity.EntityUtils; + +public class DebugVisualizerUtils { + + + public static Entity spawnVectorVisualizer(Vector3d position, Vector3d direction){ + Entity rVal = EntityUtils.spawnDrawableEntity("Models/unitcube.fbx"); + Vector3d pos = new Vector3d(position).add(new Vector3d(direction).normalize().mul(0.3)); + EntityUtils.getPosition(rVal).set(pos); + EntityUtils.getScale(rVal).set(0.05f,0.3f,0.05f); + EntityUtils.getRotation(rVal).rotateTo(new Vector3f(0,1,0), new Vector3f((float)direction.x,(float)direction.y,(float)direction.z)); + return rVal; + } + +} diff --git a/src/main/java/electrosphere/game/collision/CollisionEngine.java b/src/main/java/electrosphere/game/collision/CollisionEngine.java index 4215a6a8..eca36dff 100644 --- a/src/main/java/electrosphere/game/collision/CollisionEngine.java +++ b/src/main/java/electrosphere/game/collision/CollisionEngine.java @@ -90,11 +90,13 @@ public class CollisionEngine { Vector3d normal = null; Vector3d localPosition1 = null; Vector3d localPosition2 = null; + Vector3d worldPosA = null; + Vector3d worldPosB = null; float magnitude = 0.0f; for (int j = 0; j < manifold.getNumContacts(); j++) { ManifoldPoint contactPoint = manifold.getContactPoint(j); if (contactPoint.getDistance() < 0.0f) { - magnitude = contactPoint.getDistance(); + magnitude = -contactPoint.getDistance(); //linear dampen magnitude = magnitude;// * (float)Math.pow(1.0f - linearDamping,deltaTime * 2); hit = true; @@ -102,12 +104,14 @@ public class CollisionEngine { normal = new Vector3d(contactPoint.normalWorldOnB.x,contactPoint.normalWorldOnB.y,contactPoint.normalWorldOnB.z); localPosition1 = new Vector3d(contactPoint.localPointA.x,contactPoint.localPointA.y,contactPoint.localPointA.z); localPosition2 = new Vector3d(contactPoint.localPointB.x,contactPoint.localPointB.y,contactPoint.localPointB.z); + worldPosA = new Vector3d(contactPoint.positionWorldOnA.x,contactPoint.positionWorldOnA.y,contactPoint.positionWorldOnA.z); + worldPosB = new Vector3d(contactPoint.positionWorldOnB.x,contactPoint.positionWorldOnB.y,contactPoint.positionWorldOnB.z); break; } } if (hit) { - resolveCollision(physicsObject1,physicsObject2, normal, localPosition2, magnitude); - resolveCollision(physicsObject2,physicsObject1, new Vector3d(normal).mul(-1.0), localPosition1, magnitude); + resolveCollision(physicsObject1,physicsObject2, new Vector3d(normal).mul(-1.0), localPosition1, worldPosA, magnitude); + resolveCollision(physicsObject2,physicsObject1, normal, localPosition2, worldPosB, magnitude); // System.out.println("HIT + " + normal); // Collision happened between physicsObject1 and physicsObject2. Collision normal is in variable 'normal'. } @@ -122,31 +126,31 @@ public class CollisionEngine { } - public static void resolveCollision(Collidable impactor, Collidable receiver, Vector3d normal, Vector3d localPosition, float magnitude){ + public static void resolveCollision(Collidable impactor, Collidable receiver, Vector3d normal, Vector3d localPosition, Vector3d worldPos, float magnitude){ switch(receiver.getType()){ case Collidable.TYPE_CREATURE: switch(impactor.getType()){ case Collidable.TYPE_TERRAIN: // System.out.println(EntityUtils.getPosition(impactor.getParent()) + " " + EntityUtils.getPosition(receiver.getParent())); // System.out.println(); -// System.out.println("Terrain-creature collision: " + normal + " mag:" + magnitude); - if(normal.y > normal.x + normal.z){ - normal.x = 0; - normal.z = 0; - } - receiver.addImpulse(new Impulse(normal, localPosition, magnitude * 2, Collidable.TYPE_TERRAIN)); + // System.out.println("Terrain-creature collision: " + normal + " mag:" + magnitude); + // if(normal.y > normal.x + normal.z){ + // normal.x = 0; + // normal.z = 0; + // } + receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude * 2, Collidable.TYPE_TERRAIN)); break; case Collidable.TYPE_CREATURE: - receiver.addImpulse(new Impulse(normal, localPosition, magnitude, Collidable.TYPE_CREATURE)); + receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_CREATURE)); break; case Collidable.TYPE_STRUCTURE: // float realMag = 1f/(float)Math.pow(0.1, magnitude); // System.out.println(normal + " - " + realMag); - receiver.addImpulse(new Impulse(normal, localPosition, magnitude, Collidable.TYPE_STRUCTURE)); + receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_STRUCTURE)); // System.out.println("Structure-creature collision"); break; case Collidable.TYPE_ITEM: - receiver.addImpulse(new Impulse(normal, localPosition, magnitude, Collidable.TYPE_ITEM)); + receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_ITEM)); break; } break; @@ -156,23 +160,23 @@ public class CollisionEngine { // System.out.println(EntityUtils.getPosition(impactor.getParent()) + " " + EntityUtils.getPosition(receiver.getParent())); // System.out.println(); // System.out.println("Terrain-item collision: " + normal + " mag:" + magnitude); - if(normal.y > normal.x + normal.z){ - normal.x = 0; - normal.z = 0; - } - receiver.addImpulse(new Impulse(normal, localPosition, magnitude * 2, Collidable.TYPE_TERRAIN)); + // if(normal.y > normal.x + normal.z){ + // normal.x = 0; + // normal.z = 0; + // } + receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude * 2, Collidable.TYPE_TERRAIN)); break; case Collidable.TYPE_CREATURE: - receiver.addImpulse(new Impulse(normal, localPosition, Math.min(magnitude * 0.5,0.5), Collidable.TYPE_CREATURE)); + receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_CREATURE)); break; case Collidable.TYPE_STRUCTURE: // float realMag = 1f/(float)Math.pow(0.1, magnitude); // System.out.println(normal + " - " + realMag); - receiver.addImpulse(new Impulse(normal, localPosition, magnitude, Collidable.TYPE_STRUCTURE)); + receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_STRUCTURE)); // System.out.println("Structure-creature collision"); break; case Collidable.TYPE_ITEM: - receiver.addImpulse(new Impulse(normal, localPosition, magnitude, Collidable.TYPE_ITEM)); + receiver.addImpulse(new Impulse(normal, localPosition, worldPos, magnitude, Collidable.TYPE_ITEM)); break; } break;