Collision problem

Why korab and stancia are not collidе?

[java]

import com.jme3.renderer.RenderManager;



import com.jme3.system.AppSettings;

import com.jme3.math.FastMath;

import com.jme3.app.SimpleApplication;

import com.jme3.font.BitmapText;

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.bullet.collision.shapes.CollisionShape;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.light.DirectionalLight;

import com.jme3.material.Material;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.bullet.BulletAppState;

import com.jme3.scene.Node;

import com.jme3.light.SpotLight;

import com.jme3.input.KeyInput;

import com.jme3.input.MouseInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.input.controls.MouseButtonTrigger;

import com.jme3.scene.Spatial;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Quaternion;

import com.jme3.scene.shape.Box;

/**

  • test
  • @author normenhansen

    /

    public class Main extends SimpleApplication {



    public static void main(String[] args) {

    Main app = new Main();

    AppSettings settings=new AppSettings(true);

    settings.setRenderer(AppSettings.LWJGL_OPENGL1);

    app.setSettings(settings);

    app.start();

    }

    Boolean isRunning=true;

    float acel=0;

    private RigidBodyControl landscape; private RigidBodyControl landscape2;



    float x=0;

    Spatial korab;

    Spatial stancia;

    private BulletAppState bulletAppState;

    public void simpleInitApp() {

    /
    * create a blue box at coordinates (1,-1,1) /

    Box box1 = new Box( new Vector3f(1,-1,1), 1,1,1);

    Geometry blue = new Geometry("Box", box1);

    Material mat1 = new Material(assetManager,

    "Common/MatDefs/Misc/Unshaded.j3md");

    mat1.setColor("Color", ColorRGBA.Blue);

    blue.setMaterial(mat1);

    bulletAppState = new BulletAppState();

    stateManager.attach(bulletAppState);

    /
    * create a red box straight above the blue one at (1,3,1) /

    Box box2 = new Box( new Vector3f(1,3,1), 1,1,1);

    Geometry red = new Geometry("Box", box2);

    Material mat2 = new Material(assetManager,

    "Common/MatDefs/Misc/Unshaded.j3md");

    mat2.setColor("Color", ColorRGBA.Red);

    red.setMaterial(mat2);

    korab = assetManager.loadModel("Models/KORAB.obj");

    stancia = assetManager.loadModel("Models/Space.obj");

    Material mat_default = new Material(

    assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    korab.setMaterial(mat_default);

    rootNode.attachChild(korab);

    stancia.setMaterial(mat_default);

    rootNode.attachChild(stancia);

    stancia.setLocalRotation(new Quaternion(4f,0.5f,0.6f,1));

    stancia.setLocalTranslation(0, 0, -1000);

    /
    * Create a pivot node at (0,0,0) and attach it to the root node /

    Node pivot = new Node("pivot");

    rootNode.attachChild(pivot); // put this node in the scene

    cam.lookAt(new Vector3f(1,3,1), new Vector3f(10,30,20));

    /
    * Attach the two boxes to the pivot node. /

    pivot.attachChild(blue);

    CollisionShape sceneShape =

    CollisionShapeFactory.createMeshShape( stancia);

    landscape = new RigidBodyControl(sceneShape, 0);

    stancia.addControl(landscape);

    CollisionShape sceneShape2 =

    CollisionShapeFactory.createMeshShape( korab);

    landscape2 = new RigidBodyControl(sceneShape2, 0);

    korab.addControl(landscape2);

    pivot.attachChild(red);

    bulletAppState.getPhysicsSpace().add(landscape);

    bulletAppState.getPhysicsSpace().add(korab);

    /
    * Rotate the pivot node: Note that both boxes have rotated! /

    pivot.rotate(.4f,.4f,0f);

    SpotLight spot = new SpotLight();

    spot.setSpotRange(100f); // distance

    spot.setSpotInnerAngle(15f * FastMath.DEG_TO_RAD); // inner light cone (central beam)

    spot.setSpotOuterAngle(35f * FastMath.DEG_TO_RAD); // outer light cone (edge of the light)

    spot.setColor(ColorRGBA.White.mult(1.3f)); // light color

    spot.setPosition(cam.getLocation()); // shine from camera loc

    spot.setDirection(cam.getDirection()); // shine forward from camera loc

    rootNode.addLight(spot);

    initKeys();

    }



    private void initKeys() {

    // You can map one or several inputs to one named action

    inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));

    inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_J));

    inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));

    inputManager.addMapping("Acel", new KeyTrigger(KeyInput.KEY_R));

    inputManager.addMapping("DEC", new KeyTrigger(KeyInput.KEY_F));

    inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE),

    new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

    // Add the names to the action listener.

    inputManager.addListener(actionListener, new String[]{"Pause"});

    inputManager.addListener(analogListener, new String[]{"Left", "Right", "Rotate","DEC","Acel"});



    }

    private ActionListener actionListener = new ActionListener() {

    public void onAction(String name, boolean keyPressed, float tpf) {

    if (name.equals("Pause") && !keyPressed) {

    isRunning = !isRunning;

    }

    }

    };



    private AnalogListener analogListener = new AnalogListener() {

    public void onAnalog(String name, float value, float tpf) {



    if (name.equals("Rotate")) {

    korab.rotate(0, value
    speed, 0);

    }

    if (name.equals("Acel")) {

    acel-=1;

    }

    if (name.equals("DEC")) {

    // if(acel!=5){

    acel+=1;

    // }

    }

    if (name.equals("Right")) {

    Vector3f v = korab.getLocalTranslation();

    korab.setLocalTranslation(v.x + valuespeed, v.y, v.z);

    }

    if (name.equals("Left")) {

    Vector3f v = korab.getLocalTranslation();

    korab.setLocalTranslation(v.x - value
    speed, v.y, v.z);

    }



    }

    };

    public void simpleUpdate(float tpf) {

    x=tpfacel0.1f + x;

    korab.setLocalTranslation(0, 0, x);

    cam.setLocation(new Vector3f(0,4,x+14));



    cam.lookAt(korab.getLocalTranslation(), new Vector3f(0,0,0));

    // cam.setRotation(korab.getLocalRotation());

    }



    @Override

    public void simpleRender(RenderManager rm) {

    //TODO: add render code

    }

    }

    [/java]

what do you mean they dont collide? and please use [ java ] [ /java ] tags (no spaces)

When I try to hit the station my ship passes like a ghost through the station.

Ok first of all:

1 - 2 mesh collision shapes cannot collide

2 - Do not move physics objects using setLocalTranslation()/Move() unless they are kinematic



So leave stancia as it is. and for korab. Try this:

[java]landscape2 = new RigidBodyControl(sceneShape2, 1);

landscape2.setKinematic(true);[/java]



giving it a mass makes it into a Hull collisionshape



Also please read the physics tutorials, it tells you all of this

Look at the section called Moving a physical spatial.

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_physics



You can not move physics object by moving their spatials, that’s not how physics work. Unless they are kinematic but then they have other restrictions. Hope that helps.

also while we are at it before Paul sees:



[java]Box box1 = new Box( new Vector3f(1,-1,1), 1,1,1);[/java]



Create the box at the origin in model space and then move it with setLocalTranslation() prior to adding any physics controls.



[java]Box box1 = new Box(Vector3f.ZERO, 1,1,1);

box1.setLocalTransation(new Vector3f(1,-1,1));



//create geom

//add physics

[/java]



Otherwise you will get unexpected results when you do getLocalTranslation() and find its at (0, 0, 0); and start scratching your head

2 Likes

Can you give me a very simple example to get it right?

@nlcenslfew said:
Can you give me a very simple example to get it right?

Like in the link jmaasing posted?

Okay, now I have improved the code. But now I can not move the ship.

[java]



import com.jme3.renderer.RenderManager;

import com.jme3.system.AppSettings;

import com.jme3.app.SimpleApplication;

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.bullet.collision.shapes.CollisionShape;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.material.Material;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.bullet.BulletAppState;

import com.jme3.scene.Node;

import com.jme3.input.KeyInput;

import com.jme3.input.MouseInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.input.controls.MouseButtonTrigger;

import com.jme3.scene.Spatial;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Quaternion;

import com.jme3.scene.shape.Box;

/**

  • test
  • @author normenhansen

    /

    public class Main extends SimpleApplication {



    public static void main(String[] args) {

    Main app = new Main();

    AppSettings settings=new AppSettings(true);

    settings.setRenderer(AppSettings.LWJGL_OPENGL1);

    app.setSettings(settings);

    app.start();

    }

    Boolean isRunning=true;

    float acel=0;

    private RigidBodyControl landscape;

    private RigidBodyControl landscape2;

    float x=0;

    Spatial korab;

    private RigidBodyControl korab_phy;

    Spatial stancia;

    Geometry blue;

    Geometry red ;

    Geometry korab2;

    private BulletAppState bulletAppState;

    public void simpleInitApp() {

    initobjects();

    initcammera();

    initphysics();

    rootNode.attachChild(korab);

    rootNode.attachChild(stancia);

    Node pivot = new Node("pivot");

    rootNode.attachChild(pivot);

    pivot.attachChild(blue);

    pivot.attachChild(red);

    pivot.rotate(.4f,.4f,0f);

    initKeys();

    }

    private void initphysics()

    {

    bulletAppState = new BulletAppState();

    stateManager.attach(bulletAppState);

    CollisionShape sceneShape2 =CollisionShapeFactory.createMeshShape( korab);

    korab_phy=new RigidBodyControl(0.0f);

    korab2.addControl(korab_phy);

    bulletAppState.getPhysicsSpace().add(korab_phy);

    }

    private void initcammera()

    {

    cam.lookAt(new Vector3f(1,3,1), new Vector3f(10,30,20));

    }

    private void initobjects()

    {

    Box box1 = new Box( new Vector3f(1,-1,1), 1,1,1);

    blue = new Geometry("Box", box1);

    Material mat1 = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");

    mat1.setColor("Color", ColorRGBA.Blue);

    blue.setMaterial(mat1);

    Box box2 = new Box( new Vector3f(1,3,1), 1,1,1);

    red = new Geometry("Box", box2);

    Material mat2 = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");

    mat2.setColor("Color", ColorRGBA.Red);

    red.setMaterial(mat2);

    korab = assetManager.loadModel("Models/KORAB.obj");

    korab2= (Geometry) korab;

    stancia = assetManager.loadModel("Models/Space.obj");

    Material mat_default = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    korab.setMaterial(mat_default);

    stancia.setMaterial(mat_default);

    stancia.setLocalRotation(new Quaternion(4f,0.5f,0.6f,1));

    stancia.setLocalTranslation(0, 0, -1000);

    }

    private void initKeys() {

    inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));

    inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_J));

    inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));

    inputManager.addMapping("Acel", new KeyTrigger(KeyInput.KEY_R));

    inputManager.addMapping("DEC", new KeyTrigger(KeyInput.KEY_F));

    inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE),

    new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

    inputManager.addListener(actionListener, new String[]{"Pause"});

    inputManager.addListener(analogListener, new String[]{"Left", "Right", "Rotate","DEC","Acel"});



    }

    private ActionListener actionListener = new ActionListener() {

    public void onAction(String name, boolean keyPressed, float tpf) {

    if (name.equals("Pause") && !keyPressed) {

    isRunning = !isRunning;

    }

    }

    };



    private AnalogListener analogListener = new AnalogListener() {

    public void onAnalog(String name, float value, float tpf) {



    if (name.equals("Rotate")) {

    korab.rotate(0, value
    speed, 0);

    }

    if (name.equals("Acel")) {

    acel-=1;

    }

    if (name.equals("DEC")) {

    // if(acel!=5){

    acel+=1;

    // }

    }

    if (name.equals("Right")) {

    Vector3f v = korab.getLocalTranslation();

    korab.setLocalTranslation(v.x + valuespeed, v.y, v.z);

    }

    if (name.equals("Left")) {

    Vector3f v = korab.getLocalTranslation();

    korab.setLocalTranslation(v.x - value
    speed, v.y, v.z);

    }



    }

    };

    public void simpleUpdate(float tpf) {

    x=tpfacel0.1f + x;

    //korab.setLocalTranslation(0, 0, x);

    korab_phy.setLinearVelocity(new Vector3f(0, 0, x));

    cam.setLocation(new Vector3f(0,4,x+14));

    cam.lookAt(korab.getLocalTranslation(), new Vector3f(0,0,0));

    }



    @Override

    public void simpleRender(RenderManager rm) {



    }

    }

    [/java]

And it is still a ghost.

You create only one mesh shape, what exactly should collide with it? There is no other physics objects. Did you do the tutorials?

uops here it is:[java]import com.jme3.renderer.RenderManager;

import com.jme3.system.AppSettings;

import com.jme3.app.SimpleApplication;

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.bullet.collision.shapes.CollisionShape;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.material.Material;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.bullet.BulletAppState;

import com.jme3.scene.Node;

import com.jme3.bullet.control.CharacterControl;

import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;

import com.jme3.input.KeyInput;

import com.jme3.input.MouseInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.AnalogListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.input.controls.MouseButtonTrigger;

import com.jme3.scene.Spatial;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Quaternion;

import com.jme3.scene.shape.Box;

/**

  • test
  • @author normenhansen

    /

    public class Main extends SimpleApplication {



    public static void main(String[] args) {

    Main app = new Main();

    AppSettings settings=new AppSettings(true);

    settings.setRenderer(AppSettings.LWJGL_OPENGL1);

    app.setSettings(settings);

    app.start();

    }

    Boolean isRunning=true;

    float acel=0;

    private RigidBodyControl landscape;

    private RigidBodyControl landscape2;

    float x=0;

    Spatial korab;

    private RigidBodyControl korab_phy;

    private RigidBodyControl stancia_phy;

    Spatial stancia;

    Geometry blue;

    Geometry red ;

    private CharacterControl player;

    Geometry korab2;

    Geometry stancia2;

    private BulletAppState bulletAppState;

    public void simpleInitApp() {

    initobjects();

    initcammera();

    initphysics();

    // rootNode.attachChild(korab);

    // rootNode.attachChild(stancia);

    Node pivot = new Node("pivot");

    //rootNode.attachChild(pivot);

    pivot.attachChild(blue);

    pivot.attachChild(red);

    pivot.rotate(.4f,.4f,0f);

    initKeys();

    }

    private void initphysics()

    {

    CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);

    player = new CharacterControl(capsuleShape, 0.05f);

    player.setPhysicsLocation(new Vector3f(0, 0, -1000));

    player.setJumpSpeed(20);

    player.setFallSpeed(30);

    player.setGravity(30);

    bulletAppState = new BulletAppState();

    stateManager.attach(bulletAppState);

    // CollisionShape sceneShape3 = CollisionShapeFactory.createMeshShape((Node) korab);

    CollisionShape sceneShape =CollisionShapeFactory.createMeshShape( stancia);

    CollisionShape sceneShape2 =CollisionShapeFactory.createMeshShape( korab);

    stancia_phy=new RigidBodyControl(sceneShape,0);

    korab_phy=new RigidBodyControl(sceneShape2,0);

    stancia2.addControl(stancia_phy);

    korab2.addControl(korab_phy);

    bulletAppState.getPhysicsSpace().add(korab_phy);

    bulletAppState.getPhysicsSpace().add(korab);

    bulletAppState.getPhysicsSpace().add(stancia_phy);

    bulletAppState.getPhysicsSpace().add(stancia);

    bulletAppState.getPhysicsSpace().add(player);

    rootNode.attachChild(korab2);

    rootNode.attachChild(stancia2);

    }

    private void initcammera()

    {

    cam.lookAt(new Vector3f(1,3,1), new Vector3f(10,30,20));

    }

    private void initobjects()

    {

    Box box1 = new Box( new Vector3f(1,-1,1), 1,1,1);

    blue = new Geometry("Box", box1);

    Material mat1 = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");

    mat1.setColor("Color", ColorRGBA.Blue);

    blue.setMaterial(mat1);

    Box box2 = new Box( new Vector3f(1,3,1), 1,1,1);

    red = new Geometry("Box", box2);

    Material mat2 = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");

    mat2.setColor("Color", ColorRGBA.Red);

    red.setMaterial(mat2);

    korab = assetManager.loadModel("Models/KORAB.obj");

    korab2= (Geometry) korab;

    stancia = assetManager.loadModel("Models/Space.obj");

    Material mat_default = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    korab.setMaterial(mat_default);

    stancia.setMaterial(mat_default);

    stancia.setLocalRotation(new Quaternion(4f,0.5f,0.6f,1));

    stancia.setLocalTranslation(0, 0, -1000);

    stancia2=(Geometry) stancia;

    }

    private void initKeys() {

    inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));

    inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_J));

    inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));

    inputManager.addMapping("Acel", new KeyTrigger(KeyInput.KEY_R));

    inputManager.addMapping("DEC", new KeyTrigger(KeyInput.KEY_F));

    inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE),new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

    inputManager.addListener(actionListener, new String[]{"Pause"});

    inputManager.addListener(analogListener, new String[]{"Left", "Right", "Rotate","DEC","Acel"});

    }

    private ActionListener actionListener = new ActionListener() {

    public void onAction(String name, boolean keyPressed, float tpf) {

    if (name.equals("Pause") && !keyPressed) {

    isRunning = !isRunning;

    }

    }

    };

    private AnalogListener analogListener = new AnalogListener() {

    public void onAnalog(String name, float value, float tpf) {

    if (name.equals("Rotate")) {

    korab.rotate(0, value
    speed, 0);

    }

    if (name.equals("Acel")) {

    acel-=1;

    }

    if (name.equals("DEC")) {

    acel+=1;

    }

    if (name.equals("Right")) {

    Vector3f v = korab.getLocalTranslation();

    korab.setLocalTranslation(v.x + valuespeed, v.y, v.z);

    }

    if (name.equals("Left")) {

    Vector3f v = korab.getLocalTranslation();

    korab.setLocalTranslation(v.x - value
    speed, v.y, v.z);

    }

    }

    };

    public void simpleUpdate(float tpf) {

    x=tpfacel0.1f + x;

    korab_phy.setPhysicsLocation(new Vector3f(0,0,x));

    cam.setLocation(new Vector3f(0,4,x+14));

    cam.lookAt(korab.getLocalTranslation(), new Vector3f(0,0,0));

    }

    @Override

    public void simpleRender(RenderManager rm) {

    }

    }[/java]

    but still there is no difference/

Now you have a bunch of static meshes, still don’t see what should collide here. Please do the tutorials and come back.

And what to do to collide?

Аnd can not do the tut, because my computer is very old.

@nlcenslfew said:
Аnd can not do the tut, because my computer is very old.

Bollocks, just enable OpenGL1 mode like you do for your own app. Also just reading them doesn't require a GPU at all.
@normen said:
... Please do the tutorials and come back.

Done.But still there is no effect[java]import com.jme3.renderer.RenderManager;
import com.jme3.system.AppSettings;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.bullet.BulletAppState;
import com.jme3.scene.Node;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.scene.Spatial;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.scene.shape.Box;
/**
* test
* @author normenhansen
*/
public class Main extends SimpleApplication {

public static void main(String[] args) {
Main app = new Main();
AppSettings settings=new AppSettings(true);
settings.setRenderer(AppSettings.LWJGL_OPENGL1);
app.setSettings(settings);
app.start();
}
Boolean isRunning=true;
float acel=0;
private RigidBodyControl landscape;
private RigidBodyControl landscape2;
float x=0;
Spatial korab;
private RigidBodyControl korab_phy;
private RigidBodyControl stancia_phy;
Spatial stancia;
Geometry blue;
Geometry red ;
private CharacterControl player;
Geometry korab2;
Geometry stancia2;
private BulletAppState bulletAppState;
public void simpleInitApp() {
initobjects();
initcammera();
initphysics();
// rootNode.attachChild(korab);
// rootNode.attachChild(stancia);
Node pivot = new Node("pivot");
//rootNode.attachChild(pivot);
pivot.attachChild(blue);
pivot.attachChild(red);
pivot.rotate(.4f,.4f,0f);
initKeys();
}
private void initphysics()
{
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setPhysicsLocation(new Vector3f(0, 0, -1000));
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
// CollisionShape sceneShape3 = CollisionShapeFactory.createMeshShape((Node) korab);
CollisionShape sceneShape =CollisionShapeFactory.createMeshShape( stancia);
CollisionShape sceneShape2 =CollisionShapeFactory.createMeshShape( korab);
stancia_phy=new RigidBodyControl(sceneShape,0);
korab_phy=new RigidBodyControl(sceneShape2,0);
// stancia.addControl(stancia_phy);
korab.addControl(korab_phy);
bulletAppState.getPhysicsSpace().add(korab_phy);
bulletAppState.getPhysicsSpace().add(stancia_phy);
rootNode.attachChild(korab);
rootNode.attachChild(stancia);
}
private void initcammera()
{
cam.lookAt(new Vector3f(1,3,1), new Vector3f(10,30,20));
}
private void initobjects()
{
Box box1 = new Box( new Vector3f(1,-1,1), 1,1,1);
blue = new Geometry("Box", box1);
Material mat1 = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", ColorRGBA.Blue);
blue.setMaterial(mat1);
Box box2 = new Box( new Vector3f(1,3,1), 1,1,1);
red = new Geometry("Box", box2);
Material mat2 = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
mat2.setColor("Color", ColorRGBA.Red);
red.setMaterial(mat2);
korab = assetManager.loadModel("Models/KORAB.obj");
korab2= (Geometry) korab;
stancia = assetManager.loadModel("Models/Space.obj");
Material mat_default = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
korab.setMaterial(mat_default);
stancia.setMaterial(mat_default);
stancia.setLocalRotation(new Quaternion(4f,0.5f,0.6f,1));
stancia.setLocalTranslation(0, 0, -1000);
stancia2=(Geometry) stancia;
}
private void initKeys() {
inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_J));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));
inputManager.addMapping("Acel", new KeyTrigger(KeyInput.KEY_R));
inputManager.addMapping("DEC", new KeyTrigger(KeyInput.KEY_F));
inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE),new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, new String[]{"Pause"});
inputManager.addListener(analogListener, new String[]{"Left", "Right", "Rotate","DEC","Acel"});
}
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Pause") && !keyPressed) {
isRunning = !isRunning;
}
}
};
private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if (name.equals("Rotate")) {
korab.rotate(0, value*speed, 0);
}
if (name.equals("Acel")) {
acel-=1;
}
if (name.equals("DEC")) {
acel+=1;
}
if (name.equals("Right")) {
Vector3f v = korab.getLocalTranslation();
korab.setLocalTranslation(v.x + value*speed, v.y, v.z);
}
if (name.equals("Left")) {
Vector3f v = korab.getLocalTranslation();
korab.setLocalTranslation(v.x - value*speed, v.y, v.z);
}
}
};
public void simpleUpdate(float tpf) {
x=tpf*acel*0.1f + x;
korab_phy.setPhysicsLocation(new Vector3f(0,0,x));
cam.setLocation(new Vector3f(0,4,x+14));
cam.lookAt(korab.getLocalTranslation(), new Vector3f(0,0,0));
}
@Override
public void simpleRender(RenderManager rm) {
}
}[/java]
@nlcenslfew said:
Done.But still there is no effect[java]
if (name.equals("Left")) {
Vector3f v = korab.getLocalTranslation();
korab.setLocalTranslation(v.x - value*speed, v.y, v.z);
}
}
};
[/java]


