My own physic engine

hi

at the moment i m trying to design my own physic engine and i wanted to have a little bit feedback. i ll be going to explain my idea of my physic engine in this posting. Actually i didnt find much in the internet about how to design a physic engine and therefore my idea could be total rubbish and maybe im wasting my time. if u think so please tell me :slight_smile:


import com.jme.math.*;
import com.jme.scene.*;


public class PhysicalNode extends Node
{
   private TriMesh body=null;
   private CollisionBehaviour myCB=null;
   private String material=null;
   
   
   public PhysicalNode()
   {
      super();
      myCB = new DefaultCB();
   }
   
   private class DefaultCB implements CollisionBehaviour
   {
      public void collision(PhysicalNode src, PhysicalNode with) {   
      }   
   }


   public void setBody(TriMesh body){
      this.body=body;
   }
   public void setCollisionBehaviour(CollisionBehaviour c){
      this.myCB = c;
   }
   public void setMaterial(String material){
      this.material=material;
   }
   

   public TriMesh getBody(){
      return body;
   }
   public CollisionBehaviour getCollisionBehaviour(){
      return myCB;
   }
   public String getMaterial(){
      return material;
   }
      
   public void collision(PhysicalNode with){
      myCB.collision(this, with);
   }
}


import com.jme.math.*;


public class PhysicalDynamicNode extends PhysicalNode
{
   private int mass=0;
   private Vector3f speed=null;
   private Vector3f massCenter=null;
   private MovementRestriction myMR;
   
   public PhysicalDynamicNode()
   {
      super();
      myMR =new DefaultMR();
   }
   
   private class DefaultMR implements MovementRestriction
   {
      public Matrix3f setLocalRotation(Matrix3f m) {
         return m;
      }
      public Quaternion setLocalRotation(Quaternion q) {
         return q;
      }
      public float setLocalScale(float f) {
         return f;
      }
      public Vector3f setLocalScale(Vector3f v) {
         return v;
      }
      public Vector3f setLocalTranslation(Vector3f v) {
         return v;
      }   
   }
   
        public int getMass(){
      return mass;
   }
   public Vector3f getSpeed(){
      return speed;
   }
   public Vector3f getMassCenter(){
      return massCenter;
   }
   public MovementRestriction  getMovementRestriction(){
      return myMR;
   }

   // in kg
   public void setMass(int mass){
      this.mass=mass;
   }
   //|speed| in m/s
   public void setSpeed(Vector3f speed){
      this.speed=speed;
   }
   public void setMassCenter(Vector3f massCenter){
      this.massCenter=massCenter;
   }
   public void setMovementRestriction(MovementRestriction mr){
      myMR=mr;
   }
   
   public void setLocalRotation(Matrix3f m) {
      Matrix3f m2 = myMR.setLocalRotation(m);
      super.setLocalRotation(m2);
   }
   public void setLocalRotation(Quaternion q) {
      Quaternion q2 = myMR.setLocalRotation(q);
      super.setLocalRotation(q2);
   }
   public void setLocalScale(float f) {
      float f2 = myMR.setLocalScale(f);
      super.setLocalScale(f2);
   }
   public void setLocalScale(Vector3f v) {
      Vector3f v2 = myMR.setLocalScale(v);
      super.setLocalScale(v2);
   }
   public void setLocalTranslation(Vector3f v) {
      Vector3f v2 = myMR.setLocalTranslation(v);
      super.setLocalTranslation(v2);
   }
}



this inteface exists to write any kind of joints:

import com.jme.math.*;


public interface MovementRestriction
{
   public abstract Matrix3f setLocalRotation(Matrix3f m);
   public abstract Quaternion setLocalRotation(Quaternion q);
   public abstract float setLocalScale(float f);
   public abstract Vector3f setLocalScale(Vector3f v);
   public abstract Vector3f setLocalTranslation(Vector3f v);
}



with this interface you can define what the object does when it collides
(e.g. you could reduce the speed of an arrow to 0 if he hits an wodden object or you could just realize the Newton Pysics with this inteface)

interface CollisionBehaviour
{
   public abstract void collision(PhysicalNode src,PhysicalNode with);
}



i still have to think about how to realize stuff like Powers like gravity

the most important part is still missing too. i want to write an physicalIterator which prooves for collisions and updates the objects.
every PhysicalNode which you want to be iterated has to be registrated to that class. but to code this i have to learn this collision stuff in jme which i dont understand jet. :/ if anyone knows a good tutorial about this pls post it


I found the following article to be really helpful to get me started on a physics engine. Code samples are not in Java, but you can get a good idea of how everything works from there.



http://www.gaffer.org/game-physics/physics-in-3d/