Trying to rotate a cube, Have no idea what is happening here

My rotations aren’t working for some reason. Knowing me, it’s probably super simple & in the docs somewhere(I actually did look).

This is what happens when i press W, S, D, A.

What I’m trying to do is rotate the cube Forward, Backward, Left, & Right.

rotation(getting this under 50 KB was not fun. It’s also faster than it should be for some reason.)

Input Manager:

    inputManager.addMapping("Up_KB", new KeyTrigger(KeyInput.KEY_UP), new KeyTrigger(KeyInput.KEY_W));
    inputManager.addMapping("Down_KB", new KeyTrigger(KeyInput.KEY_DOWN), new KeyTrigger(KeyInput.KEY_S));
    inputManager.addMapping("Left_KB", new KeyTrigger(KeyInput.KEY_LEFT), new KeyTrigger(KeyInput.KEY_A));
    inputManager.addMapping("Right_KB", new KeyTrigger(KeyInput.KEY_RIGHT), new KeyTrigger(KeyInput.KEY_D));
    inputManager.addListener((ActionListener) (String name, boolean isPressed, float tpf) -> {
        gl.player.slerpNewRot(0);
    }, "Up_KB");
    inputManager.addListener((ActionListener) (String name, boolean isPressed, float tpf) -> {
        gl.player.slerpNewRot(180);
    }, "Down_KB");
    inputManager.addListener((ActionListener) (String name, boolean isPressed, float tpf) -> {
        gl.player.slerpNewRot(90);
    }, "Left_KB");
    inputManager.addListener((ActionListener) (String name, boolean isPressed, float tpf) -> {
        gl.player.slerpNewRot(270);
    }, "Right_KB");

Player Class:

public class Player {
    public Vector3f position = new Vector3f();
    public Quaternion rotation = new Quaternion();
    protected Quaternion newRot, oldRot;
    protected float rotSlerp = 0;
    public Node node;
    public void buildNode() {
        this.node = new Node();
        Geometry g = new Geometry("Box", new Box(0.5f, 0.5f, 0.5f));
        Material mat = new Material(Main.assets, "Common/MatDefs/Light/Lighting.j3md");
        mat.setColor("Diffuse", ColorRGBA.Blue);
        mat.setColor("Specular", ColorRGBA.Blue);
        g.setMaterial(mat);
        this.node.attachChild(g);
        //this.node.attachChild(CrusherBuilder.types.get(0));
    }
    public void slerpNewRot(int degrees) {
        oldRot = this.rotation;
        newRot = Quaternion.ZERO.fromAngleAxis(degrees*FastMath.DEG_TO_RAD, Vector3f.UNIT_Y);
        this.rotSlerp = 0;
    }
    public void update(GameLogic logic) {
        this.position.add(0.01f, 0, 0);
        if (newRot != null) {
            this.rotation = oldRot.slerp(oldRot, newRot, rotSlerp+=0.01f);
            if (rotSlerp >= 1) {
            //this.rotation = newRot;
                newRot = null;
                oldRot = null;
                System.out.println("done");
            }
        }
        this.node.setLocalTranslation(position);
        this.node.setLocalRotation(rotation);
    }
}

If you multiply movements and rotations by tpf it will smooth it out. If you don’t, it rotates a large angle in one frame, if you do, it rotates the angle smoothly per second. So if you rotate it at half pi * tpf it will rotate 45 degrees in one second.

Same for movements.

This is because frame times vary. If you do it this way it won’t matter if you have 10 fps of 3000 fps, it will still rotate at the same speed.

1 Like

Why not just use https://imgur.com/.

Just copy and paste the url. You don’t even have to sign up.

Images are only allowed for your avatar icon, 50k or less.

You mean rotate it like the normal fly camera rotates? Or something else?

Either way, it’s interesting how you consistently seem to find exactly the wrong direction to run in.

1 Like

I found something intresting… If I re-enable the fly-cam, the rotation works perfectly.

Rotating the player. The code is above, in the player class.
Theese are the changes I made to the camera.

this.flyCam.setEnabled(false);
this.cam.setLocation(position.subtract(7, -5, 0));
this.cam.setRotation(Quaternion.ZERO.fromAngles(35*FastMath.DEG_TO_RAD, 90*FastMath.DEG_TO_RAD, 0));

Yes it is…

Do not modify the “constants”. You will have a shit-ton of really messed up errors.

You gain NOTHING by doing this, either. Just use new Quaternion().fromAngles() like a sane person.

2 Likes

Ok, I read though all the quaternion, matrix and rotation pages and they were in need of lovin. There was no info about how not to use constants and lots of broken links and old jme2 stuff.

All better now.

I took the liberty of immortalizing your reply @pspeed as a quote, in the troubleshooting section. Hope you don’t mind.

Edit: Forgot the link for @TooMuchJava
https://wiki.jmonkeyengine.org/jme3/rotate.html#troubleshooting-rotations

I suggest reading the whole page and following the links to the quaternion and matrix pages. You will use quaternion a lot. There is also the Math for Dummies link that is a must read in the links at the side menu and top menu.

All those dummies links are must reads actually.

2 Likes

Hahah… it’s fine. Just funny that when I break decorum and use foul language, that’s when I get quoted. :slight_smile:

1 Like

I thought it accentuated the point appropriately.

None of the slideshows appear in the pdf… I guess i need to look at the site for some things.

I guess I forgot to look at the source here. Thought it might save processor power sence it appeared to be a function that just made a new Quaternion. Oops :stuck_out_tongue: Thanks though.

What pdf?

found one here
https://legacy.gitbook.com/book/jmonkeyengine/wiki-jmonkeyengine/details

I have never seen that but its from three years ago so its definitely worthless. The engine moved to github around then and went from 3.0 to 3.1, which is like apple and oranges.

Stick to the linked wiki.

Also, all you have to do to read the dummies stuff is click the links. They are also part of the wiki but they are linked to the forum for a reason.

Ok, it still covers the basic concepts though.

Here’s a really useful link. Don’t know if you saw this yet.

https://wiki.jmonkeyengine.org/jme3/intermediate/math.html

1 Like

thanks