You can not use setLocation on the spatials for physics objects unless they are set to kinematic mode. But then they will not collide as you think.

@nlcenslfew said:
Done.But still there is no effect[java]
public void simpleUpdate(float tpf) {
x=tpf*acel*0.1f + x;
korab_phy.setPhysicsLocation(new Vector3f(0,0,x));
cam.setLocation(new Vector3f(0,4,x+14));
[/java]

You can not move physics object by setting their location unless the are kinematic but then they will not collide as you think.

You must use applyForce or applyImpulse to move physics objects, the physics objects must have a mass > 0 or they are considered static and can not move. That is what static means.

It is all there in the tutorials.

Did you seriously just wait and then post the same code again? If the tutorials really couldn’t help you I am afraid this is too complicated for you, I couldn’t explain it better than the tutorials do.

By “But still there is no effect” I mean that my ships still pass thru the station like a ghost.

Ok I’ll give it one more try.



This:

[java]

korab_phy=new RigidBodyControl(sceneShape2,0);

[/java]



Means that your physics object have 0 mass. It is static. It can not move.



[java]

korab_phy.setPhysicsLocation(new Vector3f(0,0,x));

[/java]



This is wrong, you can not move physics objects by setLocation. It says so in the tutorials and I’ve told you so already. You must use forces and impulses to move the object.

Since you do it wrong (using set location, and also moving a static object) the physics engine can not work at all. Nothing will happen.

1 Like