diff --git a/.classpath b/.classpath index ba31ff6..d42d5a8 100644 --- a/.classpath +++ b/.classpath @@ -22,7 +22,7 @@ - + @@ -32,7 +32,12 @@ - + + + + + + diff --git a/src/module-info.java b/src/module-info.java index f493a68..5e0d2fe 100644 --- a/src/module-info.java +++ b/src/module-info.java @@ -9,9 +9,10 @@ open module org.atriasoft.gameengine { exports org.atriasoft.gameengine.engines; exports org.atriasoft.gameengine.geometry; exports org.atriasoft.gameengine.map; - exports org.atriasoft.gameengine.physics; + exports org.atriasoft.gameengine.physics.shape; exports org.atriasoft.gameengine.resource; requires transitive org.atriasoft.gale; requires transitive org.atriasoft.etk; + requires transitive net.jreactphysics3d; } diff --git a/src/org/atriasoft/gameengine/components/ComponentPhysics.java b/src/org/atriasoft/gameengine/components/ComponentPhysics.java index 32da1a0..a3837a2 100644 --- a/src/org/atriasoft/gameengine/components/ComponentPhysics.java +++ b/src/org/atriasoft/gameengine/components/ComponentPhysics.java @@ -5,312 +5,1263 @@ import java.util.List; import org.atriasoft.etk.Color; import org.atriasoft.etk.math.Matrix4f; +import org.atriasoft.etk.math.Quaternion; +import org.atriasoft.etk.math.Transform3D; import org.atriasoft.etk.math.Vector3f; -import org.atriasoft.gale.backend3d.OpenGL; import org.atriasoft.gale.resource.ResourceColored3DObject; -import org.atriasoft.gameengine.internal.Log; import org.atriasoft.gameengine.Component; -import org.atriasoft.gameengine.engines.EngineGravity; -import org.atriasoft.gameengine.physics.PhysicBox; -import org.atriasoft.gameengine.physics.PhysicCollisionAABB; -import org.atriasoft.gameengine.physics.PhysicMapVoxel; -import org.atriasoft.gameengine.physics.PhysicShape; -import org.atriasoft.gameengine.physics.PhysicSphere; -import org.atriasoft.gameengine.physics.ToolCollisionOBBWithOBB; +import org.atriasoft.gameengine.Environement; +import org.atriasoft.gameengine.camera.Camera; +import org.atriasoft.gameengine.engines.EnginePhysics; +import org.atriasoft.gameengine.internal.Log; +import org.atriasoft.gameengine.physics.shape.Box; +import org.atriasoft.gameengine.physics.shape.Capsule; +import org.atriasoft.gameengine.physics.shape.Concave; +import org.atriasoft.gameengine.physics.shape.Cone; +import org.atriasoft.gameengine.physics.shape.ConvexHull; +import org.atriasoft.gameengine.physics.shape.Cylinder; +import org.atriasoft.gameengine.physics.shape.Shape; +import org.atriasoft.gameengine.physics.shape.Sphere; -import entities.Entity; +import net.jreactphysics3d.body.BodyType; +import net.jreactphysics3d.body.RigidBody; +import net.jreactphysics3d.collision.ProxyShape; +import net.jreactphysics3d.collision.TriangleMesh; +import net.jreactphysics3d.collision.TriangleVertexArray; +import net.jreactphysics3d.collision.shapes.AABB; +import net.jreactphysics3d.collision.shapes.BoxShape; +import net.jreactphysics3d.collision.shapes.CapsuleShape; +import net.jreactphysics3d.collision.shapes.CollisionShape; +import net.jreactphysics3d.collision.shapes.ConcaveMeshShape; +import net.jreactphysics3d.collision.shapes.ConcaveShape; +import net.jreactphysics3d.collision.shapes.ConeShape; +import net.jreactphysics3d.collision.shapes.CylinderShape; +import net.jreactphysics3d.collision.shapes.SphereShape; public class ComponentPhysics extends Component { - private PhysicCollisionAABB aabb; - private List aabbIntersection = new ArrayList(); - private List narrowIntersection = new ArrayList(); - private List shapes = new ArrayList(); - private ComponentPosition position; - private boolean staticObject = false; - private boolean manageGravity = false; - public static float globalMaxSpeed = Float.MAX_VALUE; - private float maxSpeed = globalMaxSpeed; - // current speed of the object - private Vector3f speed = new Vector3f(0,0,0); - // current acceleration of the object - private Vector3f acceleration = new Vector3f(0,0,0); - // Applied static force on it - private Vector3f staticForce = new Vector3f(0,0,0); - // Apply dynamic force on it - private Vector3f dynamicForce = new Vector3f(0,0,0); + + //public Signal signalPosition; + protected Transform3D lastTransformEmit; + protected EnginePhysics engine; + protected RigidBody rigidBody; + protected List listShape = new ArrayList<>(); + protected List listProxyShape = new ArrayList<>(); + + protected Vector3f staticForceApplyCenterOfMass = new Vector3f(0, 0, 0); + + protected Vector3f staticTorqueApply = new Vector3f(0, 0, 0); + + protected List shape = new ArrayList<>(); //!< collision shape module ... (independent of bullet lib) + + /** + * @brief Create a basic position component (no orientation and position (0,0,0)) + */ + public ComponentPhysics(final Environement _env) { + this.engine = (EnginePhysics) _env.getEngine(getType()); + // Initial position and orientation of the rigid body + this.lastTransformEmit = new Transform3D(new Vector3f(0, 0, 0), Quaternion.identity()); + this.rigidBody = this.engine.getDynamicsWorld().createRigidBody(this.lastTransformEmit); + this.rigidBody.setUserData(this); + // set collision callback: + //this.engine.getDynamicWorld().testCollision(this.rigidBody, this); + } + + /** + * @brief Create a basic position component + * @param[in] _transform transformation of the position + */ + public ComponentPhysics(final Environement _env, final Transform3D _transform) { + this.engine = (EnginePhysics) _env.getEngine(getType()); + // Create a rigid body in the world + this.rigidBody = this.engine.getDynamicsWorld().createRigidBody(_transform); + this.rigidBody.setUserData(this); + this.lastTransformEmit = _transform; + // set collision callback: + //this.engine.getDynamicWorld().testCollision(this.rigidBody, this); + Log.error("Bounciness=" + this.rigidBody.getMaterial().getBounciness()); + Log.error("FrictionCoefficient=" + this.rigidBody.getMaterial().getFrictionCoefficient()); + Log.error("RollingResistance=" + this.rigidBody.getMaterial().getRollingResistance()); + Log.error("LinearDamping=" + this.rigidBody.getLinearDamping()); + Log.error("AngularDamping=" + this.rigidBody.getAngularDamping()); + this.rigidBody.getMaterial().setBounciness(0.4f); + //this.rigidBody.getMaterial().setFrictionCoefficient(0.01f); + //this.rigidBody.getMaterial().setRollingResistance(0.01f); + this.rigidBody.setAngularDamping(0.9f); + this.rigidBody.setLinearDamping(0.9f); + } + + public void addShape(final Shape _shape) { + this.shape.add(_shape); + } + + /** + * @brief Apply an external force to the body at a given point (in world-space coordinates). + * If the point is not at the center of mass of the body, it will also generate some torque and therefore, change the angular velocity of the body. + * If the body is sleeping, calling this method will wake it up. Note that the force will we added to the sum of the applied forces and that this sum will be reset to zero at the end of each call of the DynamicsWorld::update() method. You can only apply a force to a dynamic body otherwise, this method will do nothing. + * @param[in] _force The force to apply on the body + * @param[in] _point The point where the force is applied (in world-space coordinates) + */ + public void applyForce(final Vector3f _force, final Vector3f _point) { + if (this.rigidBody == null) { + return; + } + this.rigidBody.applyForce(_force, _point); + } + + /** + * @brief Apply an external force to the body at its center of mass. + * If the body is sleeping, calling this method will wake it up. + * @note The force will we added to the sum of the applied forces and that this sum will be reset to zero at the end of each call of the DynamicsWorld::update() method. You can only apply a force to a dynamic body otherwise, this method will do nothing. + * @param[in] _force The external force to apply on the center of mass of the body + * @param[in] _static The torque will be apply while the user des not call the same function with 0 value ... + */ + public void applyForceToCenterOfMass(final Vector3f _force) { + if (this.rigidBody == null) { + return; + } + this.rigidBody.applyForceToCenterOfMass(_force); + } + + public void applyForceToCenterOfMassStatic(final Vector3f _force) { + if (this.rigidBody == null) { + return; + } + this.staticForceApplyCenterOfMass = _force; + } + + /** + * @brief Apply an external force to the body at its center of mass. + * If the body is sleeping, calling this method will wake it up. + * @note The force is apply with a relative axis of the object + * @note The force will we added to the sum of the applied forces and that this sum will be reset to zero at the end of each call of the DynamicsWorld::update() method. You can only apply a force to a dynamic body otherwise, this method will do nothing. + * @param[in] _force The external force to apply on the center of mass of the body + * @param[in] _static The torque will be apply while the user des not call the same function with 0 value ... + */ + public void applyRelativeForceToCenterOfMass(final Vector3f _force) { + if (this.rigidBody == null) { + return; + } + final Vector3f force = this.rigidBody.getTransform().getOrientation().multiply(_force); + this.rigidBody.applyForceToCenterOfMass(force); + } + + public void applyRelativeForceToCenterOfMassStatic(final Vector3f _force) { + if (this.rigidBody == null) { + return; + } + final Vector3f force = this.rigidBody.getTransform().getOrientation().multiply(_force); + + this.staticForceApplyCenterOfMass = force; + } + + /** + * @brief Apply an external torque to the body. + * If the body is sleeping, calling this method will wake it up. + * @note The torque is apply with a relative axis of the object + * @note The force will we added to the sum of the applied torques and that this sum will be reset to zero at the end of each call of the DynamicsWorld::update() method. You can only apply a force to a dynamic body otherwise, this method will do nothing. + * @param[in] _torque The external torque to apply on the body + * @param[in] _static The torque will be apply while the user des not call the same function with 0 value ... + */ + public void applyRelativeTorque(final Vector3f _torque) { + if (this.rigidBody == null) { + return; + } + final Vector3f torque = this.rigidBody.getTransform().getOrientation().multiply(_torque); + this.rigidBody.applyTorque(torque); + } + + public void applyRelativeTorqueStatic(final Vector3f _torque) { + if (this.rigidBody == null) { + return; + } + final Vector3f torque = this.rigidBody.getTransform().getOrientation().multiply(_torque); + this.staticTorqueApply = torque; + } + + /** + * @brief Apply an external torque to the body. + * If the body is sleeping, calling this method will wake it up. + * @note The force will we added to the sum of the applied torques and that this sum will be reset to zero at the end of each call of the DynamicsWorld::update() method. You can only apply a force to a dynamic body otherwise, this method will do nothing. + * @param[in] _torque The external torque to apply on the body + * @param[in] _static The torque will be apply while the user des not call the same function with 0 value ... + */ + public void applyTorque(final Vector3f _torque) { + if (this.rigidBody == null) { + return; + } + this.rigidBody.applyTorque(_torque); + } + + public void applyTorqueStatic(final Vector3f _torque) { + if (this.rigidBody == null) { + return; + } + this.staticTorqueApply = _torque; + } + + /** + * @brief Called when a new contact point is found between two bodies that were separated before. + * @param[in] _other The other component that have the impact + * @param[in] _normal Normal of the impact + * @param[in] _pos Position of the impact at the current object + * @param[in] _posOther Position of the impact at the other object + * @param[in] _penetrationDepth Depth penetration in the object + */ + public void beginContact(final Component _other, final Vector3f _normal, final Vector3f _pos, final Vector3f _posOther, final float _penetrationDepth) { + Log.warning(" collision [BEGIN] " + _pos + " depth=" + _penetrationDepth); + } + + public void drawShape(final ResourceColored3DObject _draw, final Camera _camera) { + final Transform3D transform = getTransform(); + //final float[] mmm = new float[16]; + // Get the OpenGL matrix array of the transform + final Matrix4f mmm = transform.getOpenGLMatrix(); + + final Matrix4f transformationMatrix = mmm.clone(); + transformationMatrix.transpose(); + final Color tmpColor = new Color(1.0f, 0.0f, 0.0f, 0.3f); + for (final Shape it : this.shape) { + if (it.isBox()) { + Log.debug(" Box"); + final Box tmpElement = (Box) it; + final Transform3D transformLocal = new Transform3D(it.getOrigin(), it.getOrientation()); + + Matrix4f transformationMatrixLocal = transformLocal.getOpenGLMatrix(); + transformationMatrixLocal.transpose(); + transformationMatrixLocal = transformationMatrix.multiplyNew(transformationMatrixLocal); + _draw.drawSquare(tmpElement.getSize(), transformationMatrixLocal, tmpColor); + } else if (it.isCylinder()) { + Log.debug(" Cylinder"); + final Cylinder tmpElement = (Cylinder) it; + final Transform3D transformLocal = new Transform3D(it.getOrigin(), it.getOrientation()); + + Matrix4f transformationMatrixLocal = transformLocal.getOpenGLMatrix(); + transformationMatrixLocal.transpose(); + transformationMatrixLocal = transformationMatrix.multiplyNew(transformationMatrixLocal); + _draw.drawCylinder(tmpElement.getRadius(), tmpElement.getSize(), 10, 10, transformationMatrixLocal, tmpColor); + } else if (it.isCapsule()) { + Log.debug(" Capsule"); + final Capsule tmpElement = (Capsule) it; + final Transform3D transformLocal = new Transform3D(it.getOrigin(), it.getOrientation()); + + Matrix4f transformationMatrixLocal = transformLocal.getOpenGLMatrix(); + transformationMatrixLocal.transpose(); + transformationMatrixLocal = transformationMatrix.multiplyNew(transformationMatrixLocal); + _draw.drawCapsule(tmpElement.getRadius(), tmpElement.getSize(), 10, 10, transformationMatrixLocal, tmpColor); + } else if (it.isCone()) { + Log.debug(" Cone"); + final Cone tmpElement = (Cone) it; + final Transform3D transformLocal = new Transform3D(it.getOrigin(), it.getOrientation()); + + Matrix4f transformationMatrixLocal = transformLocal.getOpenGLMatrix(); + transformationMatrixLocal.transpose(); + transformationMatrixLocal = transformationMatrix.multiplyNew(transformationMatrixLocal); + _draw.drawCone(tmpElement.getRadius(), tmpElement.getSize(), 10, 10, transformationMatrixLocal, tmpColor); + } else if (it.isSphere()) { + + Log.debug(" Sphere"); + final Sphere tmpElement = (Sphere) it; + final Transform3D transformLocal = new Transform3D(it.getOrigin(), it.getOrientation()); + + Matrix4f transformationMatrixLocal = transformLocal.getOpenGLMatrix(); + transformationMatrixLocal.transpose(); + transformationMatrixLocal = transformationMatrix.multiplyNew(transformationMatrixLocal); + _draw.drawSphere(tmpElement.getRadius(), 10, 10, transformationMatrixLocal, tmpColor); + } else if (it.isConcave()) { + + Log.debug(" concave"); + final Concave tmpElement = (Concave) it; + final Transform3D transformLocal = new Transform3D(it.getOrigin(), it.getOrientation()); + + final Matrix4f transformationMatrixLocal = transformLocal.getOpenGLMatrix(); + transformationMatrixLocal.transpose(); + transformationMatrixLocal.multiply(transformationMatrixLocal); + + _draw.drawTriangles(tmpElement.getVertex(), tmpElement.getIndices(), transformationMatrixLocal, tmpColor); + } else if (it.isConvexHull()) { + Log.debug(" convexHull"); + final ConvexHull tmpElement = (ConvexHull) it; + break; + } + } + } + + // call done after all cycle update of the physical engine + public void emitAll() { + // emit onbly of new ... + final Transform3D transform = getTransform(); + if (this.lastTransformEmit != transform) { + this.lastTransformEmit = transform; + //TODO Later ... signalPosition.emit(transform); + } + } + + public void generate() { + if (this.shape.size() == 0) { + Log.warning("No Shape Availlable ..."); + return; + } + + // TODO: support more than one shape for each elements... (ProxyShape) + for (final Shape it : this.shape) { + if (it == null) { + continue; + } + if (it.isBox()) { + Log.debug(" Box"); + final Box tmpElement = (Box) it; + // Half extents of the box in the x, y and z directions + final Vector3f halfExtents = new Vector3f(tmpElement.getSize().x, tmpElement.getSize().y, tmpElement.getSize().z); + // Create the box shape + final BoxShape shape = new BoxShape(halfExtents, 0.0001f); + this.listShape.add(shape); + // The ephysic use Y as UP ==> ege use Z as UP + //orientation = orientation * ephysics::Quaternion(-0.707107, 0, 0, 0.707107); + final Transform3D transform = new Transform3D(it.getOrigin(), it.getOrientation()); + final ProxyShape proxyShape = this.rigidBody.addCollisionShape(shape, transform, it.getMass()); + proxyShape.setUserData(this); + this.listProxyShape.add(proxyShape); + } else if (it.isCylinder()) { + Log.debug(" Cylinder"); + final Cylinder tmpElement = (Cylinder) it; + // Create the Cylinder shape + // Create the Cylinder shape + final CylinderShape shape = new CylinderShape(tmpElement.getRadius(), tmpElement.getSize()); + // The ephysic use Y as UP ==> ege use Z as UP + final Quaternion orientation = it.getOrientation().multiplyNew(new Quaternion(-0.707107f, 0.0f, 0.0f, 0.707107f)); + final Transform3D transform = new Transform3D(it.getOrigin(), orientation); + final ProxyShape proxyShape = this.rigidBody.addCollisionShape(shape, transform, it.getMass()); + proxyShape.setUserData(this); + this.listProxyShape.add(proxyShape); + } else if (it.isCapsule()) { + Log.debug(" Capsule"); + final Capsule tmpElement = (Capsule) it; + // Create the Capsule shape + final CapsuleShape shape = new CapsuleShape(tmpElement.getRadius(), tmpElement.getSize()); + // The ephysic use Y as UP ==> ege use Z as UP + final Quaternion orientation = it.getOrientation().multiplyNew(new Quaternion(-0.707107f, 0.0f, 0.0f, 0.707107f)); + final Transform3D transform = new Transform3D(it.getOrigin(), orientation); + final ProxyShape proxyShape = this.rigidBody.addCollisionShape(shape, transform, it.getMass()); + proxyShape.setUserData(this); + this.listProxyShape.add(proxyShape); + } else if (it.isCone()) { + Log.debug(" Cone"); + final Cone tmpElement = (Cone) it; + // Create the Cone shape + final ConeShape shape = new ConeShape(tmpElement.getRadius(), tmpElement.getSize()); + // The ephysic use Y as UP ==> ege use Z as UP + final Quaternion orientation = it.getOrientation().multiplyNew(new Quaternion(-0.707107f, 0.0f, 0.0f, 0.707107f)); + final Transform3D transform = new Transform3D(it.getOrigin(), orientation); + final ProxyShape proxyShape = this.rigidBody.addCollisionShape(shape, transform, it.getMass()); + proxyShape.setUserData(this); + this.listProxyShape.add(proxyShape); + } else if (it.isSphere()) { + Log.debug(" Sphere"); + final Sphere tmpElement = (Sphere) it; + // Create the box shape + final SphereShape shape = new SphereShape(tmpElement.getRadius()); + // The ephysic use Y as UP ==> ege use Z as UP + final Quaternion orientation = it.getOrientation().multiplyNew(new Quaternion(-0.707107f, 0.0f, 0.0f, 0.707107f)); + final Transform3D transform = new Transform3D(it.getOrigin(), orientation); + final ProxyShape proxyShape = this.rigidBody.addCollisionShape(shape, transform, it.getMass()); + proxyShape.setUserData(this); + this.listProxyShape.add(proxyShape); + } else if (it.isConcave()) { + Log.debug(" Concave"); + final Concave tmpElement = (Concave) it; + //static etk::Vector vertices = {Vector3f(-100.0f,-100.0f,-50.0f),Vector3f(100.0f,-100.0f,-50.0f),Vector3f(100.0f,100.0f,-50.0f)}; + //static etk::Vector indices = {0,1,2}; + + //ephysics::TriangleVertexArray* triangleArray = ETK_NEW(ephysics::TriangleVertexArray, vertices, indices); + final TriangleVertexArray triangleArray = new TriangleVertexArray(tmpElement.getVertex(), tmpElement.getIndices()); + // Now that we have a TriangleVertexArray, we need to create a TriangleMesh and add the TriangleVertexArray into it as a subpart. + // Once this is done, we can create the actual ConcaveMeshShape and add it to the body we want to simulate as in the following example: + final TriangleMesh triangleMesh = new TriangleMesh(); + // Add the triangle vertex array to the triangle mesh + triangleMesh.addSubpart(triangleArray); + // Create the concave mesh shape + // TODO : Manage memory leak ... + final ConcaveShape shape = new ConcaveMeshShape(triangleMesh); + // The ephysic use Y as UP ==> ege use Z as UP + final Quaternion orientation = it.getOrientation().multiplyNew(new Quaternion(-0.707107f, 0.0f, 0.0f, 0.707107f)); + final Transform3D transform = new Transform3D(it.getOrigin(), it.getOrientation()); + final ProxyShape proxyShape = this.rigidBody.addCollisionShape(shape, transform, it.getMass()); + proxyShape.setUserData(this); + this.listProxyShape.add(proxyShape); + } else { + Log.debug(" ???"); + // TODO: UNKNOW type ... + } + } + } + + /** + * @brief Get the angular velocity (whole world). + * @return The angular velocity vector of the body + */ + public Vector3f getAngularVelocity() { + if (this.rigidBody == null) { + return new Vector3f(0, 0, 0); + } + return this.rigidBody.getAngularVelocity(); + } + + /** + * @brief Get the linear velocity (whole world). + * @return The linear velocity vector of the body + */ + public Vector3f getLinearVelocity() { + if (this.rigidBody == null) { + return new Vector3f(0, 0, 0); + } + return this.rigidBody.getLinearVelocity(); + } + + /** + * @brief Get the angular velocity (local Body). + * @return The angular velocity vector of the body + */ + public Vector3f getRelativeAngularVelocity() { + if (this.rigidBody == null) { + return new Vector3f(0, 0, 0); + } + final Vector3f value = this.rigidBody.getAngularVelocity(); + return this.rigidBody.getTransform().getOrientation().inverseNew().multiply(value); + } + + /** + * @brief Get the linear velocity (local Body). + * @return The linear velocity vector of the body + */ + public Vector3f getRelativeLinearVelocity() { + if (this.rigidBody == null) { + return new Vector3f(0, 0, 0); + } + final Vector3f value = this.rigidBody.getLinearVelocity(); + return this.rigidBody.getTransform().getOrientation().inverseNew().multiply(value); + } + + public List getShape() { + return this.shape; + } + + /** + * @brief set a new transformation + * @return Transformation of the position + */ + public Transform3D getTransform() { + if (this.rigidBody == null) { + return Transform3D.identity(); + } + return this.rigidBody.getTransform(); + } + @Override public String getType() { return "physics"; } - public ComponentPhysics(boolean manageGravity) { - this.manageGravity = manageGravity; - } - @Override - public void addFriendComponent(Component component) { - if (component.getType().contentEquals("position")) { - position = (ComponentPosition)component; - } - } - @Override - public void removeFriendComponent(Component component) { - // nothing to do. + /** + * @brief Called when a new contact point is found between two bodies. + * @param[in] _other The other component that have the impact + * @param[in] _normal Normal of the impact + * @param[in] _pos Position of the impact at the current object + * @param[in] _posOther Position of the impact at the other object + * @param[in] _penetrationDepth Depth penetration in the object + */ + public void newContact(final Component _other, final Vector3f _normal, final Vector3f _pos, final Vector3f _posOther, final float _penetrationDepth) { + Log.warning(" collision [ NEW ] " + _pos + " depth=" + _penetrationDepth); } - public void updateAABB() { - if (position == null) { - Log.info("No position in Entity "); + public void renderDebug(final ResourceColored3DObject _draw, final Camera _camera) { + if (this.rigidBody == null) { return; } - // TODO Add a flag to check if it is needed to update the AABB... - PhysicCollisionAABB aabbNew = PhysicCollisionAABB.beforeCalculated(); - for (PhysicShape shape : shapes) { - shape.updateAABB(position.getTransform(), aabbNew); - } - aabb = aabbNew; + final Matrix4f transformationMatrix = Matrix4f.identity(); + final Color tmpColor = new Color(0.0f, 1.0f, 0.0f, 0.8f); + final AABB value = this.rigidBody.getAABB(); + _draw.drawCubeLine(value.getMin(), value.getMax(), tmpColor, transformationMatrix, true, true); + } - public PhysicCollisionAABB getAABB() { - return aabb; + /** + * @brief Set the angular velocity (whole world). + * @param[in] _linearVelocity The angular velocity vector of the body + */ + public void setAngularVelocity(final Vector3f _angularVelocity) { + if (this.rigidBody == null) { + return; + } + this.rigidBody.setAngularVelocity(_angularVelocity); } - public void updateForNarrowCollision() { - narrowIntersection.clear(); - if (aabbIntersection.size() == 0) { + public void setBodyType(final PhysicBodyType _type) { + if (this.rigidBody == null) { return; } - if (position == null) { - Log.info("No position in Entity "); - return; + switch (_type) { + case BODY_STATIC: + this.rigidBody.setType(BodyType.STATIC); + break; + case BODY_KINEMATIC: + this.rigidBody.setType(BodyType.KINEMATIC); + break; + case BODY_DYNAMIC: + this.rigidBody.setType(BodyType.DYNAMIC); + break; } - for (PhysicShape shape : shapes) { - shape.updateForNarrowCollision(position.getTransform()); - } - } - public boolean isNarrowCollide() { - if (narrowIntersection.size() == 0) { - return false; - } - return true; - } - public boolean checkNarrowCollision() { - if (this.staticObject == true) { - return false; - } - for (ComponentPhysics elem : aabbIntersection) { - boolean collide = false; - for (PhysicShape shapeCurrent : shapes) { - if (elem.checkCollide(shapeCurrent) == true) { - collide = true; - break; - } - } - if (collide == true) { - narrowIntersection.add(elem); - elem.narrowIntersection.add(this); - } - } - return isNarrowCollide(); - } - public void narrowCollisionCreateContactAndForce() { - if (narrowIntersection.size() == 0) { - return; - } - for (ComponentPhysics elem : narrowIntersection) { - for (PhysicShape shapeCurrent : this.shapes) { - //TODO Do a better method we do this many times ... - if (elem.checkCollide(shapeCurrent) == false) { - continue; - } - elem.getCollidePoints(shapeCurrent, this.staticObject); - } - } - } - - private boolean checkCollide(PhysicShape shapeCurrent) { - if (shapeCurrent instanceof PhysicBox) { - PhysicBox shape111 = (PhysicBox)shapeCurrent; - for (PhysicShape shape : shapes) { - if (shape instanceof PhysicBox) { - PhysicBox shape222 = (PhysicBox)shape; - if (ToolCollisionOBBWithOBB.testCollide(shape111, shape222) == true) { - return true; - } - } else if (shape instanceof PhysicSphere) { - - } else if (shape instanceof PhysicMapVoxel) { - - } else { - Log.error("Not manage collision model... " + shape); - } - } - } else if (shapeCurrent instanceof PhysicSphere) { - for (PhysicShape shape : shapes) { - if (shape instanceof PhysicBox) { - - } else if (shape instanceof PhysicSphere) { - - } else if (shape instanceof PhysicMapVoxel) { - - } else { - Log.error("Not manage collision model... " + shape); - } - } - } else if (shapeCurrent instanceof PhysicMapVoxel) { - for (PhysicShape shape : shapes) { - if (shape instanceof PhysicBox) { - - } else if (shape instanceof PhysicSphere) { - - } else if (shape instanceof PhysicMapVoxel) { - - } else { - Log.error("Not manage collision model... " + shape); - } - } - } else { - Log.error("Not manage collision model... " + shapeCurrent); - } - return false; - } - private void getCollidePoints(PhysicShape shapeCurrent, boolean isStatic) { - if (shapeCurrent instanceof PhysicBox) { - PhysicBox shape111 = (PhysicBox)shapeCurrent; - for (PhysicShape shape : this.shapes) { - if (shape instanceof PhysicBox) { - PhysicBox shape222 = (PhysicBox)shape; - ToolCollisionOBBWithOBB.getCollidePoints(shape111, isStatic, shape222, this.staticObject); - } else if (shape instanceof PhysicSphere) { - - } else if (shape instanceof PhysicMapVoxel) { - - } else { - Log.error("Not manage collision model... " + shape); - } - } - } else if (shapeCurrent instanceof PhysicSphere) { - for (PhysicShape shape : this.shapes) { - if (shape instanceof PhysicBox) { - - } else if (shape instanceof PhysicSphere) { - - } else if (shape instanceof PhysicMapVoxel) { - - } else { - Log.error("Not manage collision model... " + shape); - } - } - } else if (shapeCurrent instanceof PhysicMapVoxel) { - for (PhysicShape shape : this.shapes) { - if (shape instanceof PhysicBox) { - - } else if (shape instanceof PhysicSphere) { - - } else if (shape instanceof PhysicMapVoxel) { - - } else { - Log.error("Not manage collision model... " + shape); - } - } - } else { - Log.error("Not manage collision model... " + shapeCurrent); - } - return; - } - - public void applyForces(float timeStep, EngineGravity gravity) { - // get the gravity at the specific position... - Vector3f gravityForce; - if (manageGravity == true) { - gravityForce = gravity.getGravityAtPosition(position.getTransform().getPosition()).multiplyNew(timeStep); - } else { - gravityForce = new Vector3f(0,0,0); - } - // apply this force on the Object - Log.info("apply gravity: " + gravityForce); - // relative to the object - Vector3f staticForce = this.staticForce.multiplyNew(timeStep); - float globalMass = 0; - for (PhysicShape shape : shapes) { - globalMass += shape.getMass(); - } - // note the acceleration is not real, it depend on the current delta time... - this.acceleration = gravityForce.add(this.position.getTransform().getOrientation().multiply(staticForce)).add(this.position.getTransform().getOrientation().multiply(dynamicForce)).multiply(globalMass); - this.dynamicForce = new Vector3f(0,0,0); - this.speed.add(this.acceleration); - limitWithMaxSpeed(); - Log.info("apply acceleration: " + this.acceleration); - Log.info("apply speed: " + this.speed); - this.position.getTransform().getPosition().add(this.speed.multiplyNew(timeStep)); } - public void renderDebug(ResourceColored3DObject debugDrawProperty) { - Color displayColor; - if (this.aabbIntersection.size() == 0) { - displayColor = new Color(1,1,1,1); - } else { - if (this.narrowIntersection.size() == 0) { - displayColor = new Color(1,1,0,1); - } else { - displayColor = new Color(1,0,0,1); - } + /** + * @brief Set the linear velocity (whole world). + * @param[in] _linearVelocity The linear velocity vector of the body + */ + public void setLinearVelocity(final Vector3f _linearVelocity) { + if (this.rigidBody == null) { + return; } - if (aabb != null) { - debugDrawProperty.drawCubeLine(aabb.getMin(), aabb.getMax(), displayColor, Matrix4f.identity(), true, true); - //debugDrawProperty.drawCubeLine(new Vector3f(0,0,0), new Vector3f(32,32,32), new Color(1,0,1,1), Matrix4f.identity(), true, true); - } else { - Log.error("no AABB"); + this.rigidBody.setLinearVelocity(_linearVelocity); + } + + /** + * @brief Set the angular velocity (local Body). + * @param[in] _linearVelocity The angular velocity vector of the body + */ + public void setRelativeAngularVelocity(final Vector3f _angularVelocity) { + if (this.rigidBody == null) { + return; } - for (PhysicShape shape : shapes) { - shape.renderDebug(position.getTransform(), debugDrawProperty); + final Vector3f value = this.rigidBody.getTransform().getOrientation().multiply(_angularVelocity); + this.rigidBody.setAngularVelocity(value); + } + + /** + * @brief Set the linear velocity (local Body). + * @param[in] _linearVelocity The linear velocity vector of the body + */ + public void setRelativeLinearVelocity(final Vector3f _linearVelocity) { + if (this.rigidBody == null) { + return; } + final Vector3f value = this.rigidBody.getTransform().getOrientation().multiply(_linearVelocity); + this.rigidBody.setLinearVelocity(value); } - public void addShape(PhysicShape shape) { - shapes.add(shape); + + public void setShape(final List _prop) { + this.shape = _prop; } - public void clearShape() { - shapes.clear(); - } - public boolean isManageGravity() { - return manageGravity; - } - public void setManageGravity(boolean manageGravity) { - this.manageGravity = manageGravity; - } - private void limitWithMaxSpeed() { - if (this.speed.length2() > this.maxSpeed*this.maxSpeed) { - this.speed.safeNormalize().multiply(this.maxSpeed); + + /** + * @brief set a new transformation + * @param[in] _transform transformation of the position + */ + public void setTransform(final Transform3D _transform) { + if (this.rigidBody == null) { + return; } + this.rigidBody.setTransform(_transform); } - public float getMaxSpeed() { - return maxSpeed; - } - - public void setMaxSpeed(float maxSpeed) { - this.maxSpeed = maxSpeed; - } - - public void clearAABBIntersection() { - this.aabbIntersection.clear(); - } - public void addIntersection(ComponentPhysics component) { - // do not add multiple times - for (ComponentPhysics elem : this.aabbIntersection) { - if (elem == component) { - return; - } + + // call of this function every time the call will be done + public void update(final float _delta) { + if (this.rigidBody == null) { + return; + } + if (!this.staticForceApplyCenterOfMass.isZero()) { + final Vector3f tmp = this.staticForceApplyCenterOfMass.multiplyNew(_delta); + Log.error("FORCE : " + tmp); + this.rigidBody.applyForceToCenterOfMass(tmp); + } + if (!this.staticTorqueApply.isZero()) { + final Vector3f tmp = this.staticTorqueApply.multiplyNew(_delta); + Log.error("TORQUE : " + tmp); + this.rigidBody.applyTorque(tmp); } - this.aabbIntersection.add(component); - } - public List getAabbIntersection() { - return aabbIntersection; - } - - public boolean isStaticObject() { - return staticObject; - } - - public void setStaticObject(boolean staticObject) { - this.staticObject = staticObject; } } + +// +// +// private List shapes = new ArrayList<>(); +// //public esignal::Signal signalPosition; +// protected Transform3D lastTransformEmit; +// protected RigidBody rigidBody; +// List listShape = new ArrayList<>(); +// List listProxyShape = new ArrayList<>(); +// protected boolean isStatic = false; +// Vector3f staticForceApplyCenterOfMass = new Vector3f(0, 0, 0); +// Vector3f staticTorqueApply = new Vector3f(0, 0, 0); +// +// /** +// * @brief Create a basic position component (no orientation and position (0,0,0)) +// */ +// public ComponentPhysics(final boolean isStatic) { +// this.isStatic = isStatic; +// this.lastTransformEmit = new Transform3D(new Vector3f(0, 0, 0), Quaternion.identity()); +// } +// +// public ComponentPhysics(final boolean isStatic, final Transform3D _transform) { +// this.isStatic = isStatic; +// this.lastTransformEmit = _transform; +// } +// +// @Override +// public void addFriendComponent(final Component component) { +// if (component.getType().contentEquals("position")) { +// final ComponentPosition position = (ComponentPosition) component; +// } +// } +// +// /** +// * @brief Create a basic position component +// * @param[in] _transform transformation of the position +// */ +// /* +// public ComponentPhysics(Environement _env, Transform3D _transform) { +// this.engine = ememory::dynamicPointerCast(_env.getEngine(getType())); +// // Create a rigid body in the world +// this.rigidBody = this.engine.getDynamicWorld().createRigidBody(_transform); +// this.rigidBody.setUserData(this); +// this.lastTransformEmit = _transform; +// // set collision callback: +// //this.engine.getDynamicWorld().testCollision(this.rigidBody, this); +// Log.error("Bounciness=" + this.rigidBody.getMaterial().getBounciness()); +// Log.error("FrictionCoefficient=" + this.rigidBody.getMaterial().getFrictionCoefficient()); +// Log.error("RollingResistance=" + this.rigidBody.getMaterial().getRollingResistance()); +// Log.error("LinearDamping=" + this.rigidBody.getLinearDamping()); +// Log.error("AngularDamping=" + this.rigidBody.getAngularDamping()); +// this.rigidBody.getMaterial().setBounciness(0.4); +// //this.rigidBody.getMaterial().setFrictionCoefficient(0.01); +// //this.rigidBody.getMaterial().setRollingResistance(0.01); +// this.rigidBody.setAngularDamping(0.9); +// this.rigidBody.setLinearDamping(0.9); +// } +// */ +// +// //on close: +// /* +// ~Component() { +// if (this.rigidBody == null) { +// return; +// } +// // disable callback +// this.rigidBody.setUserData(null); +// this.engine.getDynamicWorld().testCollision(this.rigidBody, null); +// this.engine.getDynamicWorld().destroyRigidBody(this.rigidBody); +// this.rigidBody = null; +// } +// */ +// +// public void addShape(final Shape _shape) { +// this.shapes.add(_shape); +// } +// +// /** +// * @brief Apply an external force to the body at a given point (in world-space coordinates). +// * If the point is not at the center of mass of the body, it will also generate some torque and therefore, change the angular velocity of the body. +// * If the body is sleeping, calling this method will wake it up. Note that the force will we added to the sum of the applied forces and that this sum will be reset to zero at the end of each call of the DynamicsWorld::update() method. You can only apply a force to a dynamic body otherwise, this method will do nothing. +// * @param[in] _force The force to apply on the body +// * @param[in] _point The point where the force is applied (in world-space coordinates) +// */ +// public void applyForce(final Vector3f _force, final Vector3f _point) { +// if (this.rigidBody == null) { +// return; +// } +// this.rigidBody.applyForce(_force, _point); +// } +// +// /** +// * @brief Apply an external force to the body at its center of mass. +// * If the body is sleeping, calling this method will wake it up. +// * @note The force will we added to the sum of the applied forces and that this sum will be reset to zero at the end of each call of the DynamicsWorld::update() method. You can only apply a force to a dynamic body otherwise, this method will do nothing. +// * @param[in] _force The external force to apply on the center of mass of the body +// * @param[in] _static The torque will be apply while the user des not call the same function with 0 value ... +// */ +// public void applyForceToCenterOfMass(final Vector3f _force, final boolean _static) { +// if (this.rigidBody == null) { +// return; +// } +// if (_static == true) { +// this.staticForceApplyCenterOfMass = _force; +// } else { +// //this.rigidBody.applyForceToCenterOfMass(_force); +// Log.todo("applyForceToCenterOfMass"); +// } +// } +// +// /** +// * @brief Apply an external force to the body at its center of mass. +// * If the body is sleeping, calling this method will wake it up. +// * @note The force is apply with a relative axis of the object +// * @note The force will we added to the sum of the applied forces and that this sum will be reset to zero at the end of each call of the DynamicsWorld::update() method. You can only apply a force to a dynamic body otherwise, this method will do nothing. +// * @param[in] _force The external force to apply on the center of mass of the body +// * @param[in] _static The torque will be apply while the user des not call the same function with 0 value ... +// */ +// public void applyRelativeForceToCenterOfMass(final Vector3f _force, final boolean _static) { +// if (this.rigidBody == null) { +// return; +// } +// final Vector3f force = this.rigidBody.getTransform().getOrientation().multiply(_force); +// if (_static == true) { +// this.staticForceApplyCenterOfMass = force; +// } else { +// //this.rigidBody.applyForceToCenterOfMass(force); +// Log.todo("applyForceToCenterOfMass"); +// } +// } +// +// /** +// * @brief Apply an external torque to the body. +// * If the body is sleeping, calling this method will wake it up. +// * @note The torque is apply with a relative axis of the object +// * @note The force will we added to the sum of the applied torques and that this sum will be reset to zero at the end of each call of the DynamicsWorld::update() method. You can only apply a force to a dynamic body otherwise, this method will do nothing. +// * @param[in] _torque The external torque to apply on the body +// * @param[in] _static The torque will be apply while the user des not call the same function with 0 value ... +// */ +// public void applyRelativeTorque(final Vector3f _torque, final boolean _static) { +// if (this.rigidBody == null) { +// return; +// } +// final Vector3f torque = this.rigidBody.getTransform().getOrientation().multiply(_torque); +// if (_static == true) { +// this.staticTorqueApply = torque; +// } else { +// this.rigidBody.applyTorque(torque); +// } +// } +// +// /** +// * @brief Apply an external torque to the body. +// * If the body is sleeping, calling this method will wake it up. +// * @note The force will we added to the sum of the applied torques and that this sum will be reset to zero at the end of each call of the DynamicsWorld::update() method. You can only apply a force to a dynamic body otherwise, this method will do nothing. +// * @param[in] _torque The external torque to apply on the body +// * @param[in] _static The torque will be apply while the user des not call the same function with 0 value ... +// */ +// public void applyTorque(final Vector3f _torque, final boolean _static) { +// if (this.rigidBody == null) { +// return; +// } +// if (_static == true) { +// this.staticTorqueApply = _torque; +// } else { +// this.rigidBody.applyTorque(_torque); +// } +// } +// +// /** +// * @brief Called when a new contact point is found between two bodies that were separated before. +// * @param[in] _other The other component that have the impact +// * @param[in] _normal Normal of the impact +// * @param[in] _pos Position of the impact at the current object +// * @param[in] _posOther Position of the impact at the other object +// * @param[in] _penetrationDepth Depth penetration in the object +// */ +// private void beginContact(final Component _other, final Vector3f _normal, final Vector3f _pos, final Vector3f _posOther, final float _penetrationDepth) { +// Log.warning(" collision [BEGIN] " + _pos + " depth=" + _penetrationDepth); +// } +// +// public void clearShape() { +// this.shapes.clear(); +// } +// +// protected RigidBody createRigidBody(final CollisionShape collisionShape, final Transform3D transform, final float mass, final DynamicsWorld dynamicsWorld) { +// +// //this.collisionShape = collisionShape; +// +// final Matrix3f inertiaTensor = new Matrix3f(); +// collisionShape.computeLocalInertiaTensor(inertiaTensor, mass); +// +// return dynamicsWorld.createRigidBody(transform, mass, inertiaTensor, collisionShape); +// } +// +// public void drawAABB(final ResourceColored3DObject debugDrawProperty) { +// if (this.rigidBody == null) { +// return; +// } +// /* +// Matrix4f transformationMatrix; +// Color tmpColor(0.0, 1.0, 0.0, 0.8); +// ephysics::AABB value = this.rigidBody.getAABB(); +// _draw.drawCubeLine(value.getMin(), value.getMax(), tmpColor, transformationMatrix); +// */ +// } +// +// // call done after all cycle update of the physical engine +// public void emitAll() { +// // emit onbly of new ... +// final Transform3D transform = getTransform(); +// if (this.lastTransformEmit != transform) { +// this.lastTransformEmit = transform; +// //signalPosition.emit(transform); +// } +// } +// +// public void generate(final DynamicsWorld dynamicsWorld) { +// if (this.shapes.size() == 0) { +// Log.warning("No Shape Availlable ..."); +// return; +// } +// for (final Shape it : this.shapes) { +// if (it == null) { +// continue; +// } +// if (it.isBox()) { +// Log.debug(" Box"); +// final Box tmpElement = (Box) it; +// // Half extents of the box in the x, y and z directions +// final Vector3f halfExtents = new Vector3f(tmpElement.getSize().x, tmpElement.getSize().y, tmpElement.getSize().z); +// // Create the box shape +// final BoxShape shape = new BoxShape(halfExtents, 0.0001f); +// //this.listShape.add(shape); +// // The ephysic use Y as UP ==> ege use Z as UP +// //orientation = orientation.multiplyNew(new Quaternion(-0.707107, 0, 0, 0.707107)); +// final Transform3D transform = new Transform3D(it.getOrigin(), it.getOrientation()); +// final ProxyShape proxyShape = this.rigidBody.addCollisionShape(shape, transform, it.getMass()); +// proxyShape.setUserData(this); +// this.listProxyShape.add(proxyShape); +// +// this.rigidBody = createRigidBody(shape, transform, it.getMass(), dynamicsWorld); +// proxyShape.setUserData(this); +// //this.rigidBody.getMaterial().setBounciness(0.4f); +// //this.rigidBody.setAngularDamping(0.9f); +// //this.rigidBody.setLinearDamping(0.9f); +// } else if (it.isCylinder()) { +// Log.debug(" Cylinder"); +// final Cylinder tmpElement = (Cylinder) it; +// // Create the Cylinder shape +// final CylinderShape shape = new CylinderShape(tmpElement.getRadius(), tmpElement.getSize(), 0.0001f); +// +// // The ephysic use Y as UP ==> ege use Z as UP +// //orientation = orientation.multiplyNew(new Quaternion(-0.707107, 0, 0, 0.707107)); +// final Transform3D transform = new Transform3D(it.getOrigin(), it.getOrientation()); +// //ProxyShape proxyShape = this.rigidBody.addCollisionShape(shape, transform, it.getMass()); +// this.rigidBody = createRigidBody(shape, transform, it.getMass(), dynamicsWorld); +// //this.colisionShape = shape; +// +// // set it enable in the system: +// //this.rigidBody.setIsMotionEnabled(true); +// // Change the material properties of the rigid body +// final Material material = this.rigidBody.getMaterial(); +// material.setBounciness(0.2f); +// } else if (it.isCapsule()) { +// Log.debug(" Capsule"); +// /* +// final Capsule tmpElement = (Capsule) it; +// +// // Create the Capsule shape +// final CapsuleShape shape = new CapsuleShape(tmpElement.getRadius(), tmpElement.getSize(), 0.0001f); +// +// // The ephysic use Y as UP ==> ege use Z as UP +// //orientation = orientation.multiplyNew(new Quaternion(-0.707107, 0, 0, 0.707107)); +// final Transform3D transform = new Transform3D(it.getOrigin(), it.getOrientation()); +// //ProxyShape proxyShape = this.rigidBody.addCollisionShape(shape, transform, it.getMass()); +// this.rigidBody = createRigidBody(shape, transform, it.getMass(), dynamicsWorld); +// //this.colisionShape = shape; +// +// // set it enable in the system: +// //this.rigidBody.isMotionEnabled(true); +// // Change the material properties of the rigid body +// final Material material = this.rigidBody.getMaterial(); +// material.setBounciness(0.2f); +// */ +// } else if (it.isCone()) { +// /* +// Log.debug(" Cone"); +// final Cone tmpElement = (Cone) it; +// final ConeShape shape = new ConeShape(tmpElement.getRadius(), tmpElement.getSize(), 0.0001f); +// // The ephysic use Y as UP ==> ege use Z as UP +// //orientation = orientation.multiplyNew(new Quaternion(-0.707107, 0, 0, 0.707107)); +// final Transform3D transform = new Transform3D(it.getOrigin(), it.getOrientation()); +// final ProxyShape proxyShape = this.rigidBody.addCollisionShape(shape, transform, it.getMass()); +// proxyShape.setUserData(this); +// this.listProxyShape.add(proxyShape); +// this.rigidBody = createRigidBody(shape, transform, it.getMass(), dynamicsWorld); +// //this.colisionShape = shape; +// +// // set it enable in the system: +// //this.rigidBody.setIsMotionEnabled(true); +// // Change the material properties of the rigid body +// final Material material = this.rigidBody.getMaterial(); +// material.setBounciness(0.2f); +// */ +// } else if (it.isSphere()) { +// /* +// Log.debug(" Sphere"); +// final Sphere tmpElement = (Sphere) it; +// final SphereShape shape = new SphereShape(tmpElement.getRadius(), 0.0001f); +// // The ephysic use Y as UP ==> ege use Z as UP +// //orientation = orientation.multiplyNew(new Quaternion(-0.707107, 0, 0, 0.707107)); +// final Transform3D transform = new Transform3D(it.getOrigin(), it.getOrientation()); +// //ProxyShape proxyShape = this.rigidBody.addCollisionShape(shape, transform, it.getMass()); +// this.rigidBody = createRigidBody(shape, transform, it.getMass(), dynamicsWorld); +// this.colisionShape = shape; +// +// // set it enable in the system: +// this.rigidBody.setIsMotionEnabled(true); +// // Change the material properties of the rigid body +// final Material material = this.rigidBody.getMaterial(); +// material.setBounciness(0.2f); +// */ +// } else if (it.isConcave()) { +// Log.debug(" Concave"); +// final Concave tmpElement = (Concave) it; +// /* +// static Vector vertices = {Vector3f(-100.0f,-100.0f,-50.0f),Vector3f(100.0f,-100.0f,-50.0f),Vector3f(100.0f,100.0f,-50.0f)}; +// static Vector indices = {0,1,2}; +// +// ephysics::TriangleVertexArray* triangleArray = ETK_NEW(ephysics::TriangleVertexArray, vertices, indices); +// #else +// ephysics::TriangleVertexArray* triangleArray = ETK_NEW(ephysics::TriangleVertexArray, tmpElement.getVertex(), tmpElement.getIndices()); +// #endif +// // Now that we have a TriangleVertexArray, we need to create a TriangleMesh and add the TriangleVertexArray into it as a subpart. +// // Once this is done, we can create the actual ConcaveMeshShape and add it to the body we want to simulate as in the following example: +// ephysics::TriangleMesh* triangleMesh = ETK_NEW(ephysics::TriangleMesh); +// // Add the triangle vertex array to the triangle mesh +// triangleMesh.addSubpart(triangleArray); +// // Create the concave mesh shape +// ephysics::ConcaveShape* shape = ETK_NEW(ephysics::ConcaveMeshShape, triangleMesh); +// // The ephysic use Y as UP ==> ege use Z as UP +// Quaternion orientation = it.getOrientation() * Quaternion(-0.707107, 0, 0, 0.707107); +// Transform3D transform(it.getOrigin(), it.getOrientation()); +// ephysics::ProxyShape* proxyShape = this.rigidBody.addCollisionShape(shape, transform, it.getMass()); +// proxyShape.setUserData(this); +// this.listProxyShape.pushBack(proxyShape); +// break; +// */ +// } else { +// Log.debug(" ???"); +// } +// } +// } +// +// /** +// * @brief Get the angular velocity (whole world). +// * @return The angular velocity vector of the body +// */ +// public Vector3f getAngularVelocity() { +// if (this.rigidBody == null) { +// return new Vector3f(0, 0, 0); +// } +// return this.rigidBody.getAngularVelocity(); +// } +// +// /** +// * @brief Get the linear velocity (whole world). +// * @return The linear velocity vector of the body +// */ +// public Vector3f getLinearVelocity() { +// if (this.rigidBody == null) { +// return new Vector3f(0, 0, 0); +// } +// return this.rigidBody.getLinearVelocity(); +// } +// +// /** +// * @brief Get the angular velocity (local Body). +// * @return The angular velocity vector of the body +// */ +// public Vector3f getRelativeAngularVelocity() { +// if (this.rigidBody == null) { +// return new Vector3f(0, 0, 0); +// } +// final Vector3f value = this.rigidBody.getAngularVelocity(); +// return this.rigidBody.getTransform().getOrientation().inverseNew().multiply(value); +// } +// +// /** +// * @brief Get the linear velocity (local Body). +// * @return The linear velocity vector of the body +// */ +// public Vector3f getRelativeLinearVelocity() { +// if (this.rigidBody == null) { +// return new Vector3f(0, 0, 0); +// } +// final Vector3f value = this.rigidBody.getLinearVelocity(); +// return this.rigidBody.getTransform().getOrientation().inverseNew().multiply(value); +// } +// +// public List getShape() { +// return this.shapes; +// } +// +// /** +// * @brief set a new transformation +// * @return Transformation of the position +// */ +// public Transform3D getTransform() { +// if (this.rigidBody == null) { +// return Transform3D.identity(); +// } +// return this.rigidBody.getTransform(); +// } +// +// @Override +// public String getType() { +// return "physics"; +// } +// +// /** +// * @brief Called when a new contact point is found between two bodies. +// * @param[in] _other The other component that have the impact +// * @param[in] _normal Normal of the impact +// * @param[in] _pos Position of the impact at the current object +// * @param[in] _posOther Position of the impact at the other object +// * @param[in] _penetrationDepth Depth penetration in the object +// */ +// private void newContact(final Component _other, final Vector3f _normal, final Vector3f _pos, final Vector3f _posOther, final float _penetrationDepth) { +// Log.warning(" collision [ NEW ] " + _pos + " depth=" + _penetrationDepth); +// } +// +// @Override +// public void removeFriendComponent(final Component component) { +// // nothing to do. +// } +// +// public void renderDebug(final ResourceColored3DObject debugDrawProperty) { +// final Color displayColor; +// /* +// if (this.aabbIntersection.size() == 0) { +// displayColor = new Color(1,1,1,1); +// } else { +// if (this.narrowIntersection.size() == 0) { +// displayColor = new Color(1,1,0,1); +// } else { +// displayColor = new Color(1,0,0,1); +// } +// } +// if (aabb != null) { +// debugDrawProperty.drawCubeLine(aabb.getMin(), aabb.getMax(), displayColor, Matrix4f.identity(), true, true); +// //debugDrawProperty.drawCubeLine(new Vector3f(0,0,0), new Vector3f(32,32,32), new Color(1,0,1,1), Matrix4f.identity(), true, true); +// } else { +// Log.error("no AABB"); +// } +// */ +// for (final Shape shape : this.shapes) { +// //shape.renderDebug(position.getTransform(), debugDrawProperty); +// } +// //} +// //public void drawShape(ewol::resource::Colored3DObject _draw, ege::Camera _camera) { +// /* +// Transform3D transform = getTransform(); +// float mmm[16]; +// // Get the OpenGL matrix array of the transform +// transform.getOpenGLMatrix(mmm); +// Matrix4f transformationMatrix(mmm); +// transformationMatrix.transpose(); +// Color tmpColor(1.0, 0.0, 0.0, 0.3); +// for (auto &it: this.shape) { +// if (it == null) { +// continue; +// } +// switch (it.getType()) { +// case ege::physics::Shape::type::box: { +// Log.debug(" Box"); +// ege::physics::shape::Box* tmpElement = it.toBox(); +// if (tmpElement == null) { +// Log.error(" Box ==> can not cast in BOX"); +// continue; +// } +// Transform3D transformLocal(it.getOrigin(), it.getOrientation()); +// transformLocal.getOpenGLMatrix(mmm); +// Matrix4f transformationMatrixLocal(mmm); +// transformationMatrixLocal.transpose(); +// transformationMatrixLocal = transformationMatrix * transformationMatrixLocal; +// _draw.drawSquare(tmpElement.getSize(), transformationMatrixLocal, tmpColor); +// break; +// } +// case ege::physics::Shape::type::cylinder: { +// Log.debug(" Cylinder"); +// ege::physics::shape::Cylinder* tmpElement = it.toCylinder(); +// if (tmpElement == null) { +// Log.error(" Cylinder ==> can not cast in Cylinder"); +// continue; +// } +// Transform3D transformLocal(it.getOrigin(), it.getOrientation()); +// transformLocal.getOpenGLMatrix(mmm); +// Matrix4f transformationMatrixLocal(mmm); +// transformationMatrixLocal.transpose(); +// transformationMatrixLocal = transformationMatrix * transformationMatrixLocal; +// _draw.drawCylinder(tmpElement.getRadius(), tmpElement.getSize(), 10, 10, transformationMatrixLocal, tmpColor); +// break; +// } +// case ege::physics::Shape::type::capsule: { +// Log.debug(" Capsule"); +// ege::physics::shape::Capsule* tmpElement = it.toCapsule(); +// if (tmpElement == null) { +// Log.error(" Capsule ==> can not cast in Capsule"); +// continue; +// } +// Transform3D transformLocal(it.getOrigin(), it.getOrientation()); +// transformLocal.getOpenGLMatrix(mmm); +// Matrix4f transformationMatrixLocal(mmm); +// transformationMatrixLocal.transpose(); +// transformationMatrixLocal = transformationMatrix * transformationMatrixLocal; +// _draw.drawCapsule(tmpElement.getRadius(), tmpElement.getSize(), 10, 10, transformationMatrixLocal, tmpColor); +// break; +// } +// case ege::physics::Shape::type::cone: { +// Log.debug(" Cone"); +// ege::physics::shape::Cone* tmpElement = it.toCone(); +// if (tmpElement == null) { +// Log.error(" Cone ==> can not cast in Cone"); +// continue; +// } +// Transform3D transformLocal(it.getOrigin(), it.getOrientation()); +// transformLocal.getOpenGLMatrix(mmm); +// Matrix4f transformationMatrixLocal(mmm); +// transformationMatrixLocal.transpose(); +// transformationMatrixLocal = transformationMatrix * transformationMatrixLocal; +// _draw.drawCone(tmpElement.getRadius(), tmpElement.getSize(), 10, 10, transformationMatrixLocal, tmpColor); +// break; +// } +// case ege::physics::Shape::type::sphere: { +// Log.debug(" Sphere"); +// ege::physics::shape::Sphere* tmpElement = it.toSphere(); +// if (tmpElement == null) { +// Log.error(" Sphere ==> can not cast in Sphere"); +// continue; +// } +// Transform3D transformLocal(it.getOrigin(), it.getOrientation()); +// transformLocal.getOpenGLMatrix(mmm); +// Matrix4f transformationMatrixLocal(mmm); +// transformationMatrixLocal.transpose(); +// transformationMatrixLocal = transformationMatrix * transformationMatrixLocal; +// _draw.drawSphere(tmpElement.getRadius(), 10, 10, transformationMatrixLocal, tmpColor); +// break; +// } +// case ege::physics::Shape::type::concave: { +// Log.debug(" concave"); +// ege::physics::shape::Concave* tmpElement = it.toConcave(); +// if (tmpElement == null) { +// Log.error(" concave ==> can not cast in convexHull"); +// continue; +// } +// Transform3D transformLocal(it.getOrigin(), it.getOrientation()); +// transformLocal.getOpenGLMatrix(mmm); +// Matrix4f transformationMatrixLocal(mmm); +// transformationMatrixLocal.transpose(); +// transformationMatrixLocal = transformationMatrix * transformationMatrixLocal; +// +// _draw.drawTriangles(tmpElement.getVertex(), tmpElement.getIndices(), transformationMatrixLocal, tmpColor); +// break; +// } +// case ege::physics::Shape::type::convexHull: { +// Log.debug(" convexHull"); +// ege::physics::shape::ConvexHull* tmpElement = it.toConvexHull(); +// if (tmpElement == null) { +// Log.error(" convexHull ==> can not cast in convexHull"); +// continue; +// } +// break; +// } +// default : +// Log.debug(" ???"); +// break; +// } +// } +// */ +// } +// +// /** +// * @brief Set the angular velocity (whole world). +// * @param[in] _linearVelocity The angular velocity vector of the body +// */ +// public void setAngularVelocity(final Vector3f _angularVelocity) { +// if (this.rigidBody == null) { +// return; +// } +// this.rigidBody.setAngularVelocity(_angularVelocity); +// } +// +// /** +// * @brief Set the linear velocity (whole world). +// * @param[in] _linearVelocity The linear velocity vector of the body +// */ +// public void setLinearVelocity(final Vector3f _linearVelocity) { +// if (this.rigidBody == null) { +// return; +// } +// this.rigidBody.setLinearVelocity(_linearVelocity); +// }; +// +// /** +// * @brief Set the angular velocity (local Body). +// * @param[in] _linearVelocity The angular velocity vector of the body +// */ +// public void setRelativeAngularVelocity(final Vector3f _angularVelocity) { +// if (this.rigidBody == null) { +// return; +// } +// final Vector3f value = this.rigidBody.getTransform().getOrientation().multiply(_angularVelocity); +// this.rigidBody.setAngularVelocity(value); +// } +// +// /** +// * @brief Set the linear velocity (local Body). +// * @param[in] _linearVelocity The linear velocity vector of the body +// */ +// public void setRelativeLinearVelocity(final Vector3f _linearVelocity) { +// if (this.rigidBody == null) { +// return; +// } +// final Vector3f value = this.rigidBody.getTransform().getOrientation().multiply(_linearVelocity); +// this.rigidBody.setLinearVelocity(value); +// } +// +// public void setShape(final List _prop) { +// this.shapes = _prop; +// } +// +// /* +// public enum type { +// bodyDynamic; +// bodyStatic; +// bodyKinematic; +// } +// +// public void setType(type _type) { +// if (this.rigidBody == null) { +// return; +// } +// switch(_type) { +// case bodyStatic: +// //rigidBody.setType(STATIC); +// break; +// case bodyKinematic: +// //rigidBody.setType(KINEMATIC); +// break; +// case bodyDynamic: +// //rigidBody.setType(DYNAMIC); +// break; +// } +// } +// */ +// /** +// * @brief set a new transformation +// * @param[in] _transform transformation of the position +// */ +// public void setTransform(final Transform3D _transform) { +// if (this.rigidBody == null) { +// return; +// } +// this.rigidBody.setTransform(_transform); +// } +// +// // call of this function every time the call will be done +// private void update(final float _delta) { +// if (this.rigidBody == null) { +// return; +// } +// /* +// if (this.staticForceApplyCenterOfMass != Vector3f(0,0,0)) { +// Vector3f tmp = this.staticForceApplyCenterOfMass*_delta; +// Log.error("FORCE : " + tmp ); +// this.rigidBody.applyForceToCenterOfMass(tmp); +// } +// if (this.staticTorqueApply != Vector3f(0,0,0)) { +// Vector3f tmp = this.staticTorqueApply*_delta; +// Log.error("TORQUE : " + tmp); +// this.rigidBody.applyTorque(tmp); +// } +// */ +// } diff --git a/src/org/atriasoft/gameengine/components/PhysicBodyType.java b/src/org/atriasoft/gameengine/components/PhysicBodyType.java new file mode 100644 index 0000000..9b7c444 --- /dev/null +++ b/src/org/atriasoft/gameengine/components/PhysicBodyType.java @@ -0,0 +1,7 @@ +package org.atriasoft.gameengine.components; + +public enum PhysicBodyType { + BODY_DYNAMIC, + BODY_STATIC, + BODY_KINEMATIC, +} diff --git a/src/org/atriasoft/gameengine/engines/EnginePhysics.java b/src/org/atriasoft/gameengine/engines/EnginePhysics.java index 31c3bec..e0460ab 100644 --- a/src/org/atriasoft/gameengine/engines/EnginePhysics.java +++ b/src/org/atriasoft/gameengine/engines/EnginePhysics.java @@ -2,150 +2,192 @@ package org.atriasoft.gameengine.engines; import java.util.Vector; +import org.atriasoft.etk.math.Vector3f; import org.atriasoft.gale.resource.ResourceColored3DObject; -import org.atriasoft.gameengine.internal.Log; import org.atriasoft.gameengine.Component; import org.atriasoft.gameengine.Engine; import org.atriasoft.gameengine.Environement; import org.atriasoft.gameengine.camera.Camera; import org.atriasoft.gameengine.components.ComponentPhysics; -import org.atriasoft.gameengine.physics.PhysicCollisionAABB; +import org.atriasoft.gameengine.internal.Log; -public class EnginePhysics extends Engine { +import net.jreactphysics3d.constraint.ContactPointInfo; +import net.jreactphysics3d.engine.DynamicsWorld; +import net.jreactphysics3d.engine.EventListener; + +public class EnginePhysics extends Engine implements EventListener { public static final String ENGINE_NAME = "physics"; + // Constant physics time step + private static final float TIME_STEP = 1.0f / 60.0f; + boolean propertyDebugAABB = false; + boolean propertyDebugShape = false; + // Start engine with no gravity + private final Vector3f gravity = new Vector3f(0.0f, 0.0f, 0.0f); private float accumulator = 0; - private static final float TIME_STEP = 0.005f; - private EngineGravity gravity; - private Vector components = new Vector(); - private ResourceColored3DObject debugDrawProperty = ResourceColored3DObject.create(); + //private final EngineGravity gravity; + private final DynamicsWorld dynamicsWorld; - public EnginePhysics(Environement env) { + private final Vector components = new Vector(); + + private final ResourceColored3DObject debugDrawProperty = ResourceColored3DObject.create(); + + public EnginePhysics(final Environement env) { super(env); - this.gravity = (EngineGravity)env.getEngine("gravity"); + /* + this.gravity = (EngineGravity) env.getEngine("gravity"); if (this.gravity == null) { Log.critical("Must initialyse Gravity before physics..."); } + */ + final Vector3f gravity = new Vector3f(0.0f, 0.0f, 0.0f); + this.dynamicsWorld = new DynamicsWorld(gravity); + // Set the number of iterations of the constraint solver + this.dynamicsWorld.setNbIterationsVelocitySolver(15); + this.dynamicsWorld.setEventListener(this); } - - @Override - public void componentRemove(Component ref) { - components.remove(ref); + + private void applyForces(final float timeStep) { + for (final ComponentPhysics it : this.components) { + //it.applyForces(TIME_STEP, gravity); + } } - + @Override - public void componentAdd(Component ref) { + public void beginContact(final ContactPointInfo contact) { + ComponentPhysics component1 = null; + ComponentPhysics component2 = null; + // Called when a new contact point is found between two bodies that were separated before. + Log.warning("collision detection [BEGIN] " + contact.localPoint1 + " depth=" + contact.penetrationDepth); + if (contact.shape1 != null && contact.shape1.getUserData() != null) { + component1 = (ComponentPhysics) contact.shape1.getUserData(); + } + if (contact.shape2 != null && contact.shape2.getUserData() != null) { + component2 = (ComponentPhysics) contact.shape2.getUserData(); + } + if (component1 != null) { + component1.beginContact(component2, contact.normal, contact.localPoint1, contact.localPoint2, contact.penetrationDepth); + } + if (component2 != null) { + component2.beginContact(component1, contact.normal.multiplyNew(-1), contact.localPoint2, contact.localPoint1, contact.penetrationDepth); + } + } + + @Override + public void beginInternalTick() { + // TODO Auto-generated method stub + + } + + @Override + public void componentAdd(final Component ref) { if (ref instanceof ComponentPhysics == false) { return; } - components.add((ComponentPhysics)ref); + final ComponentPhysics elem = (ComponentPhysics) ref; + this.components.add(elem); + elem.generate(); } - + @Override - public void update(long deltaMili) { - // Add the time difference in the accumulator - accumulator += (float)deltaMili*0.0001f; - // While there is enough accumulated time to take one or several physics steps - while (accumulator >= TIME_STEP) { - Log.info("update physic ... " + accumulator); - //applyForces(TIME_STEP); - updateAABB(TIME_STEP); - updateCollisionsAABB(TIME_STEP); - updateCollisionsNarrowPhase(TIME_STEP); - generateResultCollisionsForces(TIME_STEP); - // Decrease the accumulated time - accumulator -= TIME_STEP; - } - + public void componentRemove(final Component ref) { + this.components.remove(ref); } - - private void applyForces(float timeStep) { - for (ComponentPhysics it: components) { - it.applyForces(TIME_STEP, gravity); - } - } - /** - * Collision detection STEP 1: Upadte the AABB positioning of each elements - * @param timeStep Delta time since the last check - */ - private void updateAABB(float timeStep) { - for (ComponentPhysics it: components) { - it.updateAABB(); - } - } - /** - * Collision Detection STEP 2: update the list of each element that collide together in the AABB Boxs (update is done between each boxes) - * @param timeStep Delta time since the last check - */ - private void updateCollisionsAABB(float timeStep) { - // clear all object intersection - for (ComponentPhysics it: components) { - it.clearAABBIntersection(); - } - // update the current object intersection... - for (int iii=0; iii< components.size(); iii++) { - ComponentPhysics current = components.get(iii); - PhysicCollisionAABB currentAABB = current.getAABB(); - for (int jjj=iii+1; jjj< components.size(); jjj++) { - ComponentPhysics remote = components.get(jjj); - if (currentAABB.intersect(components.get(jjj).getAABB()) == true) { - current.addIntersection(remote); - remote.addIntersection(current); - } - } - } - } - /** - * Collision Detection STEP 3: Narrow phase: process the collision between every OBB boxes (or other..) - * @param timeStep Delta time since the last check - */ - private void updateCollisionsNarrowPhase(float timeStep) { - // clear all object intersection - for (ComponentPhysics it: components) { - it.updateForNarrowCollision(); - } - // check for every component if the narrow collision is available. - for (int iii=0; iii< components.size(); iii++) { - ComponentPhysics current = components.get(iii); - boolean collide = current.checkNarrowCollision(); - - } - // update the force of collision available. - for (int iii=0; iii< components.size(); iii++) { - ComponentPhysics current = components.get(iii); - current.narrowCollisionCreateContactAndForce(); - } - } - /** - * Collision Detection STEP 4: apply all calculated forces (with containts) - * @param timeStep - */ - private void generateResultCollisionsForces(float timeStep) { - - } - + @Override - public void render(long deltaMili, Camera camera) { + public void endInternalTick() { // TODO Auto-generated method stub - for (ComponentPhysics it: this.components) { + + } + + public DynamicsWorld getDynamicsWorld() { + return this.dynamicsWorld; + } + + @Override + public String getType() { + // TODO Auto-generated method stub + return ENGINE_NAME; + } + + @Override + public void newContact(final ContactPointInfo contact) { + + ComponentPhysics component1 = null; + ComponentPhysics component2 = null; + //Called when a new contact point is found between two bodies. + Log.warning("collision detection [ NEW ] " + contact.localPoint1 + " depth=" + contact.penetrationDepth); + if (contact.shape1 != null && contact.shape1.getUserData() != null) { + component1 = (ComponentPhysics) contact.shape1.getUserData(); + } + if (contact.shape2 != null && contact.shape2.getUserData() != null) { + component2 = (ComponentPhysics) contact.shape2.getUserData(); + } + if (component1 != null) { + component1.newContact(component2, contact.normal, contact.localPoint1, contact.localPoint2, contact.penetrationDepth); + } + if (component2 != null) { + component2.newContact(component1, contact.normal.multiplyNew(-1), contact.localPoint2, contact.localPoint1, contact.penetrationDepth); + } + } + + @Override + public void render(final long deltaMili, final Camera camera) { + // TODO Auto-generated method stub + for (final ComponentPhysics it : this.components) { //Log.info("Render " + it); - it.renderDebug(debugDrawProperty); + it.renderDebug(this.debugDrawProperty, camera); } //debugDrawProperty.drawCone(2, 5, 9, 12, Matrix4f.identity(), new Color(1,1,0,1)); //debugDrawProperty.drawSquare(new Vector3f(1,1,1), Matrix4f.identity(), new Color(1,1,0,1)); //debugDrawProperty.drawCubeLine(new Vector3f(1,1,1), new Vector3f(5,5,5), new Color(1,0,1,1), Matrix4f.identity(), true, true); //debugDrawProperty.drawCubeLine(new Vector3f(0,0,0), new Vector3f(32,32,32), new Color(1,0,1,1), Matrix4f.identity(), true, true); } - + @Override - public void renderDebug(long deltaMili, Camera camera) { - // TODO Auto-generated method stub - + public void renderDebug(final long deltaMili, final Camera camera) { + if (this.propertyDebugShape == true) { + for (final ComponentPhysics it : this.components) { + it.drawShape(this.debugDrawProperty, camera); + } + } + if (this.propertyDebugAABB == true) { + for (final ComponentPhysics it : this.components) { + it.renderDebug(this.debugDrawProperty, camera); + } + } } - + + void setGravity(final Vector3f _axePower) { + if (this.dynamicsWorld != null) { + final Vector3f gravity = _axePower.clone(); + this.dynamicsWorld.setGravity(gravity); + } + } + @Override - public String getType() { - // TODO Auto-generated method stub - return ENGINE_NAME; + public void update(final long deltaMili) { + final float deltaTime = deltaMili * 0.0001f; + // Add the time difference in the accumulator + this.accumulator += deltaTime; + // While there is enough accumulated time to take one or several physics steps + while (this.accumulator >= TIME_STEP) { + if (this.dynamicsWorld != null) { + // call every object to usdate their constant forces applyed + for (final ComponentPhysics it : this.components) { + if (it != null) { + it.update(TIME_STEP); + } + } + // Update the Dynamics world with a constant time step + Log.debug("Update the Physic engine ... " + TIME_STEP); + this.dynamicsWorld.update(TIME_STEP); + } + // Decrease the accumulated time + this.accumulator -= TIME_STEP; + } + for (final ComponentPhysics elem : this.components) { + elem.emitAll(); + } } - + } diff --git a/src/org/atriasoft/gameengine/internal/Log.java b/src/org/atriasoft/gameengine/internal/Log.java index 81377a8..4e48707 100644 --- a/src/org/atriasoft/gameengine/internal/Log.java +++ b/src/org/atriasoft/gameengine/internal/Log.java @@ -14,47 +14,55 @@ public class Log { private static final boolean PRINT_VERBOSE = Logger.getNeedPrint(LIB_NAME, LogLevel.VERBOSE); private static final boolean PRINT_TODO = Logger.getNeedPrint(LIB_NAME, LogLevel.TODO); private static final boolean PRINT_PRINT = Logger.getNeedPrint(LIB_NAME, LogLevel.PRINT); - - private Log() {} - - public static void print(String data) { - if (PRINT_PRINT) - Logger.print(LIB_NAME_DRAW, data); - } - - public static void todo(String data) { - if (PRINT_TODO) - Logger.todo(LIB_NAME_DRAW, data); - } - - public static void critical(String data) { - if (PRINT_CRITICAL) + + public static void critical(final String data) { + if (PRINT_CRITICAL) { Logger.critical(LIB_NAME_DRAW, data); + } } - - public static void error(String data) { - if (PRINT_ERROR) - Logger.error(LIB_NAME_DRAW, data); - } - - public static void warning(String data) { - if (PRINT_WARNING) - Logger.warning(LIB_NAME_DRAW, data); - } - - public static void info(String data) { - if (PRINT_INFO) - Logger.info(LIB_NAME_DRAW, data); - } - - public static void debug(String data) { - if (PRINT_DEBUG) + + public static void debug(final String data) { + if (PRINT_DEBUG) { Logger.debug(LIB_NAME_DRAW, data); + } } - - public static void verbose(String data) { - if (PRINT_VERBOSE) + + public static void error(final String data) { + if (PRINT_ERROR) { + Logger.error(LIB_NAME_DRAW, data); + } + } + + public static void info(final String data) { + if (PRINT_INFO) { + Logger.info(LIB_NAME_DRAW, data); + } + } + + public static void print(final String data) { + if (PRINT_PRINT) { + Logger.print(LIB_NAME_DRAW, data); + } + } + + public static void todo(final String data) { + if (PRINT_TODO) { + Logger.todo(LIB_NAME_DRAW, data); + } + } + + public static void verbose(final String data) { + if (PRINT_VERBOSE) { Logger.verbose(LIB_NAME_DRAW, data); + } } - + + public static void warning(final String data) { + if (PRINT_WARNING) { + Logger.warning(LIB_NAME_DRAW, data); + } + } + + private Log() {} + } diff --git a/src/org/atriasoft/gameengine/map/MapVoxel.java b/src/org/atriasoft/gameengine/map/MapVoxel.java index a6d635a..e5393c9 100644 --- a/src/org/atriasoft/gameengine/map/MapVoxel.java +++ b/src/org/atriasoft/gameengine/map/MapVoxel.java @@ -25,8 +25,6 @@ import org.atriasoft.gameengine.components.ComponentTexture; import org.atriasoft.gameengine.components.ComponentTextures; import org.atriasoft.gameengine.engines.EngineLight; import org.atriasoft.gameengine.engines.EngineMap; -import org.atriasoft.gameengine.physics.PhysicBox; -import org.atriasoft.gameengine.physics.PhysicMapVoxel; public class MapVoxel extends EngineMap { //List listOfChunks = new ArrayList(); @@ -75,9 +73,9 @@ public class MapVoxel extends EngineMap { new Uri("DATA", "basicMaterial.frag"), (EngineLight)env.getEngine(EngineLight.ENGINE_NAME))); ComponentPhysics physics = new ComponentPhysics(false); - PhysicMapVoxel box = new PhysicMapVoxel(tmpVoxelChunk); - physics.addShape(box); - physics.setStaticObject(true); + //PhysicMapVoxel box = new PhysicMapVoxel(tmpVoxelChunk); + //physics.addShape(box); + //physics.setStaticObject(true); tmpEntity.addComponent(physics); this.env.addEntity(tmpEntity); diff --git a/src/org/atriasoft/gameengine/physics/ColisionPoints.java b/src/org/atriasoft/gameengine/physics/ColisionPoints.java deleted file mode 100644 index 9a70a00..0000000 --- a/src/org/atriasoft/gameengine/physics/ColisionPoints.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.atriasoft.gameengine.physics; - -import org.atriasoft.etk.math.Vector3f; - -public class ColisionPoints { - public Vector3f position; - public Vector3f force; - - public ColisionPoints(Vector3f position, Vector3f force) { - this.position = position; - this.force = force; - } -} diff --git a/src/org/atriasoft/gameengine/physics/Collision.java b/src/org/atriasoft/gameengine/physics/Collision.java deleted file mode 100644 index d91bab9..0000000 --- a/src/org/atriasoft/gameengine/physics/Collision.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.atriasoft.gameengine.physics; - -public class Collision { - public final ColisionPoints[] colisionPointLocal; - public final PhysicShape shapeRemote; - public final ColisionPoints[] colisionPointRemote; - public final boolean staticRemote; - public Collision(ColisionPoints[] colisionPointLocal, PhysicShape shapeRemote, - ColisionPoints[] colisionPointRemote, boolean staticRemote) { - super(); - this.colisionPointLocal = colisionPointLocal; - this.shapeRemote = shapeRemote; - this.colisionPointRemote = colisionPointRemote; - this.staticRemote = staticRemote; - } - -} diff --git a/src/org/atriasoft/gameengine/physics/GravityMap.java b/src/org/atriasoft/gameengine/physics/GravityMap.java deleted file mode 100644 index 7f1ce24..0000000 --- a/src/org/atriasoft/gameengine/physics/GravityMap.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.atriasoft.gameengine.physics; - -import org.atriasoft.etk.math.Vector3f; - -public abstract class GravityMap { - public abstract Vector3f getGravityAtPosition(Vector3f position); -} diff --git a/src/org/atriasoft/gameengine/physics/PhysicBox.java b/src/org/atriasoft/gameengine/physics/PhysicBox.java deleted file mode 100644 index d513263..0000000 --- a/src/org/atriasoft/gameengine/physics/PhysicBox.java +++ /dev/null @@ -1,112 +0,0 @@ -package org.atriasoft.gameengine.physics; - -import java.util.ArrayList; -import java.util.List; - -import org.atriasoft.etk.Color; -import org.atriasoft.etk.math.Matrix4f; -import org.atriasoft.etk.math.Transform3D; -import org.atriasoft.etk.math.Vector3f; -import org.atriasoft.gale.resource.ResourceColored3DObject; -import org.atriasoft.gameengine.internal.Log; - -public class PhysicBox extends PhysicShape { - // Box size property in X, Y and Z - private Vector3f size = new Vector3f(1, 1, 1); - - public PhysicBox() { - super(PhysicShapeType.BOX); - } - - public Vector3f getSize() { - return size; - } - - public void setSize(Vector3f size) { - this.size = size; - } - - @Override - public void updateAABB(Transform3D transformGlobal, PhysicCollisionAABB aabb) { - // store it, many time usefull... - this.transformGlobal = transformGlobal; - this.colisionPoints.clear(); - // TODO Auto-generated method stub - aabb.update(transformGlobal.multiply(this.transform.multiply(new Vector3f(this.size.x * 0.5f, this.size.y * 0.5f, this.size.z * 0.5f)))); - aabb.update(transformGlobal.multiply(this.transform.multiply(new Vector3f(-this.size.x * 0.5f, this.size.y * 0.5f, this.size.z * 0.5f)))); - aabb.update(transformGlobal.multiply(this.transform.multiply(new Vector3f(-this.size.x * 0.5f, -this.size.y * 0.5f, this.size.z * 0.5f)))); - aabb.update(transformGlobal.multiply(this.transform.multiply(new Vector3f(this.size.x * 0.5f, -this.size.y * 0.5f, this.size.z * 0.5f)))); - aabb.update(transformGlobal.multiply(this.transform.multiply(new Vector3f(this.size.x * 0.5f, this.size.y * 0.5f, -this.size.z * 0.5f)))); - aabb.update(transformGlobal.multiply(this.transform.multiply(new Vector3f(-this.size.x * 0.5f, this.size.y * 0.5f, -this.size.z * 0.5f)))); - aabb.update(transformGlobal.multiply(this.transform.multiply(new Vector3f(-this.size.x * 0.5f, -this.size.y * 0.5f, -this.size.z * 0.5f)))); - aabb.update(transformGlobal.multiply(this.transform.multiply(new Vector3f(this.size.x * 0.5f, -this.size.y * 0.5f, -this.size.z * 0.5f)))); - } - - // only needed for the narrow phase calculation ... - public Vector3f narrowPhaseGlobalPos; - public Vector3f narrowPhaseAxisX = new Vector3f(1, 0, 0); - public Vector3f narrowPhaseAxisY = new Vector3f(1, 0, 0); - public Vector3f narrowPhaseAxisZ = new Vector3f(1, 0, 0); - public Vector3f narrowPhaseHalfSize; - - @Override - public void updateForNarrowCollision(Transform3D transformGlobal) { - this.narrowPhaseGlobalPos = transformGlobal.multiply(this.transform.multiply(new Vector3f(0, 0, 0))); - this.narrowPhaseAxisX = transformGlobal.multiply(this.transform.multiply(new Vector3f(1, 0, 0))).less(this.narrowPhaseGlobalPos); - this.narrowPhaseAxisY = transformGlobal.multiply(this.transform.multiply(new Vector3f(0, 1, 0))).less(this.narrowPhaseGlobalPos); - this.narrowPhaseAxisZ = transformGlobal.multiply(this.transform.multiply(new Vector3f(0, 0, 1))).less(this.narrowPhaseGlobalPos); - this.narrowPhaseHalfSize = this.size.multiplyNew(0.5f); - } - - private void renderPoint(Vector3f subPosition, Transform3D transformGlobal, ResourceColored3DObject debugDrawProperty) { - Matrix4f transformation = transformGlobal.getOpenGLMatrix().multiplyNew(this.transform.getOpenGLMatrix()).multiply(Matrix4f.createMatrixTranslate(subPosition)); - - debugDrawProperty.drawSquare(new Vector3f(0.08f, 0.08f, 0.08f), transformation, new Color(0, 0, 1, 1)); - } - - private void renderPoint2(Vector3f subPosition, Transform3D transformGlobal, ResourceColored3DObject debugDrawProperty) { - Matrix4f transformation = transformGlobal.getOpenGLMatrix().multiplyNew(this.transform.getOpenGLMatrix()).multiply(Matrix4f.createMatrixTranslate(subPosition)); - debugDrawProperty.drawSquare(new Vector3f(0.05f, 0.05f, 0.05f), transformation, new Color(0, 1, 0, 1)); - - } - - private void renderPoint3(Vector3f subPosition, Transform3D transformGlobal, ResourceColored3DObject debugDrawProperty) { - Matrix4f transformation = transformGlobal.getOpenGLMatrix().multiplyNew(this.transform.getOpenGLMatrix()).multiply(Matrix4f.createMatrixTranslate(subPosition)); - debugDrawProperty.drawSquare(new Vector3f(0.05f, 0.05f, 0.05f), transformation, new Color(1, 1, 0, 1)); - - } - - private void renderPoint4(Vector3f subPosition, Vector3f force, ResourceColored3DObject debugDrawProperty) { - debugDrawProperty.drawSquare(new Vector3f(0.1f, 0.1f, 0.1f), Matrix4f.identity().multiply(Matrix4f.createMatrixTranslate(subPosition)), new Color(1, 0, 0, 1)); - List tmp = new ArrayList<>(); - tmp.add(new Vector3f(0,0,0)); - tmp.add(force); - debugDrawProperty.drawLine(tmp, new Color(1, 0, 0, 1), Matrix4f.identity().multiply(Matrix4f.createMatrixTranslate(subPosition)), true, false); - } - - @Override - public void renderDebug(Transform3D transformGlobal, ResourceColored3DObject debugDrawProperty) { - debugDrawProperty.drawSquare(this.size.multiplyNew(0.5f), this.transform.getOpenGLMatrix().multiplyNew(transformGlobal.getOpenGLMatrix()), new Color(0, 1, 0, 0.25f)); - Vector3f dimention = this.size.multiplyNew(0.5f); - renderPoint2(new Vector3f(+dimention.x, +dimention.y, +dimention.z), transformGlobal, debugDrawProperty); - renderPoint(new Vector3f(-dimention.x, +dimention.y, +dimention.z), transformGlobal, debugDrawProperty); - renderPoint(new Vector3f(+dimention.x, -dimention.y, +dimention.z), transformGlobal, debugDrawProperty); - renderPoint(new Vector3f(-dimention.x, -dimention.y, +dimention.z), transformGlobal, debugDrawProperty); - renderPoint(new Vector3f(+dimention.x, +dimention.y, -dimention.z), transformGlobal, debugDrawProperty); - renderPoint(new Vector3f(-dimention.x, +dimention.y, -dimention.z), transformGlobal, debugDrawProperty); - renderPoint(new Vector3f(+dimention.x, -dimention.y, -dimention.z), transformGlobal, debugDrawProperty); - renderPoint3(new Vector3f(-dimention.x, -dimention.y, -dimention.z), transformGlobal, debugDrawProperty); - for (Collision elem: this.colisionPoints) { - if (elem != null) { - if (elem.colisionPointLocal == null) { - Log.error("colision point must be set !!!"); - continue; - } - for (int iii = 0; iii < elem.colisionPointLocal.length; iii++) { - renderPoint4(elem.colisionPointLocal[iii].position, elem.colisionPointLocal[iii].force, debugDrawProperty); - } - } - } - } - -} diff --git a/src/org/atriasoft/gameengine/physics/PhysicCollisionAABB.java b/src/org/atriasoft/gameengine/physics/PhysicCollisionAABB.java deleted file mode 100644 index 1bd36e7..0000000 --- a/src/org/atriasoft/gameengine/physics/PhysicCollisionAABB.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.atriasoft.gameengine.physics; - -import org.atriasoft.etk.math.Vector3f; - -public class PhysicCollisionAABB { - public float minX; - public float minY; - public float minZ; - public float maxX; - public float maxY; - public float maxZ; - public PhysicCollisionAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) { - super(); - this.minX = minX; - this.minY = minY; - this.minZ = minZ; - this.maxX = maxX; - this.maxY = maxY; - this.maxZ = maxZ; - } - public boolean intersect(PhysicCollisionAABB other) { - if (this == other) { - return false; - } - if (minX > other.maxX) { - return false; - } - if (maxX < other.minX) { - return false; - } - if (minY > other.maxY) { - return false; - } - if (maxY < other.minY) { - return false; - } - if (minZ > other.maxZ) { - return false; - } - if (maxZ < other.minZ) { - return false; - } - return true; - } - public void update(Vector3f point) { - if (minX > point.x) { - minX = point.x; - } - if (maxX < point.x) { - maxX = point.x; - } - if (minY > point.y) { - minY = point.y; - } - if (maxY < point.y) { - maxY = point.y; - } - if (minZ > point.z) { - minZ = point.z; - } - if (maxZ < point.z) { - maxZ = point.z; - } - } - public Vector3f getMin() { - return new Vector3f(minX, minY, minZ); - } - public Vector3f getMax() { - return new Vector3f(maxX, maxY, maxZ); - } - public static PhysicCollisionAABB beforeCalculated() { - // TODO Auto-generated method stub - return new PhysicCollisionAABB(999999,999999,999999,-999999,-999999,-999999); - } -} - - diff --git a/src/org/atriasoft/gameengine/physics/PhysicMapVoxel.java b/src/org/atriasoft/gameengine/physics/PhysicMapVoxel.java deleted file mode 100644 index 16db627..0000000 --- a/src/org/atriasoft/gameengine/physics/PhysicMapVoxel.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.atriasoft.gameengine.physics; - -import org.atriasoft.etk.math.Transform3D; -import org.atriasoft.etk.math.Vector3f; -import org.atriasoft.gale.resource.ResourceColored3DObject; -import org.atriasoft.gameengine.map.VoxelChunk; - -public class PhysicMapVoxel extends PhysicShape { - // Box size property in X, Y and Z - private VoxelChunk chunk; - public PhysicMapVoxel(VoxelChunk chunk) { - super(PhysicShapeType.MAP_VOXEL); - this.chunk = chunk; - } - @Override - public void updateAABB(Transform3D transform, PhysicCollisionAABB aabb) { - if (this.chunk == null) { - return; - } - this.colisionPoints.clear(); - aabb.update(new Vector3f(this.chunk.getPosition().x,this.chunk.getPosition().y,this.chunk.getPosition().z)); - aabb.update(new Vector3f( - this.chunk.getPosition().x + VoxelChunk.VOXEL_CHUNK_SIZE, - this.chunk.getPosition().y + VoxelChunk.VOXEL_CHUNK_SIZE, - this.chunk.getPosition().z + VoxelChunk.VOXEL_CHUNK_SIZE)); - } - // only needed for the narrow phase calculation ... - private Vector3f narrowPhaseGlobalPos; - private Vector3f narrowPhaseAxisX = new Vector3f(1,0,0); - private Vector3f narrowPhaseAxisY = new Vector3f(1,0,0); - private Vector3f narrowPhaseAxisZ = new Vector3f(1,0,0); - private Vector3f narrowPhaseHalfSize = new Vector3f(0.5f,0.5f,0.5f); - @Override - public void updateForNarrowCollision(Transform3D transform) { - this.narrowPhaseGlobalPos = transform.multiply(this.transform.multiply(new Vector3f(0,0,0))); - } - @Override - public void renderDebug(Transform3D transform, ResourceColored3DObject debugDrawProperty) { - - - } -} diff --git a/src/org/atriasoft/gameengine/physics/PhysicShape.java b/src/org/atriasoft/gameengine/physics/PhysicShape.java deleted file mode 100644 index 4057f2d..0000000 --- a/src/org/atriasoft/gameengine/physics/PhysicShape.java +++ /dev/null @@ -1,101 +0,0 @@ -package org.atriasoft.gameengine.physics; - -import java.util.ArrayList; -import java.util.List; - -import org.atriasoft.etk.math.Quaternion; -import org.atriasoft.etk.math.Transform3D; -import org.atriasoft.etk.math.Vector3f; -import org.atriasoft.gale.resource.ResourceColored3DObject; - - - - -public abstract class PhysicShape { - - - - protected List colisionPoints = new ArrayList<>(); - // protected Quaternion quaternion; - // protected Vector3f origin; - protected Transform3D transform; - protected Transform3D transformGlobal; - protected float mass = 0; - protected final PhysicShapeType type; - - public PhysicShape(PhysicShapeType type) { - this.type = type; - this.transform = new Transform3D(); - // this.quaternion = Quaternion.identity(); - // this.origin = Vector3f.zero(); - this.mass = 0; - } - - public PhysicShape(PhysicShapeType type, Quaternion quaternion, Vector3f origin, float mass) { - this.type = type; - this.transform = new Transform3D(origin, quaternion); - // this.quaternion = quaternion; - // this.origin = origin; - this.mass = mass; - } - - public Quaternion getQuaternionFull() { - return transformGlobal.getOrientation().multiplyNew(transform.getOrientation()); - } - - public Quaternion getQuaternion() { - return transform.getOrientation(); - } - - public void setQuaternion(Quaternion quaternion) { - this.transform.setOrientation(quaternion); - } - - public Vector3f getOrigin() { - return this.transform.getPosition(); - } - - public void setOrigin(Vector3f origin) { - this.transform.setPosition(origin); - } - - public Transform3D getTransform() { - return transform; - } - - public void setTransform(Transform3D transform) { - this.transform = transform; - } - - public Transform3D getTransformGlobal() { - return transformGlobal; - } - - public void setTransformGlobal(Transform3D transform) { - this.transformGlobal = transform; - } - - public float getMass() { - return mass; - } - - public void setMass(float mass) { - this.mass = mass; - } - - public PhysicShapeType getType() { - return type; - } - - public void addColision(Collision colision) { - colisionPoints.add(colision); - } - - - public abstract void updateAABB(Transform3D transform, PhysicCollisionAABB aabb); - - public abstract void updateForNarrowCollision(Transform3D transform); - - public abstract void renderDebug(Transform3D transform, ResourceColored3DObject debugDrawProperty); - -} diff --git a/src/org/atriasoft/gameengine/physics/PhysicShapeType.java b/src/org/atriasoft/gameengine/physics/PhysicShapeType.java deleted file mode 100644 index f1c9ee2..0000000 --- a/src/org/atriasoft/gameengine/physics/PhysicShapeType.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.atriasoft.gameengine.physics; - -public enum PhysicShapeType { - UNKNOWN, - BOX, - CAPSULE, - CONE, - CONVEXHULL, - CYLINDER, - SPHERE, - CONCAVE, - MAP_VOXEL -} diff --git a/src/org/atriasoft/gameengine/physics/PhysicSphere.java b/src/org/atriasoft/gameengine/physics/PhysicSphere.java deleted file mode 100644 index 091e236..0000000 --- a/src/org/atriasoft/gameengine/physics/PhysicSphere.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.atriasoft.gameengine.physics; - -import org.atriasoft.etk.Color; -import org.atriasoft.etk.math.Transform3D; -import org.atriasoft.etk.math.Vector3f; -import org.atriasoft.gale.resource.ResourceColored3DObject; - -public class PhysicSphere extends PhysicShape { - // Box size property in X, Y and Z - private float size; - public PhysicSphere() { - super(PhysicShapeType.SPHERE); - } - public float getSize() { - return size; - } - public void setSize(float size) { - this.size = size; - } - @Override - public void updateAABB(Transform3D transform, PhysicCollisionAABB aabb) { - aabb.update(transform.multiply(this.transform.getPosition()).addNew(new Vector3f(this.size,0,0))); - aabb.update(transform.multiply(this.transform.getPosition()).addNew(new Vector3f(-this.size,0,0))); - aabb.update(transform.multiply(this.transform.getPosition()).addNew(new Vector3f(0,this.size,0))); - aabb.update(transform.multiply(this.transform.getPosition()).addNew(new Vector3f(0,-this.size,0))); - aabb.update(transform.multiply(this.transform.getPosition()).addNew(new Vector3f(0,0,this.size))); - aabb.update(transform.multiply(this.transform.getPosition()).addNew(new Vector3f(0,0,-this.size))); - } - @Override - public void updateForNarrowCollision(Transform3D transform) { - - } - @Override - public void renderDebug(Transform3D transform, ResourceColored3DObject debugDrawProperty) { - debugDrawProperty.drawSphere(this.size, 9, 9, this.transform.getOpenGLMatrix().multiplyNew(transform.getOpenGLMatrix()), new Color(0,1,0,1)); - - } -} \ No newline at end of file diff --git a/src/org/atriasoft/gameengine/physics/ToolCollisionOBBWithOBB.java b/src/org/atriasoft/gameengine/physics/ToolCollisionOBBWithOBB.java deleted file mode 100644 index ca8acc3..0000000 --- a/src/org/atriasoft/gameengine/physics/ToolCollisionOBBWithOBB.java +++ /dev/null @@ -1,377 +0,0 @@ -package org.atriasoft.gameengine.physics; - -import org.atriasoft.etk.math.Quaternion; -import org.atriasoft.etk.math.Vector3f; -import org.atriasoft.gameengine.internal.Log; -import org.atriasoft.gameengine.samples.LoxelEngine.LoxelApplication; - -// set the relevant elements of our oriented bounding box -class OBB { - public Vector3f position; - public Vector3f axisX; - public Vector3f axisY; - public Vector3f axisZ; - public Vector3f halfSize; - - public OBB() { - position = new Vector3f(); - axisX = new Vector3f(); - axisY = new Vector3f(); - axisZ = new Vector3f(); - halfSize = new Vector3f(); - } -}; - -public class ToolCollisionOBBWithOBB { - private ToolCollisionOBBWithOBB() {} - - // check if there's a separating plane in between the selected axes - private static boolean getSeparatingPlane(Vector3f rPos, Vector3f plane, OBB box1, OBB box2) { - return (Math.abs(rPos.dot(plane)) > (Math.abs(box1.axisX.multiplyNew(box1.halfSize.x).dot(plane)) + Math.abs(box1.axisY.multiplyNew(box1.halfSize.y).dot(plane)) - + Math.abs(box1.axisZ.multiplyNew(box1.halfSize.z).dot(plane)) + Math.abs(box2.axisX.multiplyNew(box2.halfSize.x).dot(plane)) - + Math.abs(box2.axisY.multiplyNew(box2.halfSize.y).dot(plane)) + Math.abs(box2.axisZ.multiplyNew(box2.halfSize.z).dot(plane)))); - } - - // test for separating planes in all 15 axes - private static boolean getCollision(OBB box1, OBB box2) { - Vector3f rPos = box2.position.lessNew(box1.position); - boolean ret = getSeparatingPlane(rPos, box1.axisX, box1, box2) || getSeparatingPlane(rPos, box1.axisY, box1, box2) || getSeparatingPlane(rPos, box1.axisZ, box1, box2) - || getSeparatingPlane(rPos, box2.axisX, box1, box2) || getSeparatingPlane(rPos, box2.axisY, box1, box2) || getSeparatingPlane(rPos, box2.axisZ, box1, box2) - || getSeparatingPlane(rPos, box1.axisX.cross(box2.axisX), box1, box2) || getSeparatingPlane(rPos, box1.axisX.cross(box2.axisY), box1, box2) - || getSeparatingPlane(rPos, box1.axisX.cross(box2.axisZ), box1, box2) || getSeparatingPlane(rPos, box1.axisY.cross(box2.axisX), box1, box2) - || getSeparatingPlane(rPos, box1.axisY.cross(box2.axisY), box1, box2) || getSeparatingPlane(rPos, box1.axisY.cross(box2.axisZ), box1, box2) - || getSeparatingPlane(rPos, box1.axisZ.cross(box2.axisX), box1, box2) || getSeparatingPlane(rPos, box1.axisZ.cross(box2.axisY), box1, box2) - || getSeparatingPlane(rPos, box1.axisZ.cross(box2.axisZ), box1, box2); - return !ret; - } -// -// // a quick test to see the code working -// public static void main(String[] args) { -// // create two obbs -// OBB aaa = new OBB(); -// OBB bbb = new OBB(); -// -// // set the first obb's properties -// aaa.position = new Vector3f(0.0f, 0.0f, 0.0f); // set its center position -// -// // set the half size -// aaa.halfSize = new Vector3f(10.0f, 1.0f, 1.0f); -// -// // set the axes orientation -// aaa.axisX = new Vector3f(1.0f, 0.0f, 0.0f); -// aaa.axisY = new Vector3f(0.0f, 1.0f, 0.0f); -// aaa.axisZ = new Vector3f(0.0f, 0.0f, 1.0f); -// -// // set the second obb's properties -// bbb.position = new Vector3f(20.0f, 0.0f, 0.0f); // set its center position -// -// // set the half size -// bbb.halfSize = new Vector3f(10.0f, 1.0f, 1.0f); -// -// // set the axes orientation -// bbb.axisX = new Vector3f(1.0f, 0.0f, 0.0f); -// bbb.axisY = new Vector3f(0.0f, 1.0f, 0.0f); -// bbb.axisZ = new Vector3f(0.0f, 0.0f, 1.0f); -// -// // run the code and get the result as a message -// if (getCollision(aaa, bbb)) { -// Log.info("Collision!!!"); -// } else { -// Log.info("NO Collision!!!"); -// } -// } - - // check if there's a separating plane in between the selected axes - private static boolean getSeparatingPlane222(Vector3f rPos, Vector3f plane, PhysicBox box1, PhysicBox box2) { - return (Math.abs(rPos.dot(plane)) > (Math.abs(box1.narrowPhaseAxisX.multiplyNew(box1.narrowPhaseHalfSize.x).dot(plane)) - + Math.abs(box1.narrowPhaseAxisY.multiplyNew(box1.narrowPhaseHalfSize.y).dot(plane)) + Math.abs(box1.narrowPhaseAxisZ.multiplyNew(box1.narrowPhaseHalfSize.z).dot(plane)) - + Math.abs(box2.narrowPhaseAxisX.multiplyNew(box2.narrowPhaseHalfSize.x).dot(plane)) + Math.abs(box2.narrowPhaseAxisY.multiplyNew(box2.narrowPhaseHalfSize.y).dot(plane)) - + Math.abs(box2.narrowPhaseAxisZ.multiplyNew(box2.narrowPhaseHalfSize.z).dot(plane)))); - } - - public static boolean testCollide(PhysicBox box1, PhysicBox box2) { - - Vector3f rPos = box2.narrowPhaseGlobalPos.lessNew(box1.narrowPhaseGlobalPos); - boolean ret = getSeparatingPlane222(rPos, box1.narrowPhaseAxisX, box1, box2) || getSeparatingPlane222(rPos, box1.narrowPhaseAxisY, box1, box2) - || getSeparatingPlane222(rPos, box1.narrowPhaseAxisZ, box1, box2) || getSeparatingPlane222(rPos, box2.narrowPhaseAxisX, box1, box2) - || getSeparatingPlane222(rPos, box2.narrowPhaseAxisY, box1, box2) || getSeparatingPlane222(rPos, box2.narrowPhaseAxisZ, box1, box2) - || getSeparatingPlane222(rPos, box1.narrowPhaseAxisX.cross(box2.narrowPhaseAxisX), box1, box2) - || getSeparatingPlane222(rPos, box1.narrowPhaseAxisX.cross(box2.narrowPhaseAxisY), box1, box2) - || getSeparatingPlane222(rPos, box1.narrowPhaseAxisX.cross(box2.narrowPhaseAxisZ), box1, box2) - || getSeparatingPlane222(rPos, box1.narrowPhaseAxisY.cross(box2.narrowPhaseAxisX), box1, box2) - || getSeparatingPlane222(rPos, box1.narrowPhaseAxisY.cross(box2.narrowPhaseAxisY), box1, box2) - || getSeparatingPlane222(rPos, box1.narrowPhaseAxisY.cross(box2.narrowPhaseAxisZ), box1, box2) - || getSeparatingPlane222(rPos, box1.narrowPhaseAxisZ.cross(box2.narrowPhaseAxisX), box1, box2) - || getSeparatingPlane222(rPos, box1.narrowPhaseAxisZ.cross(box2.narrowPhaseAxisY), box1, box2) - || getSeparatingPlane222(rPos, box1.narrowPhaseAxisZ.cross(box2.narrowPhaseAxisZ), box1, box2); - return !ret; - } - - public static void getCollidePoints(PhysicBox box1, boolean isStatic1, PhysicBox box2, boolean isStatic2) { - // Log.info("Try to calculare reverse force ........"); - Vector3f rPos1 = box1.narrowPhaseGlobalPos.lessNew(box2.narrowPhaseGlobalPos); - Vector3f rPos2 = box2.narrowPhaseGlobalPos.lessNew(box1.narrowPhaseGlobalPos); - Quaternion quat1 = box1.getQuaternionFull(); - Quaternion quat2 = box2.getQuaternionFull(); - // Step 1: set the Box 2 in the repere of the Box 1: - Quaternion quatTransfer1 = Quaternion.diff(quat1, quat2); - Quaternion quatTransfer2 = Quaternion.diff(quat2, quat1); - // quatTransfer.normalize(); - - // LoxelApplication.relativeTest = quatTransfer; - // Vector3f tmp = rPos.addNew(new Vector3f(0,0,14)); - // LoxelApplication.relativeTestPos.getTransform().setPosition(tmp); - // LoxelApplication.relativeTestPos.getTransform().setOrientation(quatTransfer); - // LoxelApplication.boxTest.setSize(box1.getSize()); - // Log.info("" + rPos + quatTransfer1); - // /*res = */getCollidePointsAABBCenteredWithOBB(box1.narrowPhaseHalfSize, box2.narrowPhaseHalfSize, quatTransfer, rPos); - /* res = transfert in generic plan the new res ... */ - // test origin AABB with OBB collision - // Step 2: set the Box 1 in the repere of the Box 2: - // test origin AABB with OBB collision - // tmp = rPos.addNew(new Vector3f(0,0,14)); - LoxelApplication.testRpos = quat2.inverseNew().getMatrix4().multiply(rPos1); - LoxelApplication.testQTransfert = quatTransfer2; - LoxelApplication.box1HalfSize = box2.narrowPhaseHalfSize; - LoxelApplication.box2HalfSize = box1.narrowPhaseHalfSize; - - // LoxelApplication.relativeTestPos.getTransform().setPosition(tmp); - // LoxelApplication.relativeTestPos.getTransform().setOrientation(quatTransfer); - // LoxelApplication.boxTest.setSize(box1.getSize()); - // foinctionne avec la box qui n'est pas orienter... - // getCollidePointsAABBCenteredWithOBB(box2.narrowPhaseHalfSize, box1.narrowPhaseHalfSize, quatTransfer2, rPos1); - // fonctionne quand le block est trourner de 90% petit pb de positionnement en hauteur.... - // getCollidePointsAABBCenteredWithOBB(box2.narrowPhaseHalfSize, box1.narrowPhaseHalfSize, quatTransfer2, quat2.multiply(rPos2)); - ColisionPoints[] collide1 = getCollidePointsAABBCenteredWithOBB(box2.narrowPhaseHalfSize, box1.narrowPhaseHalfSize, quatTransfer2, quat2.inverseNew().getMatrix4().multiply(rPos1)); - // transfer detection point collision in global environement: - if (collide1 != null) { - for (int iii = 0; iii < collide1.length; iii++) { - collide1[iii].position = quat1.multiply(collide1[iii].position).add(box1.narrowPhaseGlobalPos); - collide1[iii].force = quat2.multiply(collide1[iii].force);//.add(box1.narrowPhaseGlobalPos); - } - } - /* res = trensfert in generic plan the new res ... */ - - LoxelApplication.testRpos = quat1.inverseNew().getMatrix4().multiply(rPos2); - LoxelApplication.testQTransfert = quatTransfer1; - LoxelApplication.box1HalfSize = box1.narrowPhaseHalfSize; - LoxelApplication.box2HalfSize = box2.narrowPhaseHalfSize; - ColisionPoints[] collide2 = getCollidePointsAABBCenteredWithOBB(box1.narrowPhaseHalfSize, box2.narrowPhaseHalfSize, quatTransfer1, quat1.inverseNew().getMatrix4().multiply(rPos2)); - if (collide2 != null) { - for (int iii = 0; iii < collide2.length; iii++) { - collide2[iii].position = quat2.multiply(collide2[iii].position).add(box2.narrowPhaseGlobalPos); - collide2[iii].force = quat1.multiply(collide2[iii].force);//.add(box2.narrowPhaseGlobalPos); - } - } - // add only if NOT static, when static no colision is performed - if (true) { //!isStatic1) { - Collision colision = new Collision(collide1, box2, collide2, isStatic2); - box1.addColision(colision); - } - if (true) { //!isStatic1) { - Collision colision = new Collision(collide2, box1, collide1, isStatic1); - box2.addColision(colision); - } - - } - - public static ColisionPoints[] getCollidePointsAABBCenteredWithOBB(Vector3f box1HalfSize, Vector3f box2HalfSize, Quaternion box2Orientation, Vector3f box2Position) { - - // point in AABB - Vector3f topBackRight = box2Orientation.multiply(new Vector3f(+box2HalfSize.x, +box2HalfSize.y, +box2HalfSize.z)).add(box2Position); - Vector3f topBackLeft = box2Orientation.multiply(new Vector3f(-box2HalfSize.x, +box2HalfSize.y, +box2HalfSize.z)).add(box2Position); - Vector3f topFrontRight = box2Orientation.multiply(new Vector3f(+box2HalfSize.x, -box2HalfSize.y, +box2HalfSize.z)).add(box2Position); - Vector3f topFrontLeft = box2Orientation.multiply(new Vector3f(-box2HalfSize.x, -box2HalfSize.y, +box2HalfSize.z)).add(box2Position); - Vector3f bottomBackRight = box2Orientation.multiply(new Vector3f(+box2HalfSize.x, +box2HalfSize.y, -box2HalfSize.z)).add(box2Position); - Vector3f bottomBackLeft = box2Orientation.multiply(new Vector3f(-box2HalfSize.x, +box2HalfSize.y, -box2HalfSize.z)).add(box2Position); - Vector3f bottomFrontRight = box2Orientation.multiply(new Vector3f(+box2HalfSize.x, -box2HalfSize.y, -box2HalfSize.z)).add(box2Position); - Vector3f bottomFrontLeft = box2Orientation.multiply(new Vector3f(-box2HalfSize.x, -box2HalfSize.y, -box2HalfSize.z)).add(box2Position); - LoxelApplication.testPoints.clear(); - LoxelApplication.testPoints.add(topBackRight); - LoxelApplication.testPoints.add(topBackLeft); - LoxelApplication.testPoints.add(topFrontRight); - LoxelApplication.testPoints.add(topFrontLeft); - LoxelApplication.testPoints.add(bottomBackRight); - LoxelApplication.testPoints.add(bottomBackLeft); - LoxelApplication.testPoints.add(bottomFrontRight); - LoxelApplication.testPoints.add(bottomFrontLeft); - LoxelApplication.testPointsBox.clear(); - LoxelApplication.testPointsBox.add(new Vector3f(+box1HalfSize.x, +box1HalfSize.y, +box1HalfSize.z)); - LoxelApplication.testPointsBox.add(new Vector3f(-box1HalfSize.x, +box1HalfSize.y, +box1HalfSize.z)); - LoxelApplication.testPointsBox.add(new Vector3f(+box1HalfSize.x, -box1HalfSize.y, +box1HalfSize.z)); - LoxelApplication.testPointsBox.add(new Vector3f(-box1HalfSize.x, -box1HalfSize.y, +box1HalfSize.z)); - LoxelApplication.testPointsBox.add(new Vector3f(+box1HalfSize.x, +box1HalfSize.y, -box1HalfSize.z)); - LoxelApplication.testPointsBox.add(new Vector3f(-box1HalfSize.x, +box1HalfSize.y, -box1HalfSize.z)); - LoxelApplication.testPointsBox.add(new Vector3f(+box1HalfSize.x, -box1HalfSize.y, -box1HalfSize.z)); - LoxelApplication.testPointsBox.add(new Vector3f(-box1HalfSize.x, -box1HalfSize.y, -box1HalfSize.z)); - Vector3f insideTopBackRight = pointDistanceInAABB(box1HalfSize, topBackRight); - Vector3f insideTopBackLeft = pointDistanceInAABB(box1HalfSize, topBackLeft); - Vector3f insideTopFrontRight = pointDistanceInAABB(box1HalfSize, topFrontRight); - Vector3f insideTopFrontLeft = pointDistanceInAABB(box1HalfSize, topFrontLeft); - Vector3f insideBottomBackRight = pointDistanceInAABB(box1HalfSize, bottomBackRight); - Vector3f insideBottomBackLeft = pointDistanceInAABB(box1HalfSize, bottomBackLeft); - Vector3f insideBottomFrontRight = pointDistanceInAABB(box1HalfSize, bottomFrontRight); - Vector3f insideBottomFrontLeft = pointDistanceInAABB(box1HalfSize, bottomFrontLeft); - LoxelApplication.testPointsCollide.clear(); - LoxelApplication.testPointsCollide.add(insideTopBackRight == null ? false : true); - LoxelApplication.testPointsCollide.add(insideTopBackLeft == null ? false : true); - LoxelApplication.testPointsCollide.add(insideTopFrontRight == null ? false : true); - LoxelApplication.testPointsCollide.add(insideTopFrontLeft == null ? false : true); - LoxelApplication.testPointsCollide.add(insideBottomBackRight == null ? false : true); - LoxelApplication.testPointsCollide.add(insideBottomBackLeft == null ? false : true); - LoxelApplication.testPointsCollide.add(insideBottomFrontRight == null ? false : true); - LoxelApplication.testPointsCollide.add(insideBottomFrontLeft == null ? false : true); - int count = 0; - if (insideTopBackRight != null) { - count++; - } - if (insideTopBackLeft != null) { - count++; - } - if (insideTopFrontRight != null) { - count++; - } - if (insideTopFrontLeft != null) { - count++; - } - if (insideBottomBackRight != null) { - count++; - } - if (insideBottomBackLeft != null) { - count++; - } - if (insideBottomFrontRight != null) { - count++; - } - if (insideBottomFrontLeft != null) { - count++; - } - ColisionPoints[] out = new ColisionPoints[count]; - count = 0; - if (insideTopBackRight != null) { - out[count] = new ColisionPoints(new Vector3f(+box2HalfSize.x, +box2HalfSize.y, +box2HalfSize.z), insideTopBackRight); - count++; - } - if (insideTopBackLeft != null) { - out[count] = new ColisionPoints(new Vector3f(-box2HalfSize.x, +box2HalfSize.y, +box2HalfSize.z), insideTopBackLeft); - count++; - } - if (insideTopFrontRight != null) { - out[count] = new ColisionPoints(new Vector3f(+box2HalfSize.x, -box2HalfSize.y, +box2HalfSize.z), insideTopFrontRight); - count++; - } - if (insideTopFrontLeft != null) { - out[count] = new ColisionPoints(new Vector3f(-box2HalfSize.x, -box2HalfSize.y, +box2HalfSize.z), insideTopFrontLeft); - count++; - } - if (insideBottomBackRight != null) { - out[count] = new ColisionPoints(new Vector3f(+box2HalfSize.x, +box2HalfSize.y, -box2HalfSize.z), insideBottomBackRight); - count++; - } - if (insideBottomBackLeft != null) { - out[count] = new ColisionPoints(new Vector3f(-box2HalfSize.x, +box2HalfSize.y, -box2HalfSize.z), insideBottomBackLeft); - count++; - } - if (insideBottomFrontRight != null) { - out[count] = new ColisionPoints(new Vector3f(+box2HalfSize.x, -box2HalfSize.y, -box2HalfSize.z), insideBottomFrontRight); - count++; - } - if (insideBottomFrontLeft != null) { - out[count] = new ColisionPoints(new Vector3f(-box2HalfSize.x, -box2HalfSize.y, -box2HalfSize.z), insideBottomFrontLeft); - count++; - } - if (count != 0) { - // Find a point inside the BOX ... - /* - Log.info("Detect point inside ... " + insideTopBackRight + " " + insideTopBackLeft + " " + insideTopFrontRight + " " + insideTopFrontLeft + " " + insideBottomBackRight + " " - + insideBottomBackLeft + " " + insideBottomFrontRight + " " + insideBottomFrontLeft); - */ - return out; - } - // line in AABB - // TODO: - // Log.info("Need to detect line inside ..."); // pas tot a fait... si ca colisione déja avec un point de l'autre ... - return null; - } - - public static boolean pointInAABB(Vector3f halfSize, Vector3f point) { - if (point.x > -halfSize.x && point.x < halfSize.x && point.y > -halfSize.y && point.y < halfSize.y && point.z > -halfSize.z && point.z < halfSize.z) { - return true; - } - return false; - } - - public static Vector3f pointDistanceInAABB(Vector3f halfSize, Vector3f point) { - Vector3f out = new Vector3f(); - if (point.x < 0) { - if (point.x > -halfSize.x) { - - out.x = -(halfSize.x + point.x); - - //out.x = -halfSize.x - point.x; - //out.x = -halfSize.x + point.x; - //out.x = + point.x; - } else { - return null; - } - } else { - if (point.x < halfSize.x) { - //out.x = halfSize.x + point.x; - out.x = halfSize.x - point.x; - //out.x = - point.x; - } else { - return null; - } - } - if (point.y < 0) { - if (point.y > -halfSize.y) { - out.y = -halfSize.y - point.y; - //out.y = -halfSize.y + point.y; - //out.y = point.y; - } else { - return null; - } - } else { - if (point.y < halfSize.y) { - //out.y = halfSize.y + point.y; - out.y = halfSize.y - point.y; - //out.y = - point.y; - } else { - return null; - } - } - if (point.z < 0) { - if (point.z > -halfSize.z) { - out.z = -halfSize.z - point.z; - //out.z = -halfSize.z + point.z; - //out.z = + point.z; - } else { - return null; - } - } else { - if (point.z < halfSize.z) { - //out.z = halfSize.z + point.z; - out.z = halfSize.z - point.z; - //out.z = - point.z; - } else { - return null; - } - } - if (Math.abs(out.x) < Math.abs(out.y)) { - out.y = 0; - if (Math.abs(out.x) < Math.abs(out.z)) { - out.z = 0; - return out; - } - out.x = 0; - return out; - } - out.x = 0; - if (Math.abs(out.y) < Math.abs(out.z)) { - out.z = 0; - return out; - } - out.y = 0; - return out; - } -} diff --git a/src/org/atriasoft/gameengine/physics/shape/Box.java b/src/org/atriasoft/gameengine/physics/shape/Box.java new file mode 100644 index 0000000..5dfacb5 --- /dev/null +++ b/src/org/atriasoft/gameengine/physics/shape/Box.java @@ -0,0 +1,36 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license MPL v2.0 (see license file) + */ +package org.atriasoft.gameengine.physics.shape; + +import org.atriasoft.etk.math.Vector3f; + +public class Box extends Shape { + private Vector3f size; // Box size property in X, Y and Z + + @Override + public boolean parse(String _line) { + /* + if (super.parse(_line) == true) { + return true; + } + if(strncmp(_line, "half-extents:", 13) == 0) { + sscanf(&_line[13], "%f %f %f", &size.m_floats[0], &size.m_floats[1], &size.m_floats[2] ); + EGE_VERBOSE(" halfSize=" << size); + return true; + } + */ + return false; + } + + public Vector3f getSize() { + return size; + } + + public void setSize(Vector3f size) { + this.size = size; + } +} + diff --git a/src/org/atriasoft/gameengine/physics/shape/Capsule.java b/src/org/atriasoft/gameengine/physics/shape/Capsule.java new file mode 100644 index 0000000..d67b089 --- /dev/null +++ b/src/org/atriasoft/gameengine/physics/shape/Capsule.java @@ -0,0 +1,46 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license MPL v2.0 (see license file) + */ +package org.atriasoft.gameengine.physics.shape; + +import org.atriasoft.etk.math.Vector3f; + +public class Capsule extends Shape { + private float radius = 1; + private float size = 1; + @Override + public boolean parse(String _line) { + /* + if (super.parse(_line) == true) { + return true; + } + if(strncmp(_line, "radius:", 7) == 0) { + sscanf(&_line[7], "%f", &m_radius ); + EGE_VERBOSE(" radius=" << m_radius); + return true; + } + if(strncmp(_line, "size:", 5) == 0) { + sscanf(&_line[5], "%f", &m_size ); + EGE_VERBOSE(" height=" << m_size); + return true; + } + */ + return false; + } + public float getRadius() { + return radius; + } + public void setRadius(float radius) { + this.radius = radius; + } + + public float getSize() { + return size; + } + public void setSize(float size) { + this.size = size; + } +} + diff --git a/src/org/atriasoft/gameengine/physics/shape/Concave.java b/src/org/atriasoft/gameengine/physics/shape/Concave.java new file mode 100644 index 0000000..fa2034c --- /dev/null +++ b/src/org/atriasoft/gameengine/physics/shape/Concave.java @@ -0,0 +1,62 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license MPL v2.0 (see license file) + */ +package org.atriasoft.gameengine.physics.shape; + +import java.util.ArrayList; +import java.util.List; + +import org.atriasoft.etk.math.Vector3f; +import org.atriasoft.gameengine.internal.Log; + +public class Concave extends Shape { + private List vertexes = new ArrayList<>(); + + private final List indices = new ArrayList<>(); + + public void addTriangle(final List index) { + /* + if (m_indices.size() == 0) { + m_indices = _index; + return; + } + */ + if (index.size() % 3 != 0) { + Log.error("wrong number of faces : " + index.size() + " ==> not a multiple of 3"); + return; + } + for (final Integer it : index) { + this.indices.add(it); + } + } + + public void clear() { + this.vertexes.clear(); + this.indices.clear(); + } + + public List getIndices() { + return this.indices; + } + + public List getVertex() { + return this.vertexes; + } + + @Override + public boolean parse(final String _line) { + /* + if (super.parse(_line) == true) { + return true; + } + // TODO ... + */ + return false; + } + + public void setListOfVertex(final List vertexes) { + this.vertexes = vertexes; + } +} diff --git a/src/org/atriasoft/gameengine/physics/shape/Cone.java b/src/org/atriasoft/gameengine/physics/shape/Cone.java new file mode 100644 index 0000000..861ff8d --- /dev/null +++ b/src/org/atriasoft/gameengine/physics/shape/Cone.java @@ -0,0 +1,46 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license MPL v2.0 (see license file) + */ +package org.atriasoft.gameengine.physics.shape; + +import org.atriasoft.etk.math.Vector3f; + +public class Cone extends Shape { + private float radius = 1; + private float size = 1; + @Override + public boolean parse(String _line) { + /* + if (super.parse(_line) == true) { + return true; + } + if(strncmp(_line, "radius:", 7) == 0) { + sscanf(&_line[7], "%f", &m_radius ); + EGE_VERBOSE(" radius=" << m_radius); + return true; + } + if(strncmp(_line, "size:", 5) == 0) { + sscanf(&_line[5], "%f", &m_size ); + EGE_VERBOSE(" size=" << m_size); + return true; + } + */ + return false; + } + public float getRadius() { + return radius; + } + public void setRadius(float radius) { + this.radius = radius; + } + public float getSize() { + return size; + } + public void setSize(float size) { + this.size = size; + } + +} + diff --git a/src/org/atriasoft/gameengine/physics/shape/ConvexHull.java b/src/org/atriasoft/gameengine/physics/shape/ConvexHull.java new file mode 100644 index 0000000..16404e6 --- /dev/null +++ b/src/org/atriasoft/gameengine/physics/shape/ConvexHull.java @@ -0,0 +1,65 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license MPL v2.0 (see license file) + */ +package org.atriasoft.gameengine.physics.shape; + +import java.util.ArrayList; +import java.util.List; + +import org.atriasoft.etk.math.Vector3f; + +public class ConvexHull extends Shape { + private Vector3f scale = new Vector3f(1, 1, 1); + private List points = new ArrayList<>(); + @Override + public boolean parse(String _line) { + /* + if (super.parse(_line) == true) { + return true; + } + if(strncmp(_line, "points:", 6) == 0) { + //EGE_DEBUG("convex hull point parsing " << _line); + char* base = (char*)(&_line[6]); + char* tmp= strchr(base, '|'); + vec3 pos(0,0,0); + while (tmp != null) { + *tmp = '\0'; + sscanf(base, "%f %f %f", &pos.m_floats[0], &pos.m_floats[1], &pos.m_floats[2] ); + m_points.pushBack(pos); + base = tmp+1; + tmp= strchr(base, '|'); + } + sscanf(base, "%f %f %f", &pos.m_floats[0], &pos.m_floats[1], &pos.m_floats[2] ); + m_points.pushBack(pos); + / * + for (int32_t iii=0; iii(); + } else if (name == "sphere") { + tmpp = ememory::makeShared(); + } else if (name == "cone") { + tmpp = ememory::makeShared(); + } else if (name == "cylinder") { + tmpp = ememory::makeShared(); + } else if (name == "capsule") { + tmpp = ememory::makeShared(); + } else if (name == "convexhull") { + tmpp = ememory::makeShared(); + } else if (name == "autoconcave") { + tmpp = ememory::makeShared(); + } else { + EGE_ERROR("Create an unknow element : '" << _name << "' availlable : [BOX,SPHERE,CONE,CYLINDER,CAPSULE,CONVEXHULL,autoConcave]"); + return null; + } + if (tmpp == null) { + EGE_ERROR("Allocation error for physical element : '" << _name << "'"); + } + return tmpp; +} +*/ diff --git a/src/org/atriasoft/gameengine/physics/shape/Sphere.java b/src/org/atriasoft/gameengine/physics/shape/Sphere.java new file mode 100644 index 0000000..70458bf --- /dev/null +++ b/src/org/atriasoft/gameengine/physics/shape/Sphere.java @@ -0,0 +1,34 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license MPL v2.0 (see license file) + */ +package org.atriasoft.gameengine.physics.shape; + +import org.atriasoft.etk.math.Vector3f; + +public class Sphere extends Shape { + private float radius = 1; + @Override + public boolean parse(String _line) { + /* + if (super.parse(_line) == true) { + return true; + } + if(strncmp(_line, "radius:", 7) == 0) { + sscanf(&_line[7], "%f", &m_radius ); + EGE_VERBOSE(" radius=" << m_radius); + return true; + } + */ + return false; + } + public float getRadius() { + return radius; + } + public void setRadius(float radius) { + this.radius = radius; + } + +} + diff --git a/src/org/atriasoft/gameengine/samples/LoxelEngine/LoxelApplication.java b/src/org/atriasoft/gameengine/samples/LoxelEngine/LoxelApplication.java index bf012c4..dc89d59 100644 --- a/src/org/atriasoft/gameengine/samples/LoxelEngine/LoxelApplication.java +++ b/src/org/atriasoft/gameengine/samples/LoxelEngine/LoxelApplication.java @@ -15,6 +15,11 @@ import org.atriasoft.gale.Gale; import org.atriasoft.gale.backend3d.OpenGL; import org.atriasoft.gale.backend3d.OpenGL.Flag; import org.atriasoft.gale.context.Context; +import org.atriasoft.gale.key.KeyKeyboard; +import org.atriasoft.gale.key.KeySpecial; +import org.atriasoft.gale.key.KeyStatus; +import org.atriasoft.gale.key.KeyType; +import org.atriasoft.gale.resource.ResourceColored3DObject; import org.atriasoft.gameengine.ControlCameraPlayer; import org.atriasoft.gameengine.Entity; import org.atriasoft.gameengine.Environement; @@ -37,27 +42,12 @@ import org.atriasoft.gameengine.components.ComponentStaticMesh; import org.atriasoft.gameengine.components.ComponentTexture; import org.atriasoft.gameengine.engines.EngineLight; import org.atriasoft.gameengine.map.MapVoxel; -import org.atriasoft.gameengine.physics.PhysicBox; +import org.atriasoft.gameengine.physics.shape.Box; import org.atriasoft.gameengine.tools.MeshGenerator; -import org.atriasoft.gale.key.KeyKeyboard; -import org.atriasoft.gale.key.KeySpecial; -import org.atriasoft.gale.key.KeyStatus; -import org.atriasoft.gale.key.KeyType; -import org.atriasoft.gale.resource.ResourceColored3DObject; public class LoxelApplication extends Application { - private Environement env; - private ComponentPosition objectPosition; - private Quaternion basicRotation = Quaternion.identity(); - private Quaternion basicRotation2 = Quaternion.identity(); - private boolean creationDone; - private ControlCameraPlayer simpleControl; - private ComponentPosition lightPosition; - private float angleLight = 0; - private MapVoxel map; - private ComponentPlayer objectPlayer; -// public static ComponentPosition relativeTestPos; -// public static PhysicBox boxTest; + // public static ComponentPosition relativeTestPos; + // public static Box boxTest; public static List testPoints = new ArrayList(); public static List testPointsBox = new ArrayList(); public static List testPointsCollide = new ArrayList(); @@ -65,280 +55,260 @@ public class LoxelApplication extends Application { public static Quaternion testQTransfert; public static Vector3f box1HalfSize; public static Vector3f box2HalfSize; + private Environement env; + private ComponentPosition objectPosition; + private final Quaternion basicRotation = Quaternion.identity(); + private final Quaternion basicRotation2 = Quaternion.identity(); + private boolean creationDone; + private ControlCameraPlayer simpleControl; + private ComponentPosition lightPosition; + private float angleLight = 0; + private MapVoxel map; + private ComponentPlayer objectPlayer; private ResourceColored3DObject debugDrawProperty; - public LoxelApplication(){ - creationDone = false; + + public LoxelApplication() { + this.creationDone = false; } + @Override - public void onCreate(Context context) { + public void onCreate(final Context context) { // set the system global max speed - ComponentPhysics.globalMaxSpeed = 3; - Gale.getContext().grabPointerEvents(true, new Vector2f(0,0)); - env = new Environement(); + //ComponentPhysics.globalMaxSpeed = 3; + Gale.getContext().grabPointerEvents(true, new Vector2f(0, 0)); + this.env = new Environement(); this.canDraw = true; setSize(new Vector2f(1500, 1500)); setTitle("Loxel sample"); - map = new MapVoxel(this.env); -// this.env.addEngine(map); -// map.init(); - - // simple sun to have a global light ... - Entity globalGravity = new Entity(this.env); - globalGravity.addComponent(new ComponentGravityStatic(new Vector3f(0,0,-1))); - env.addEntity(globalGravity); + this.map = new MapVoxel(this.env); + // this.env.addEngine(map); + // map.init(); // simple sun to have a global light ... - Entity sun = new Entity(this.env); - sun.addComponent(new ComponentPosition(new Transform3D(new Vector3f(1000,1000,1000)))); - sun.addComponent(new ComponentLightSun(new Light(new Vector3f(0.4f,0.4f,0.4f), new Vector3f(0,0,0), new Vector3f(0.8f,0,0)))); - env.addEntity(sun); - + final Entity globalGravity = new Entity(this.env); + globalGravity.addComponent(new ComponentGravityStatic(new Vector3f(0, 0, -1))); + this.env.addEntity(globalGravity); + + // simple sun to have a global light ... + final Entity sun = new Entity(this.env); + sun.addComponent(new ComponentPosition(new Transform3D(new Vector3f(1000, 1000, 1000)))); + sun.addComponent(new ComponentLightSun(new Light(new Vector3f(0.4f, 0.4f, 0.4f), new Vector3f(0, 0, 0), new Vector3f(0.8f, 0, 0)))); + this.env.addEntity(sun); + // add a cube to show where in the light ... - Entity localLight = new Entity(this.env); - lightPosition = new ComponentPosition(new Transform3D(new Vector3f(-10,-10,17))); -// localLight.addComponent(lightPosition); -// localLight.addComponent(new ComponentStaticMesh(new Uri("RES", "cube.obj"))); -// localLight.addComponent(new ComponentTexture(new Uri("RES", "grass.png"))); -// localLight.addComponent(new ComponentLight(new Light(new Vector3f(0,1,0), new Vector3f(0,0,0), new Vector3f(0.8f,0.03f,0.002f)))); -// localLight.addComponent(new ComponentRenderTexturedStaticMesh( -// new Uri("DATA", "basic.vert"), -// new Uri("DATA", "basic.frag"))); -// env.addEntity(localLight); + final Entity localLight = new Entity(this.env); + this.lightPosition = new ComponentPosition(new Transform3D(new Vector3f(-10, -10, 17))); + // localLight.addComponent(lightPosition); + // localLight.addComponent(new ComponentStaticMesh(new Uri("RES", "cube.obj"))); + // localLight.addComponent(new ComponentTexture(new Uri("RES", "grass.png"))); + // localLight.addComponent(new ComponentLight(new Light(new Vector3f(0,1,0), new Vector3f(0,0,0), new Vector3f(0.8f,0.03f,0.002f)))); + // localLight.addComponent(new ComponentRenderTexturedStaticMesh( + // new Uri("DATA", "basic.vert"), + // new Uri("DATA", "basic.frag"))); + // env.addEntity(localLight); { // add a cube to test collision ... - Entity localBox = new Entity(this.env); - localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(-2,-2,14)))); + final Entity localBox = new Entity(this.env); + localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(-2, -2, 14)))); localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); - localBox.addComponent(new ComponentLight(new Light(new Vector3f(0,1,0), new Vector3f(0,0,0), new Vector3f(0.8f,0.03f,0.002f)))); - localBox.addComponent(new ComponentRenderTexturedStaticMesh( - new Uri("DATA", "basic.vert"), - new Uri("DATA", "basic.frag"))); - ComponentPhysics physics2 = new ComponentPhysics(true); - PhysicBox box2 = new PhysicBox(); - box2.setSize(new Vector3f(1,1,1)); - box2.setOrigin(new Vector3f(0,0,0)); + localBox.addComponent(new ComponentLight(new Light(new Vector3f(0, 1, 0), new Vector3f(0, 0, 0), new Vector3f(0.8f, 0.03f, 0.002f)))); + localBox.addComponent(new ComponentRenderTexturedStaticMesh(new Uri("DATA", "basic.vert"), new Uri("DATA", "basic.frag"))); + final ComponentPhysics physics2 = new ComponentPhysics(this.env); + final Box box2 = new Box(); + box2.setSize(new Vector3f(1, 1, 1)); + box2.setOrigin(new Vector3f(0, 0, 0)); box2.setMass(1); physics2.addShape(box2); localBox.addComponent(physics2); - env.addEntity(localBox); + this.env.addEntity(localBox); } { // add a cube to test collision ... - Entity localBox = new Entity(this.env); - localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(0,4,12.5f)))); + final Entity localBox = new Entity(this.env); + localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(0, 4, 12.5f)))); localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); - localBox.addComponent(new ComponentLight(new Light(new Vector3f(0,1,0), new Vector3f(0,0,0), new Vector3f(0.8f,0.03f,0.002f)))); - localBox.addComponent(new ComponentRenderTexturedStaticMesh( - new Uri("DATA", "basic.vert"), - new Uri("DATA", "basic.frag"))); - ComponentPhysics physics2 = new ComponentPhysics(true); - PhysicBox box2 = new PhysicBox(); - box2.setSize(new Vector3f(1,1,1)); - box2.setOrigin(new Vector3f(0,0,0)); + localBox.addComponent(new ComponentLight(new Light(new Vector3f(0, 1, 0), new Vector3f(0, 0, 0), new Vector3f(0.8f, 0.03f, 0.002f)))); + localBox.addComponent(new ComponentRenderTexturedStaticMesh(new Uri("DATA", "basic.vert"), new Uri("DATA", "basic.frag"))); + final ComponentPhysics physics2 = new ComponentPhysics(this.env); + final Box box2 = new Box(); + box2.setSize(new Vector3f(1, 1, 1)); + box2.setOrigin(new Vector3f(0, 0, 0)); box2.setMass(1); physics2.addShape(box2); localBox.addComponent(physics2); - env.addEntity(localBox); + this.env.addEntity(localBox); } { // add a cube to test collision ... - Entity localBox = new Entity(this.env); - localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(-2,2,14.5f)))); + final Entity localBox = new Entity(this.env); + localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(-2, 2, 14.5f)))); localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); - localBox.addComponent(new ComponentRenderTexturedStaticMesh( - new Uri("DATA", "basic.vert"), - new Uri("DATA", "basic.frag"))); - ComponentPhysics physics2 = new ComponentPhysics(true); - PhysicBox box2 = new PhysicBox(); - box2.setSize(new Vector3f(1,1,1)); - box2.setOrigin(new Vector3f(0,0,0)); + localBox.addComponent(new ComponentRenderTexturedStaticMesh(new Uri("DATA", "basic.vert"), new Uri("DATA", "basic.frag"))); + final ComponentPhysics physics2 = new ComponentPhysics(this.env); + final Box box2 = new Box(); + box2.setSize(new Vector3f(1, 1, 1)); + box2.setOrigin(new Vector3f(0, 0, 0)); box2.setMass(1); physics2.addShape(box2); localBox.addComponent(physics2); - env.addEntity(localBox); + this.env.addEntity(localBox); } - - { - // add a cube to test collision ... - Entity localBox = new Entity(this.env); - localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(-5,-5,14)))); - localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); - localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); - localBox.addComponent(new ComponentRenderTexturedStaticMesh( - new Uri("DATA", "basic.vert"), - new Uri("DATA", "basic.frag"))); - ComponentPhysics physics2 = new ComponentPhysics(true); - PhysicBox box2 = new PhysicBox(); - box2.setSize(new Vector3f(4,4,4)); - box2.setOrigin(new Vector3f(0,0,0)); - box2.setMass(1); - physics2.addShape(box2); - localBox.addComponent(physics2); - env.addEntity(localBox); - } - { - // add a cube to test collision ... - Entity localBox = new Entity(this.env); - Quaternion transform = new Quaternion(0.5f,0.2f,0.4f,1); - transform.normalize(); - localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(15,15,14), transform))); - localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); - localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); - localBox.addComponent(new ComponentRenderTexturedStaticMesh( - new Uri("DATA", "basic.vert"), - new Uri("DATA", "basic.frag"))); - ComponentPhysics physics2 = new ComponentPhysics(true); - PhysicBox box2 = new PhysicBox(); - box2.setSize(new Vector3f(8,8,8)); - box2.setOrigin(new Vector3f(0,0,0)); - box2.setMass(1); - physics2.addShape(box2); - localBox.addComponent(physics2); - env.addEntity(localBox); - } - { - // add a cube to test collision ... - Entity localBox = new Entity(this.env); - Quaternion transform = new Quaternion(0.3f,0.3f,0.4f,1); - transform.normalize(); - localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(2,-2,14.2f),transform))); - localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); - localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); - localBox.addComponent(new ComponentRenderTexturedStaticMesh( - new Uri("DATA", "basic.vert"), - new Uri("DATA", "basic.frag"))); - ComponentPhysics physics2 = new ComponentPhysics(true); - PhysicBox box2 = new PhysicBox(); - box2.setSize(new Vector3f(1,1,1)); - box2.setOrigin(new Vector3f(0,0,0)); - box2.setMass(1); - physics2.addShape(box2); - localBox.addComponent(physics2); - env.addEntity(localBox); - } - { - // add a cube to test collision ... - Entity localBox = new Entity(this.env); - Quaternion transform = new Quaternion(0,0,1,1); - transform.normalize(); - localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(2,2,14.2f),transform))); - localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); - localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); - localBox.addComponent(new ComponentRenderTexturedStaticMesh( - new Uri("DATA", "basic.vert"), - new Uri("DATA", "basic.frag"))); - ComponentPhysics physics2 = new ComponentPhysics(true); - PhysicBox box2 = new PhysicBox(); - box2.setSize(new Vector3f(1,1,1)); - box2.setOrigin(new Vector3f(0,0,0)); - box2.setMass(1); - physics2.addShape(box2); - localBox.addComponent(physics2); - env.addEntity(localBox); - } -// { -// // add a cube to test collision ... -// Entity localBox = new Entity(this.env); -// relativeTestPos = new ComponentPosition(new Transform3D(new Vector3f(0,0,14),new Quaternion(0.5f,0.2f,0.4f,1))); -// localBox.addComponent(relativeTestPos); -//// localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); -//// localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); -//// localBox.addComponent(new ComponentLight(new Light(new Vector3f(0,1,0), new Vector3f(0,0,0), new Vector3f(0.8f,0.03f,0.002f)))); -//// localBox.addComponent(new ComponentRenderTexturedStaticMesh( -//// new Uri("DATA", "basic.vert"), -//// new Uri("DATA", "basic.frag"))); -// ComponentPhysics physics2 = new ComponentPhysics(true); -// boxTest = new PhysicBox(); -// boxTest.setSize(new Vector3f(1,1,1)); -// boxTest.setOrigin(new Vector3f(0,0,0)); -// boxTest.setMass(1); -// physics2.addShape(boxTest); -// localBox.addComponent(physics2); -// env.addEntity(localBox); -// } -// { -// Entity localBox = new Entity(this.env); -// localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(0,0,14)))); -// localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); -// localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); -// localBox.addComponent(new ComponentRenderTexturedStaticMesh( -// new Uri("DATA", "basic.vert"), -// new Uri("DATA", "basic.frag"))); -// env.addEntity(localBox); -// } - Entity gird = new Entity(this.env); - gird.addComponent(new ComponentPosition(new Transform3D(new Vector3f(0,0,13)))); + { + // add a cube to test collision ... + final Entity localBox = new Entity(this.env); + localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(-5, -5, 14)))); + localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); + localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); + localBox.addComponent(new ComponentRenderTexturedStaticMesh(new Uri("DATA", "basic.vert"), new Uri("DATA", "basic.frag"))); + final ComponentPhysics physics2 = new ComponentPhysics(this.env); + final Box box2 = new Box(); + box2.setSize(new Vector3f(4, 4, 4)); + box2.setOrigin(new Vector3f(0, 0, 0)); + box2.setMass(1); + physics2.addShape(box2); + localBox.addComponent(physics2); + this.env.addEntity(localBox); + } + { + // add a cube to test collision ... + final Entity localBox = new Entity(this.env); + final Quaternion transform = new Quaternion(0.5f, 0.2f, 0.4f, 1); + transform.normalize(); + localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(15, 15, 14), transform))); + localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); + localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); + localBox.addComponent(new ComponentRenderTexturedStaticMesh(new Uri("DATA", "basic.vert"), new Uri("DATA", "basic.frag"))); + final ComponentPhysics physics2 = new ComponentPhysics(this.env); + final Box box2 = new Box(); + box2.setSize(new Vector3f(8, 8, 8)); + box2.setOrigin(new Vector3f(0, 0, 0)); + box2.setMass(1); + physics2.addShape(box2); + localBox.addComponent(physics2); + this.env.addEntity(localBox); + } + { + // add a cube to test collision ... + final Entity localBox = new Entity(this.env); + final Quaternion transform = new Quaternion(0.3f, 0.3f, 0.4f, 1); + transform.normalize(); + localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(2, -2, 14.2f), transform))); + localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); + localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); + localBox.addComponent(new ComponentRenderTexturedStaticMesh(new Uri("DATA", "basic.vert"), new Uri("DATA", "basic.frag"))); + final ComponentPhysics physics2 = new ComponentPhysics(this.env); + final Box box2 = new Box(); + box2.setSize(new Vector3f(1, 1, 1)); + box2.setOrigin(new Vector3f(0, 0, 0)); + box2.setMass(1); + physics2.addShape(box2); + localBox.addComponent(physics2); + this.env.addEntity(localBox); + } + { + // add a cube to test collision ... + final Entity localBox = new Entity(this.env); + final Quaternion transform = new Quaternion(0, 0, 1, 1); + transform.normalize(); + localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(2, 2, 14.2f), transform))); + localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); + localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); + localBox.addComponent(new ComponentRenderTexturedStaticMesh(new Uri("DATA", "basic.vert"), new Uri("DATA", "basic.frag"))); + final ComponentPhysics physics2 = new ComponentPhysics(this.env); + final Box box2 = new Box(); + box2.setSize(new Vector3f(1, 1, 1)); + box2.setOrigin(new Vector3f(0, 0, 0)); + box2.setMass(1); + physics2.addShape(box2); + localBox.addComponent(physics2); + this.env.addEntity(localBox); + } + // { + // // add a cube to test collision ... + // Entity localBox = new Entity(this.env); + // relativeTestPos = new ComponentPosition(new Transform3D(new Vector3f(0,0,14),new Quaternion(0.5f,0.2f,0.4f,1))); + // localBox.addComponent(relativeTestPos); + //// localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); + //// localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); + //// localBox.addComponent(new ComponentLight(new Light(new Vector3f(0,1,0), new Vector3f(0,0,0), new Vector3f(0.8f,0.03f,0.002f)))); + //// localBox.addComponent(new ComponentRenderTexturedStaticMesh( + //// new Uri("DATA", "basic.vert"), + //// new Uri("DATA", "basic.frag"))); + // ComponentPhysics physics2 = new ComponentPhysics(true); + // boxTest = new Box(); + // boxTest.setSize(new Vector3f(1,1,1)); + // boxTest.setOrigin(new Vector3f(0,0,0)); + // boxTest.setMass(1); + // physics2.addShape(boxTest); + // localBox.addComponent(physics2); + // env.addEntity(localBox); + // } + // { + // Entity localBox = new Entity(this.env); + // localBox.addComponent(new ComponentPosition(new Transform3D(new Vector3f(0,0,14)))); + // localBox.addComponent(new ComponentStaticMesh(new Uri("RES", "cube-one.obj"))); + // localBox.addComponent(new ComponentTexture(new Uri("DATA", "blocks/clay.png"))); + // localBox.addComponent(new ComponentRenderTexturedStaticMesh( + // new Uri("DATA", "basic.vert"), + // new Uri("DATA", "basic.frag"))); + // env.addEntity(localBox); + // } + + final Entity gird = new Entity(this.env); + gird.addComponent(new ComponentPosition(new Transform3D(new Vector3f(0, 0, 13)))); gird.addComponent(new ComponentStaticMesh(MeshGenerator.createGrid(5))); - gird.addComponent(new ComponentRenderColoredStaticMesh( - new Uri("DATA_EGE", "wireColor.vert"), - new Uri("DATA_EGE", "wireColor.frag"))); - env.addEntity(gird); - - Entity player = new Entity(this.env); - objectPosition = new ComponentPositionPlayer(new Transform3D(new Vector3f(5,5,13))); - player.addComponent(objectPosition); - objectPlayer = new ComponentPlayer(); - player.addComponent(objectPlayer); + gird.addComponent(new ComponentRenderColoredStaticMesh(new Uri("DATA_EGE", "wireColor.vert"), new Uri("DATA_EGE", "wireColor.frag"))); + this.env.addEntity(gird); + + final Entity player = new Entity(this.env); + this.objectPosition = new ComponentPositionPlayer(new Transform3D(new Vector3f(5, 5, 13))); + player.addComponent(this.objectPosition); + this.objectPlayer = new ComponentPlayer(); + player.addComponent(this.objectPlayer); player.addComponent(new ComponentMaterial(new Material())); //player.addComponent(new ComponentStaticMesh(new Uri("RES", "person.obj"))); player.addComponent(new ComponentStaticMesh(new Uri("RES", "person_-yfw_zup.obj"))); player.addComponent(new ComponentTexture(new Uri("RES", "playerTexture.png"))); - player.addComponent(new ComponentRenderTexturedMaterialsStaticMesh( - new Uri("DATA", "basicMaterial.vert"), - new Uri("DATA", "basicMaterial.frag"), - (EngineLight)env.getEngine(EngineLight.ENGINE_NAME))); - ComponentPhysics physics = new ComponentPhysics(true); - PhysicBox box = new PhysicBox(); - box.setSize(new Vector3f(0.6f,0.6f,1.8f)); - box.setOrigin(new Vector3f(0,0,0.9f)); + player.addComponent(new ComponentRenderTexturedMaterialsStaticMesh(new Uri("DATA", "basicMaterial.vert"), new Uri("DATA", "basicMaterial.frag"), + (EngineLight) this.env.getEngine(EngineLight.ENGINE_NAME))); + final ComponentPhysics physics = new ComponentPhysics(this.env); + final Box box = new Box(); + box.setSize(new Vector3f(0.6f, 0.6f, 1.8f)); + box.setOrigin(new Vector3f(0, 0, 0.9f)); box.setMass(1); physics.addShape(box); player.addComponent(physics); - env.addEntity(player); + this.env.addEntity(player); - - Camera mainView = new Camera(); - env.addCamera("default", mainView); - mainView.setPitch((float)Math.PI*-0.25f); - mainView.setPosition(new Vector3f(0,-5,5)); + final Camera mainView = new Camera(); + this.env.addCamera("default", mainView); + mainView.setPitch((float) Math.PI * -0.25f); + mainView.setPosition(new Vector3f(0, -5, 5)); this.simpleControl = new ControlCameraPlayer(mainView, player); - env.addControlInterface(simpleControl); + this.env.addControlInterface(this.simpleControl); // start the engine. - env.setPropertyStatus(GameStatus.gameStart); + this.env.setPropertyStatus(GameStatus.gameStart); - basicRotation.setEulerAngles(new Vector3f(0.005f,0.005f,0.01f)); - basicRotation2.setEulerAngles(new Vector3f(0.003f,0.01f,0.001f)); + this.basicRotation.setEulerAngles(new Vector3f(0.005f, 0.005f, 0.01f)); + this.basicRotation2.setEulerAngles(new Vector3f(0.003f, 0.01f, 0.001f)); // ready to let Gale & Ege manage the display Log.info("==> Init APPL (END)"); - creationDone = true; + this.creationDone = true; } @Override - public void onRegenerateDisplay(Context context) { - //Log.verbose("Regenerate Gale Application"); - if (!this.creationDone) { - return; - } - angleLight += 0.01; - lightPosition.getTransform().getPosition().x = 5 + (float)Math.cos(angleLight) * 7.0f; - lightPosition.getTransform().getPosition().y = 5 + (float)Math.sin(angleLight) * 7.0f; - env.periodicCall(); - markDrawingIsNeeded(); - } - - @Override - public void onDraw(Context context) { + public void onDraw(final Context context) { //Log.info("==> appl Draw ..."); - Vector2f size = getSize(); + final Vector2f size = getSize(); if (!this.creationDone) { - OpenGL.setViewPort(new Vector2f(0,0), size); - Color bgColor = new Color(0.8f, 0.5f, 0.5f, 1.0f); + OpenGL.setViewPort(new Vector2f(0, 0), size); + final Color bgColor = new Color(0.8f, 0.5f, 0.5f, 1.0f); OpenGL.clearColor(bgColor); Log.info("==> appl clear ==> not created ..."); return; @@ -346,49 +316,54 @@ public class LoxelApplication extends Application { // Store openGl context. OpenGL.push(); // set projection matrix: - Matrix4f tmpProjection = Matrix4f.createMatrixPerspective(3.14f*0.5f, getAspectRatio(), 0.1f, 50000); + final Matrix4f tmpProjection = Matrix4f.createMatrixPerspective(3.14f * 0.5f, getAspectRatio(), 0.1f, 50000); OpenGL.setMatrix(tmpProjection); // set the basic openGL view port: (Draw in all the windows...) - OpenGL.setViewPort(new Vector2f(0,0), size); - + OpenGL.setViewPort(new Vector2f(0, 0), size); + // clear background - Color bgColor = new Color(0.18f, 0.43f, 0.95f, 1.0f); + final Color bgColor = new Color(0.18f, 0.43f, 0.95f, 1.0f); OpenGL.clearColor(bgColor); // real clear request: OpenGL.clear(OpenGL.ClearFlag.clearFlag_colorBuffer); OpenGL.clear(OpenGL.ClearFlag.clearFlag_depthBuffer); OpenGL.enable(Flag.flag_depthTest); - + //Log.info("==> appl Draw ..."); - env.render(20, "default"); + this.env.render(20, "default"); if (this.debugDrawProperty == null) { - debugDrawProperty = ResourceColored3DObject.create(); + this.debugDrawProperty = ResourceColored3DObject.create(); } // now render the point test collision ... - for (int iii=0; iii