Advertisement
Guest User

Untitled

a guest
Mar 20th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.32 KB | None | 0 0
  1. public class MyKinematicCharacter extends ActionInterface {
  2.  
  3.     private PairCachingGhostObject ghostObject;
  4.     private float radius;
  5.     private float[] glMatrix;
  6.     private float deltaTime,speed;
  7.     private Vector3f gravity;
  8.     private Vector3f moveDirection;
  9.     private boolean onGround;
  10.     private Timer timer;
  11.     private float rayLength=2f;
  12.     private CollisionWorld world;
  13.     private Transform transform;
  14.     private Vector3f currentGravitySpeed;
  15.     private boolean jump;
  16.  
  17.     public MyKinematicCharacter(SphereShape shape)
  18.     {
  19.         timer=new Timer();
  20.         gravity=new Vector3f(0,-9.81f/4f,0);
  21.         moveDirection=new Vector3f();
  22.         ghostObject=new PairCachingGhostObject();
  23.         ghostObject.setCollisionShape(shape);
  24.         ghostObject.setWorldTransform(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(0, 0, 0), 1.0f)));
  25.         radius = shape.getRadius();
  26.         glMatrix=new float[16];
  27.         transform=new Transform();
  28.         currentGravitySpeed=new Vector3f();
  29.         ghostObject.setCollisionFlags(CollisionFlags.CHARACTER_OBJECT);
  30.     }
  31.  
  32.     public PairCachingGhostObject GetGhostObject()
  33.     {
  34.         return ghostObject;
  35.     }
  36.  
  37.     public void SetPosition(Vector3f position)
  38.     {
  39.         ghostObject.setWorldTransform(new Transform(new Matrix4f(new Quat4f(0,0,0,1), position, 1.0f)));
  40.     }
  41.  
  42.     public void SetRayLength(float rayLength)
  43.     {
  44.         this.rayLength=rayLength;
  45.     }
  46.  
  47.     public Vector3f GetCurrentPosition()
  48.     {
  49.         ghostObject.getWorldTransform(transform);
  50.         return transform.origin;
  51.     }
  52.  
  53.     public void SetGravity(Vector3f gravity)
  54.     {
  55.         this.gravity=gravity;
  56.     }
  57.  
  58.     public void Stop()
  59.     {
  60.         System.out.println("Stop!");
  61.         if(!jump)moveDirection=new Vector3f();
  62.     }
  63.  
  64.     public void MoveOnce(Vector3f direction, float speed, float forTime) {
  65.         if(onGround) {
  66.             this.speed = speed;
  67.             moveDirection = direction;
  68.             timer.schedule(new TimerTask() {
  69.                 @Override
  70.                 public void run() {
  71.                     Stop();
  72.                 }
  73.             }, (long) forTime);
  74.         }
  75.     }
  76.  
  77.     public void MoveAlways(Vector3f direction, float speed)
  78.     {
  79.         if(onGround) {
  80.             this.speed = speed;
  81.             moveDirection = direction;
  82.         }
  83.     }
  84.  
  85.     public void Draw(GL2 gl2, GLUT glut)
  86.     {
  87.         gl2.glPushMatrix();
  88.         ghostObject.getWorldTransform(new Transform()).getOpenGLMatrix(glMatrix);
  89.         gl2.glMultMatrixf(FloatBuffer.wrap(glMatrix));
  90.         gl2.glDisable(GL2.GL_LIGHTING);
  91.         gl2.glColor3f(0.7f,0,0);
  92.         glut.glutSolidSphere(radius,10,10);
  93.         gl2.glEnable(GL2.GL_LIGHTING);
  94.         gl2.glPopMatrix();
  95.     }
  96.  
  97.  
  98.     private void Gravity()
  99.     {
  100.         if(!onGround)
  101.         {
  102.             currentGravitySpeed=MyUtil.AddVector(MyUtil.MulVector(gravity, deltaTime), currentGravitySpeed);
  103.             SetPosition(MyUtil.AddVector(GetCurrentPosition(), currentGravitySpeed));
  104.         }
  105.         else
  106.         {
  107.             currentGravitySpeed=new Vector3f();
  108.         }
  109.     }
  110.  
  111.     private void CheckGround()
  112.     {
  113.         Vector3f toWorld=new Vector3f(GetCurrentPosition());
  114.         Vector3f fromWorld = new Vector3f(GetCurrentPosition());
  115.         fromWorld.y-=radius;
  116.         toWorld.y-=(rayLength+radius);
  117.         CollisionWorld.RayResultCallback rayInfo = new CollisionWorld.ClosestRayResultCallback(fromWorld,toWorld);
  118.  
  119.         rayInfo.collisionFilterGroup=2;
  120.         world.rayTest(fromWorld, toWorld, rayInfo);
  121.         if(rayInfo.hasHit())
  122.         {
  123.             onGround=true;
  124.             currentGravitySpeed=new Vector3f();
  125.         }
  126.         else
  127.         {
  128.             onGround=false;
  129.         }
  130.     }
  131.  
  132.     public void Jump(float speed)
  133.     {
  134.         MoveAlways(new Vector3f(0,1,0),speed);
  135.         jump=true;
  136.         onGround=false;
  137.     }
  138.  
  139.     private void CalculatePos(Vector3f moveDirection)
  140.     {
  141.         Vector3f newPosition = MyUtil.MulVector(moveDirection, speed*deltaTime);
  142.         newPosition=MyUtil.AddVector(newPosition, GetCurrentPosition());
  143.         SetPosition(newPosition);
  144.     }
  145.  
  146.     private void PreStep()
  147.     {
  148.         if(GetCurrentPosition().y<0)
  149.         {
  150.             SetPosition(new Vector3f(-10,5,10));
  151.         }
  152.  
  153.         CheckGround();
  154.         if(!onGround && !jump)
  155.         {
  156.             Gravity();
  157.         }
  158.         else
  159.         {
  160.             if(moveDirection.length()>0)
  161.             {
  162.                 Vector3f fromWorld = new Vector3f(GetCurrentPosition());
  163.                 Vector3f toWorld = new Vector3f(GetCurrentPosition());
  164.                 fromWorld.y-=radius;
  165.                 toWorld.y=fromWorld.y;
  166.                 Vector3f temp=MyUtil.MulVector(moveDirection,rayLength);
  167.                 toWorld=MyUtil.AddVector(toWorld,temp);
  168.                 CollisionWorld.ClosestRayResultCallback callback = new CollisionWorld.ClosestRayResultCallback(fromWorld,toWorld);
  169.                 callback.collisionFilterGroup=2;
  170.                 world.rayTest(fromWorld,toWorld,callback);
  171.  
  172.                 if(callback.hasHit())
  173.                 {
  174.                     double angle=MyUtil.AngleToY(callback);
  175.                     System.out.println(angle+ " " + callback.collisionObject.getCollisionShape().getName());
  176.                     if(angle>=60.0)
  177.                     {
  178.                         Stop();
  179.                     }
  180.                     else
  181.                     {
  182.                         CalculatePos(MyUtil.InvertXZ(callback.hitNormalWorld));
  183.                     }
  184.                 }
  185.                 else
  186.                 {
  187.                     CalculatePos(moveDirection);
  188.                 }
  189.  
  190.                 if(jump)
  191.                 {
  192.                     moveDirection = MyUtil.SubVector(moveDirection,new Vector3f(0,0.1f,0));
  193.                     if(moveDirection.y<=0)
  194.                     {
  195.                         moveDirection=new Vector3f();
  196.                         jump=false;
  197.                     }
  198.                 }
  199.             }
  200.         }
  201.     }
  202.  
  203.     @Override
  204.     public void updateAction(CollisionWorld collisionWorld, float deltaTime) {
  205.         this.deltaTime=deltaTime;
  206.         world=collisionWorld;
  207.  
  208.         PreStep();
  209.     }
  210.  
  211.     @Override
  212.     public void debugDraw(IDebugDraw iDebugDraw) {
  213.  
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement