Collision & last position

Hi,



well, I know that the people talk a lot of this topic, but I have another question. I read post in this forums how the flagRush tutorial, and the post of the tank and the wall,… , but don't have solution. I want to do a system of collision that when the player collision with the wall the player don't cross them, and this system doing if the player don't collision with the walls I save this position, and if the player collision with the walls I put the player in the lastPosition that don't collision with the walls. The teory it's simple, I don't do a very complex system, I only do this.



My Code:

import java.util.logging.Logger;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.Text;
import com.jme.scene.shape.Box;
import com.jme.scene.state.MaterialState;

/**
 * Started Date: Jul 22, 2004<br><br>
 *
 * Demonstrates loading formats.
 *
 * @author Jack Lindamood
 */
public class HelloModelLoading extends SimpleGame {
    private static final Logger logger = Logger
            .getLogger(HelloModelLoading.class.getName());
   
    private Node player, terrain;
   
    private Text infotext;
   
    private float angle = 0;
   
    private Vector3f lastGood = new Vector3f();
   
    public static void main(String[] args) {
        HelloModelLoading app = new HelloModelLoading();
        app.setConfigShowMode(ConfigShowMode.AlwaysShow);
        // Turn the logger off so we can see the XML later on
        app.start();
    }

    protected void simpleInitGame() {
        createPlayer();
        createTerrain();
        posCam();
        setupInput();
       
        infotext = Text.createDefaultTextLabel("text informatiu");
        infotext.getLocalTranslation().set( 0, 20, 0 );
        statNode.attachChild( infotext );
       
    }
   
    protected void createPlayer(){
       player = new Node("playerNode");
       rootNode.attachChild(player);
       Box b = new Box("playerBox", new Vector3f(0,2,0),0.25f,0.25f,0.25f);
       b.setModelBound(new BoundingBox());
       b.updateModelBound();
       
       final MaterialState materialState = display.getRenderer().createMaterialState();
        materialState.setSpecular( ColorRGBA.red );
        b.setRenderState( materialState );
        b.updateRenderState();
       
       player.attachChild(b);
    }
   
    protected void posCam(){
       cam.setLocation(new Vector3f(-6,24,-24));
       cam.lookAt(new Vector3f(0,-1,1), new Vector3f(0,1,0));
    }
   
    protected void createTerrain(){
       terrain = new Node("terrain");
       rootNode.attachChild(terrain);
       Box c = new Box("floor",new Vector3f(0,0,0),10,1,10);
       c.setModelBound(new BoundingBox());
       c.updateModelBound();
       
       Box d = new Box("obstacle",new Vector3f(10,1,0),3,3,3);
       d.setModelBound(new BoundingBox());
       d.updateModelBound();
       
       terrain.attachChild(c);
       terrain.attachChild(d);   
    }
   
    private void setupInput()
   {
      KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();

        keyboard.set("for", KeyInput.KEY_U);
        keyboard.set("back", KeyInput.KEY_J);
        keyboard.set("right", KeyInput.KEY_K);
        keyboard.set("left", KeyInput.KEY_H); 
   }
   
    public void rotarPlayer(int direccio){ //if direccio == 1 gira a la dreta, si direccio = -1 esquerra
       if (timer.getTimePerFrame() < 1) {
          angle = angle + (timer.getTimePerFrame() * direccio * 5);
          if (angle > 360) {
             angle = 0;
          }
       }
       Quaternion rotQuat = new Quaternion();
       rotQuat.set(player.getLocalRotation());
       rotQuat.fromAngleAxis(angle, new Vector3f(0, 1, 0));

       player.setLocalRotation(rotQuat);
  }
   
    protected void simpleUpdate(){

       if(KeyBindingManager.getKeyBindingManager().isValidCommand("for", true)){
           Vector3f loc = player.getLocalTranslation();
            loc.addLocal(player.getLocalRotation().getRotationColumn(2, new Vector3f())
                    .multLocal(0.018f));
            player.setLocalTranslation(loc);
        }
       if(KeyBindingManager.getKeyBindingManager().isValidCommand("back", true)){
           Vector3f loc = player.getLocalTranslation();
           loc.subtractLocal(player.getLocalRotation().getRotationColumn(2, new Vector3f())
                    .multLocal(0.018f));
            player.setLocalTranslation(loc);
        }
       if(KeyBindingManager.getKeyBindingManager().isValidCommand("right", true)){
          rotarPlayer(-1);
        }
       if(KeyBindingManager.getKeyBindingManager().isValidCommand("left", true)){
          rotarPlayer(1);
        }
       
       if(player.hasCollision(terrain, true)){
          
          player.setLocalTranslation(lastGood);
          infotext.print("Collision: " + lastGood.x + " " + lastGood.z + " <> " + player.getLocalTranslation().x + "  " + player.getLocalTranslation().z);
       }
       else{
          lastGood.set(player.getLocalTranslation());
          infotext.print("No Collision: " + lastGood.x + " " + lastGood.z + " <> " + player.getLocalTranslation().x + "  " + player.getLocalTranslation().z);
       }
    }
}



Thanks for read another post of collisions... and sorry for my very bad english...

Thanks,
Guindillaaaaa

there was a tiny error in your code:


// player.setLocalTranslation(lastGood);
player.getLocalTranslation().set(lastGood);



setLocalTranslation() only sets the reference to the vector3f, that means the players location and the lastGood point to the exact same Vecor3f reference.



edit: just another example from your code:

Vector3f loc = player.getLocalTranslation();
//.. modify loc
player.setLocalTranslation(loc);


the setLocalTranslation() is unnecessary


I hope in JDK7 we get a @Dangerous annotation for such methods :wink:

Also with your code, you'll still get stuck.



No Collision: 6.7452135 -0.026255278 <> 6.7452135  -0.026255278

No Collision: 6.7497134 -0.026272794 <> 6.7497134  -0.026272794   -> lastGoodPos

Collision: 6.7497134 -0.026272794 <> 6.7497134  -0.026272794



i'd just save not only the last good pos, but the last 2-3 good position to get a bit safety space.


Thank you very much!



I know that I have a stupid error… but with the help of all, everybody can solution their problems, and learn of them.



Here is the code, that when the box (player) collision with the box (terrain), the player stop:


if(player.hasCollision(terrain, true)){
          Vector3f actualPos = new Vector3f();
          actualPos.set(player.getLocalTranslation());
          
          lastGood0.multLocal(2);
          lastGood0.subtractLocal(actualPos);
          player.getLocalTranslation().set(lastGood0);
          
          infotext.print("Collision: " + lastGood0.x + " " + lastGood0.z + " <> " + player.getLocalTranslation().x + "  " + player.getLocalTranslation().z);
       }
       else{
          
          lastGood0.set(player.getLocalTranslation());
          infotext.print("No Collision: " + lastGood0.x + " " + lastGood0.z + " <> " + player.getLocalTranslation().x + "  " + player.getLocalTranslation().z);
       }
}



Thanks another time!and thanks for reply fast!

Salut!