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