Rotation bug with Kinematic control?

Hello,

I think I found a bug in the rotation of a node. It works fine, rotating, however when new objects are added to the (root)node, they are not properly constructed because of a rotation problem in these. Ie a box is a flat object.



I tried setting up a example, but Im not sure if it shows the bug.



Btw: this might not be a bug but a “feature” of kinematic controls, I dont know.



[java]/*

  • Copyright © 2009-2010 jMonkeyEngine
  • All rights reserved.

    *
  • Redistribution and use in source and binary forms, with or without
  • modification, are permitted provided that the following conditions are
  • met:

    *
    • Redistributions of source code must retain the above copyright
  • notice, this list of conditions and the following disclaimer.

    *
    • Redistributions in binary form must reproduce the above copyright
  • notice, this list of conditions and the following disclaimer in the
  • documentation and/or other materials provided with the distribution.

    *
    • Neither the name of ‘jMonkeyEngine’ nor the names of its contributors
  • may be used to endorse or promote products derived from this software
  • without specific prior written permission.

    *
  • THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  • "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  • TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  • PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  • CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  • EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  • PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  • PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  • LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  • NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  • SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    */

    package jme3test.bullet;



    import com.jme3.bullet.BulletAppState;

    import com.jme3.app.SimpleApplication;

    import com.jme3.asset.TextureKey;

    import com.jme3.bullet.PhysicsSpace;

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

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

    import com.jme3.bullet.control.RigidBodyControl;

    import com.jme3.font.BitmapText;

    import com.jme3.input.MouseInput;

    import com.jme3.input.controls.ActionListener;

    import com.jme3.input.controls.MouseButtonTrigger;

    import com.jme3.material.Material;

    import com.jme3.math.Quaternion;

    import com.jme3.math.Vector2f;

    import com.jme3.math.Vector3f;

    import com.jme3.renderer.queue.RenderQueue.ShadowMode;

    import com.jme3.scene.Geometry;

    import com.jme3.scene.Node;

    import com.jme3.scene.shape.Box;

    import com.jme3.scene.shape.Sphere;

    import com.jme3.scene.shape.Sphere.TextureMode;

    import com.jme3.shadow.BasicShadowRenderer;

    import com.jme3.texture.Texture;

    import com.jme3.texture.Texture.WrapMode;



    /**

    *
  • @author double1984

    */

    public class TestBrickWall extends SimpleApplication {



    static float bLength = 0.48f;

    static float bWidth = 0.24f;

    static float bHeight = 0.12f;

    Material mat;

    Material mat2;

    Material mat3;

    BasicShadowRenderer bsr;

    private static final Sphere bullet;

    private static final Box brick;

    private static final SphereCollisionShape bulletCollisionShape;



    static {

    bullet = new Sphere(32, 32, 0.4f, true, false);

    bullet.setTextureMode(TextureMode.Projected);

    bulletCollisionShape = new SphereCollisionShape(0.4f);



    brick = new Box(Vector3f.ZERO, bLength, bHeight, bWidth);

    brick.scaleTextureCoordinates(new Vector2f(1f, .5f));

    }

    private BulletAppState bulletAppState;



    public static void main(String args[]) {

    TestBrickWall f = new TestBrickWall();

    f.start();

    }

    float time = 0;

    @Override

    public void simpleUpdate(float tpf) {



    time += tpf / speed;

    if (time > 3f / speed) {

    addBrick(Vector3f.UNIT_Y.mult(10f));

    time = 0;

    }

    }



    @Override

    public void simpleInitApp() {

    bulletAppState = new BulletAppState();

    bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);

    stateManager.attach(bulletAppState);

    initMaterial();

    initWall();

    initFloor();

    initCrossHairs();

    this.cam.setLocation(new Vector3f(0, 6f, 6f));

    cam.lookAt(Vector3f.ZERO, new Vector3f(0, 1, 0));

    cam.setFrustumFar(15);

    inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

    inputManager.addListener(actionListener, "shoot");



    rootNode.setShadowMode(ShadowMode.Off);

    bsr = new BasicShadowRenderer(assetManager, 256);

    bsr.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());

    viewPort.addProcessor(bsr);

    }



    private PhysicsSpace getPhysicsSpace() {

    return bulletAppState.getPhysicsSpace();

    }

    private ActionListener actionListener = new ActionListener() {



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

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

    Geometry bulletg = new Geometry("bullet", bullet);

    bulletg.setMaterial(mat2);

    bulletg.setShadowMode(ShadowMode.CastAndReceive);

    bulletg.setLocalTranslation(cam.getLocation());

    RigidBodyControl bulletNode = new BombControl(assetManager, bulletCollisionShape, 1);

    // RigidBodyControl bulletNode = new RigidBodyControl(bulletCollisionShape, 1);

    bulletNode.setLinearVelocity(cam.getDirection().mult(25));

    bulletg.addControl(bulletNode);

    rootNode.attachChild(bulletg);

    getPhysicsSpace().add(bulletNode);

    }

    }

    };



    public void initWall() {

    float startpt = bLength / 4;

    float height = 0;

    for (int j = 0; j < 15; j++) {

    for (int i = 0; i < 4; i++) {

    Vector3f vt = new Vector3f(i * bLength * 2 + startpt, bHeight + height, 0);

    addBrick(vt);

    }

    startpt = -startpt;

    height += 2 * bHeight;

    }

    }



    public void initFloor() {

    Box floorBox = new Box(Vector3f.ZERO, 10f, 0.1f, 5f);

    floorBox.scaleTextureCoordinates(new Vector2f(3, 6));



    Geometry floor = new Geometry("floor", floorBox);

    floor.setMaterial(mat3);

    floor.setShadowMode(ShadowMode.Receive);

    floor.setLocalTranslation(0, -0.1f, 0);

    floor.addControl(new RigidBodyControl(new BoxCollisionShape(new Vector3f(10f, 0.1f, 5f)), 0));

    this.rootNode.attachChild(floor);

    this.getPhysicsSpace().add(floor);

    }



    public void initMaterial() {

    mat = new Material(assetManager, "Common/MatDefs/Misc/SimpleTextured.j3md");

    TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");

    key.setGenerateMips(true);

    Texture tex = assetManager.loadTexture(key);

    mat.setTexture("ColorMap", tex);



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

    TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");

    key2.setGenerateMips(true);

    Texture tex2 = assetManager.loadTexture(key2);

    mat2.setTexture("ColorMap", tex2);



    mat3 = new Material(assetManager, "Common/MatDefs/Misc/SimpleTextured.j3md");

    TextureKey key3 = new TextureKey("Textures/Terrain/Pond/Pond.png");

    key3.setGenerateMips(true);

    Texture tex3 = assetManager.loadTexture(key3);

    tex3.setWrap(WrapMode.Repeat);

    mat3.setTexture("ColorMap", tex3);

    }



    public void addBrick(Vector3f ori) {



    Geometry reBoxg = new Geometry("brick", brick);

    reBoxg.setMaterial(mat);

    reBoxg.setLocalTranslation(ori);

    //for geometry with sphere mesh the physics system automatically uses a sphere collision shape



    reBoxg.setShadowMode(ShadowMode.CastAndReceive);

    reBoxg.getControl(RigidBodyControl.class).setFriction(0.6f);

    float rot = 0.4f;

    Node node = new Node();

    RigidBodyControl rigid = new RigidBodyControl(1.5f);

    rigid.setKinematic(true);

    node.addControl(rigid);

    node.attachChild(reBoxg);

    node.setLocalRotation(new Quaternion().fromAngleAxis(rot, Vector3f.UNIT_Y));



    this.rootNode.attachChild(node);

    this.getPhysicsSpace().add(reBoxg);

    }



    protected void initCrossHairs() {

    guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");

    BitmapText ch = new BitmapText(guiFont, false);

    ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);

    ch.setText("+"); // crosshairs

    ch.setLocalTranslation( // center

    settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,

    settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);

    guiNode.attachChild(ch);

    }

    }

    [/java]

