Collision - either stuck or floating through

Hi, I just arrived in this community and just finished the flagrush tutorials etc.

We use Java at university so I was looking for some java 3D stuff and finally arrived here.



My Problem is about collision: I try to navigate a simple flat cylinder in an arena and check

if there is a collision with the walls. If so I reset it to the old position.

But somehow it either drives through the walls or gets stuck in them.

What am I doing wrong? I remember I had that problem years ago when I was using DarkBASIC too …

Seems like I have a wrong strategy about collision routines :wink:


import com.jme.scene.*;
import com.jme.scene.shape.*;
import com.jme.math.*;
import com.jme.bounding.*;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.state.*;
import com.jme.system.*;

public class BotModel extends Node {
   
   private float speed = 0;
   private float angle = 0;   

   private Vector3f oldLocation = new Vector3f();
   private Quaternion oldRotation = new Quaternion();
   
   AIArena parent = null;
   
   public BotModel(String name, AIArena root) {
      super(name);
      parent = root;
      DisplaySystem display = DisplaySystem.getDisplaySystem();
      
      [...]

      this.updateModelBound();
   }
   
   protected void update(float time) {      
      Vector3f tempVa = new Vector3f();
      Vector3f loc = this.getLocalTranslation();
           loc.addLocal(this.getLocalRotation().getRotationColumn(2, tempVa)
                   .multLocal(speed));
           this.setLocalTranslation(loc);
      this.getLocalRotation().fromAngleAxis(angle, new Vector3f(0,1,0));
          
      if(this.hasCollision(parent.arena.getWallsNode(), true)) {
         this.setLocalTranslation(oldLocation);
         this.setLocalRotation(oldRotation);
      } else {
         oldLocation = new Vector3f(this.getLocalTranslation());
         oldRotation = new Quaternion(this.getLocalRotation());
      }
      this.updateGeometricState(time, true);
   }
}

Perhaps you should update the geometric state before checking for collision. It seems you are bouncing back to a position that already was in collision with the walls.

   
   this.updateGeometricState(time, true);
   if(this.hasCollision(parent.arena.getWallsNode(), true)) {
      [...]
   }


That makes no difference. I wrote a System.out.println("Collision!") for test cases within the if-body and
it works fine. The collision is detected, but for some reason I don't reset the node.
The oldLocation vector is even different to the getTranslation() vector while debugging.
So it's neither a problem of not detecting the collision nor of variables having the same reference.

If you could post a minimal working program that has this problem, it would be very helpful.

Well … forgive me for what I am gonna to now …

The whole thing is like a minimal programm. I have it on my website:

http://www.sk-downloading.de/AIArena.zip

It’s just 3 files:

AIArena.java (Main file, Swing stuff etc)

Arena.java - the Node for the walls and the ground

BotModel.java - the Node which is moving around

OK, I think I know what the problem is:



When you set

Vector3f loc = this.getLocalTranslation();

in update, oldLocation and loc are exactly the same object… therefore, when you do the addLocal, you are modifying oldLocation.


Vector3f loc = (Vector3f)this.getLocalTranslation().clone();



Is a quick fix.

seems alot of plp r having similar problems  :smiley:

No, that doesn't work.

Even the following doesn't work:


if(this.hasCollision(parent.arena.getWallsNode(), true)) {
   this.setLocalTranslation(oldLocation);
   this.setLocalRotation(oldRotation);
} else {
   oldLocation = new Vector3f(this.getLocalTranslation());
   oldRotation = new Quaternion(this.getLocalRotation());
}   


And this time it is not the same reference.

That's very strange, because it worked for me… I ran it and stopped at the wall, I could even rotate and advance following the wall.  :?



Even more… what you are proposing will not work, because the line that sets them to the same reference is


Vector3f loc = this.getLocalTranslation();



Because getLocalTranslation() return the reference and not a copy...

http://neko.bio.utk.edu/~duenez/BotModel.java

Here is the file I changed... see if it works.

I know this post is old, but I thought that I'd chime in to say that I was having the same trouble as the original poster and that the modified code in the previous post to mine worked… and helped me understand why the problem was happening.  Anyway, closure… for any who might come along after.



AV

Cool!  :smiley: