Results 1 to 1 of 1

Thread: Simple box Physics

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Simple box Physics

    Hello everyone.

    I am making a game in the Java language (mind the prefix, it is close enough), and I got every aspect done. Graphics, sound and a game logic.

    Now I have an issue with the physics. I have a dice (box) with 8 points. I need to simulate physics by changing the boxes rotation speed and velocity. I understand that I have to check if a point of this box pierces another box or is under the terrain, but then I get issues. I have to make the box "bounce off", and based on the location it must rotate a certain value minus some sort of factor, so it doesn't go bouncing around forever.

    This code can *somewhat* do the "below height 0 physics", but it is pretty buggy since it does not take account to its own velocity:
    java Code:
    1. public void move(float forceX, float forceY, float forceZ) {
    2.         this.velocity.add(forceX, forceY, forceZ);
    3.     }
    4.     public void rotate(float rotX, float rotY, float rotZ) {
    5.         Vector v = new Vector();
    6.         v.x = rotX;
    7.         v.y = rotY + Common.sin(this.location.pitch) * rotZ;
    8.         v.z = Common.cos(this.location.pitch) * rotZ;      
    9.         this.rotation.add(v);
    10.     }
    11.     public void push(Vector location, Vector force, float DiceHeight) {
    12.         location = location.clone();
    13.         location.subtract(this.location.getPosition());
    14.         location.rotate(new Vector(-this.location.yaw, 0, 0));
    15.    
    16.         float forcefactor = 150 * (DiceHeight - 1);
    17.         float constantforce = 0.0f;
    18.         float pitch = 0;
    19.         float roll = 0;
    20.         float max = 0.48f;
    21.         float min = 0.001f;
    22.         if (location.x < -min && location.x > -max) {
    23.             roll = forcefactor * (force.y + constantforce);
    24.         } else if (location.x > min && location.x < max) {
    25.             roll = -forcefactor * (force.y + constantforce);
    26.         }
    27.         if (location.z < -min && location.z > -max) {
    28.             pitch = forcefactor * (force.y + constantforce);
    29.         } else if (location.z > min && location.z < max) {
    30.             pitch = -forcefactor * (force.y + constantforce);
    31.         }
    32.         this.rotate(0, pitch, roll);
    33.         this.move(force.x, force.y, force.z);
    34.     }
    35.  
    36.     private void doCollisionCheck() {
    37.         java.util.Vector<Vector> collidedpoints = new java.util.Vector<Vector>();
    38.         float lowestpoint = 1;
    39.         float highestpoint = 0;
    40.         for (Vector v : geometry.getRelativePoints(this.location)) {
    41.             //Check for each point of this box if it pierces (is inside) another box
    42.                    
    43.             //Check if any points pierce the terrain
    44.             if (v.y <= 0) {
    45.                  collidedpoints.add(v);
    46.             }
    47.              if (v.y < lowestpoint) lowestpoint = v.y;   
    48.              if (v.y > highestpoint) highestpoint = v.y;
    49.         }
    50.         if (collidedpoints.size() != 0) {
    51.             Vector offsetcenter = Vector.getCenter(collidedpoints);
    52.             //float fallforce = -0.6f * this.velocity.y;
    53.             this.location.y -= lowestpoint;
    54.             float fallforce = 0.5f * (topheight - this.location.y);
    55.             fallforce += 0.09f * (highestpoint - lowestpoint);
    56.             topheight = this.location.y;
    57.             this.velocity.y = 0;
    58.             this.push(offsetcenter, new Vector(this.velocity.x * -0.1f, fallforce, this.velocity.z * -0.1f), highestpoint - lowestpoint);      
    59.             if (fallforce > 0.2f) Resources.s_collide.play(this.location.getPosition());
    60.         }
    61.     }
    62.     public void doLogic(float timefactor) {
    63.         //Do collision detection before or after velocity and location update?
    64.        
    65.         //======================Velocity and location update================
    66.         acceleration.y = -9.81f;
    67.         velocity.x += acceleration.x * timefactor;
    68.         velocity.y += acceleration.y * timefactor;
    69.         velocity.z += acceleration.z * timefactor;
    70.        
    71.         //Air friction move factor
    72.         velocity.x -= 0.2f * velocity.x * timefactor;
    73.         velocity.y -= 0.2f * velocity.y * timefactor;
    74.         velocity.z -= 0.2f * velocity.z * timefactor;
    75.        
    76.         //Air friction rotation factor
    77.         rotation.x -= 0.8f * rotation.x * timefactor;
    78.         rotation.y -= 0.8f * rotation.y * timefactor;
    79.         rotation.z -= 0.8f * rotation.z * timefactor;  
    80.        
    81.         location.x += velocity.x * timefactor;
    82.         location.y += velocity.y * timefactor;
    83.         location.z += velocity.z * timefactor;
    84.         location.yaw += rotation.x * timefactor;
    85.         location.pitch += rotation.y * timefactor;
    86.         location.roll += rotation.z * timefactor;
    87.         if (location.y > topheight) topheight = location.y;
    88.         //=======================================================================
    89.        
    90.         doCollisionCheck();
    91.     }

    And I have completely NO idea how to do collisions with other boxes...

    Any help on what factors I should take account to properly rotate a box based on an impact collision?

    I guess I would have to use the impact direction vector + force, but how can I possible convert this to rotation and velocity changes?

    java Code:
    1. public void applyForce(Vector from, Vector hitat, float force) {
    2.        
    3.     }

    EDIT 2

    I start to believe I have a way more annoying problem: I can't rotate my geometry in the axis location. At a certain point my geometry can only use yaw or roll, but no pitch.
    I use the following transformation parern:
    - rotate yaw (+ clockwise, - anti-clockwise)
    - rotate pitch (+ forward, - backward)
    - rotate roll (+ clockwise, - anti-clockwise

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width