The physics object first has the rotation you give it when you add it to the physics space, which is zero in your case. Then when the update loop is running it is set to the spatials location. Set the spatials location before you attach the control, that sets that rotation to the control as well before you add it.

The location and rotation are set up fine. I shall elaborate:

I have rootnode with a car in it, the car has a kinematic rigidbodycontrol (cause I want it to move and rotate at user input). When I move the car new landscape is produced and thus new objects. Whenever I rotate the car with localrotation or just rotate, some of the new landscape (the box items) produced will turn out flat (instead of a box in this case). The terrain will remain good.

The items are rigidbodycontrols as well, however not kinematic (since they do not move at user input ofcourse).



A (quick)fix could be that I replace the rigidbodycontrol of the car with something else, thatll probably be the vehicle control.



However thought I just mentioned…

This sounds more like you somehow modify one of the unit vectors in your code somewhere. Something like Vector3f.UNIT_X.multLocal(1,0,1);

That could be something like it I guess…

I used:



.setLocalRotation(new Quaternion().fromAngleAxis(rot*(180/(float)Math.PI), Vector3f.UNIT_Y));



where rot was acquired by getRotation by fromangleaxis().



Perhaps I used a unitvector for the first setRotation.



EDIT: I’m trying to use a vehiclecontrol, and my vehicle wont budge. Doesnt it work without wheels?



[java]

BoxCollisionShape box = new BoxCollisionShape(new Vector3f(8,6,10));

rigid = new VehicleControl(box,0);

rigid.setGravity(new Vector3f(0,0,0));

//rigid.setPhysicsLocation(Vector3f.ZERO);

//rigid.setPhysicsRotation(new Matrix3f());

// rigid.setLinearVelocity(Vector3f.ZERO);

//rigid.setAngularVelocity(Vector3f.ZERO);

subNode.addControl(rigid);

Physics.bulletAppState.getPhysicsSpace().addAll(subNode);

[/java]

Ive it set up like this and am calling steer and accelerate like in the example.

kajos said:
EDIT: I'm trying to use a vehiclecontrol, and my vehicle wont budge. Doesnt it work without wheels?

Rofl, of course not xD

Oh… In my neighbourhood vehicles usually dont have wheels. I come from the future where we have flying vehicles (so not because Im from the ghetto).

Either way, I added two wheels, one for steering and one for acceleration. It still wont budge, even when its touching ground (its rough terrain so Im hoping thats not the problem).

Bullets vehicle is a raycast vehicle, a description of what that means is found in the javadoc. The acceleration force is applied at the ray contact point so if theres no contact theres no acceleration.

I see, so if I was to make a hovercraft I should just apply forces manually.

as in the hovercraft example, yes.