jME Collision Detection Problem

Hi Everyone,

I’m still working on my top down space fighter “game” (I use that term very loosely) and I’m working on collision detection right now.



I can detect collisions between the ship and stationary objects no problem. When the collision occurs I simply make the x and z velocities of the ship negative. (Since the collision is with an immovable object i.e. infinite mass)



This should make the ship bounce back. Here’s the problem that I’ve found. After I reverse the velocities, in the next frame the ship hasn’t had enough time to get out of the collision. Therefore, another collision is detected and the velocities are reversed again. So instead of bouncing back the ship just sorta gets stuck. In reality the velocities are flipping from positive to negative every frame.



I just wanted to check to see what people came up with the solve this problem. I can think of a few possible solutions.


  1. WHen a collision occurs, move the ships position backwards a small amount to get it out of collision area.



    hmm … actually thats all I can think of at the moment. Is this the accepted solution to this problem? Thanks for any advice.
  1. yeah, getX returns the actual field… But the set(Quat) method of Quat does a copy of the float values, so they two Quats are not being siamese twined.



    If you had done ship.previousRot = ship.getLocalRotation(); then yes, that would have been a problem.

Well I got it working :slight_smile:



But I had a question which is probably more java oriented than jME.



To set the previousPosition I was doing this


ship.previousRot.set(ship.getLocalRotation());



I found this didn't work and I had to do this instead

       
Quaternion temp = new Quaternion(ship.getLocalRotation());
ship.previousRot.set(temp);



My guess being that getLocalRotation() returns a reference to the local rotation quaternion rather than a copy, so that the previousRot would be changed whenever the local rotation changed.

1) Is this correct?
2) Was creating the temp and then assigning previousRot to the temp the right way to handle it.

Once again thank you for all the help!!

^^;;;;



Haha … I think you’re right Per. I need to set the previous rotation in the KeyNodeRotateRight and KeyNodeRotateLeft as opposed to in update() before the new rotation is computed.

I think you got those two lines mixed up :slight_smile:


ship.previousRot.set(ship.getLocalRotation()); // This line sets the previous rotation to the current rotation.
ship.setLocalRotation(ship.previousRot); // So this line sets the current rotation to the current rotation ;)



So it should be:


ship.setLocalRotation(ship.previousRot);
ship.previousRot.set(ship.getLocalRotation());

I’m not positive but I think most of the problems left with collision detection stem from rotation of the object in question (i.e. the ship). Not only do I need to reset the ships previous position using setTranslation() but I think I need to reset the ship to its previous rotation state.



Unfortunately, I can’t get it to work properly. I set the previousRotation value in the same place that I set the previousTranslation value (in update()) Then I reset to the old rotation in processCollision().



I’m using the KeyNodeRotateLeft and KeyNodeRotateRight classes and use

ship.previousRot.set(ship.getLocalRotation());
ship.setLocalRotation(ship.previousRot);


to reset the local rotation of the ship. Is the use of KeyNodeRotateLeft and Right a problem? I'm not sure when that gets called in relation to the update loop.

For a nodes new position to be considered by the collision detection routine does updateGeometricState() need to be called before calculateCollisions()



In other words,


  1. ship.setTranslation(newPosition);
  2. calculateCollisions



    vs.


  3. ship.setTranslation(newPosition);
  4. rootNode.updateGeometricState();
  5. calculateCollisions



    which one is correct if I want the collision detection to check the newPosition of ship as set in step 1.

    Thanks!!



    ******EDIT



    Looks like the second one is needed and you have to updateGeometricState() before calling calculateCollisions.



    One other thing I noticed. If you continue to "ram" into the object (i.e. keep trying to push into it) eventually you get stuck. You bounce off of it less and less until the collision gets found everytime. Almost like you manage to sneak in before a collision is detected.



    The other problem occurs when you "float" (no friction since its space) towards an object with your ship very slowly and spin your ship around in circles, you can actually "catch" the object and get stuck as well.



    If anyone has dealt with these issues please let me know your solution.



    I just started thinking about these problem so it could be the way I coded up the collision stuff. Anyway, thanks for all the help from everyone today. I really appreciate it.

That’s how I have always handled it. My steps are usually:


  1. move
  2. check collision
  3. if no collision end
  4. else move to previous position
  5. reverse direction