[SOLVED] Vector direction left or right tip

Sort of unrelated except it’s an example of the magic of dot products… as the matrix multiple that does rotation is just a bunch of dot products.

Let me ask you something that may be related with the dot/cross , if objA do an lookat at objB, but if the upvector you want to use is not align with the objA Y plane, it seens it bugs the lookat results right ?
The way to fix it is to use the dot / cross to align the up vector to objA Y plane, am I correct ?

No it doesn’t bug them, the object is simply aligned with the up vector you give and the direction vector towards the other object.

Thats is what I did notice, the way to fix it is to use the dot / cross right ?

Why “fix”? Thats the intended result. What is your intended result?

I need to do an look at to an object but keep my own up vector.

tempNode.lookAt(AppStateGamePhaseRun.shipPosNode.getWorldTranslation() , Utils.getFowardVector(spatial));

Since the upvector of the spatial is not align with the ship upvector, it will be required to fix I guess.
I know this is kind of silly, but I just figure out now why I was getting wrong results when using lookat all this time :stuck_out_tongue:

It’s weird that you pass a forward vector for an up vector.

If you need to look at an object but keep your own up vector then pass your own up vector (not the forward vector but the up vector) to lookAt().

Yes, its the same game, the boss is moved using an central node, so in order to move it I just slerp this central node into an rotation looking at the ship position. To do that, I create an node at 0,0,0, and lookat on the shipposition using as upvector, the fowardvector from the boss.
Should I create an new topic ?

Easiest for us would be to find some inputs that produce an output that you don’t expect… and then we can explain why those inputs are busted or why your output expectations are off.

…slight chance there’s really a bug, too.

But if you find a case of “bad” results, capture the vectors, etc. that you passed and make a super simple test case with hard-coded vectors.

Thanks pspeed, I dont think its a bug, just an bad upvector I am using.
I will do some research and create an new topic lather, will try to input all information to make it easy to understand ( its always hard to explain :slight_smile: )

Well, if your test case consists of a few variables and some hard-coded values then we don’t have to judge what you are trying to do until we figure out which inputs are wrong, etc…

Well I just built an test case above.
I was expecting objA to allways shows in the same position of objB, but the result is odd, if the objB is moving the objA kind of moves, if not, it enters in some kind of invalid state and start to shake like crazy :

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;

public class Main extends SimpleApplication implements AnalogListener {

    Node NodeObjA = new Node();
    Node NodeObjB = new Node();
    Geometry objCentral,objA,objB;

    public static void main(String[] args) { new Main().start(); }
    public Main() { super(null); }

    @Override public void simpleInitApp() {
        rootNode.attachChild(NodeObjA);
        rootNode.attachChild(NodeObjB);

        objCentral=createBox(rootNode,null,0.1f,ColorRGBA.Gray);
        objA=createBox(NodeObjA,new Vector3f(0,2,0),0.4f,ColorRGBA.Blue);
        objB=createBox(NodeObjB,new Vector3f(0,2,0),0.4f,ColorRGBA.Red);
        NodeObjB.rotate(0, 0, 1);

        inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_LEFT));     inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_A ));
        inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_RIGHT));   inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("up", new KeyTrigger(KeyInput.KEY_UP));         inputManager.addMapping("up", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("down", new KeyTrigger(KeyInput.KEY_DOWN));     inputManager.addMapping("down", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addListener(this, "left","right","up","down");
    }


    @Override public void simpleUpdate(float tpf) {
        Node tempNode = new Node(); rootNode.attachChild(tempNode);
        tempNode.lookAt(objB.getWorldTranslation() , getFowardVector(objA) );

        NodeObjA.setLocalRotation(tempNode.getLocalRotation());
    }


    public void onAnalog(String name, float value, float tpf) {
        if (name.equals("up"))    NodeObjB.rotate(1*tpf, 0, 0);
        if (name.equals("down"))  NodeObjB.rotate(-1*tpf, 0, 0);
        if (name.equals("left"))  NodeObjB.rotate(0, 0, 1*tpf);
        if (name.equals("right")) NodeObjB.rotate(0, 0, -1*tpf);
    }

    @Override public void simpleRender(RenderManager rm) { }

    public static Vector3f getUpVector(Geometry obj) { return obj.getWorldRotation().mult(Vector3f.UNIT_Y); }
    public static Vector3f getFowardVector(Geometry obj) { return obj.getWorldRotation().mult(Vector3f.UNIT_Z); }

    public Geometry createBox(Node ownerNode, Vector3f position, float size,ColorRGBA color) {
        Box b = new Box(size, size, size);
        Geometry geom = new Geometry("Box", b);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", color);
        mat.getAdditionalRenderState().setWireframe(true);
        geom.setMaterial(mat);
        if(position!=null) geom.setLocalTranslation(position);
        ownerNode.attachChild(geom);
        return geom;
    }

}

Let me explain what I think its wrong, and Normen gives me the tip.
I am trying to make the boss to pursuit the player, to do that the boss needs to be “facing” the player in the persuit.
How to do that ? I was planning to do this using lookat from the central node to the player, using the foward vector from the boss to the player as upvector, but since its an sphere, this upvector from the boos is not 100% align with the player.
If it works, I could just slerp into this rotation and the boss would face and persuit the player.
I maybe doing it the hard way thought, maybe there is another way to do it ?

You really attach a new node every frame? I’m surprised this doesn’t crash very quickly.

I still don’t understand why you use the forward vector for the up vector. If you want one object to look at another object then why not lookAt() that object?

felt this worth repeating. You could save us all a lot of time by just hard coding some values in a main() method and showing us what you get and why it’s not what you expect.

Else what we are really doing is debugging your approach which is also fine but a different problem.

This new Node is at the center of the sphere, the objects are in the orbit. (obs: I supouse not to add this new node to the rootnode, just did it to debug )
The objective is to make objA to face ( in the sphere ) to objB, and here is my problem.
I guess I will have to use some kind of trigonometric function or dot to take the angles maybe.
So, in this case, what should be my upvector ?

But once a frame you create a node, attach it, then never remove it again. So after 1 second at 60 FPS, you have 60 objects. After 1 minute you will have 3600 objects in your scene, and so on.

If you need to “get the angle” you are probably doing it wrong. After all, it’s not like the lookAt() method is some magic black box that’s source cannot be viewed. Angles will certainly never work in your case anyway as they lose too much information… unless you mean ship’s heading in your spherical game data coordinates… in which case as from previous threads, that’s how your game objects should have been thinking about things all along.

…but it’s more than trig that will be required to solve that problem unless you make it a steering issue and just use the dot product from earlier in this thread. heading += turnAmount * maxTurnRate or whatever.

This puzzles kills me ! :smile:
Anyway, you are right about the tempNode, but, the way to go is to invest some time to understand the cross / dot functions right ?

I feel like you haven’t adequately explained why objA lookAt objB won’t work and the conversation loop isn’t fun for me anymore.

I am not sure why this dosent work, I guess is because what normen said :

No it doesn’t bug them, the object is simply aligned with the up vector you give and the direction vector towards the other object.

My upvector is wrong, running the code the lookat just point to wrong place.
Anyway, I was thinking to follow another aprouch, just to take the vector from my boss into the ship and dot into the foward vector from my boss, if I understood what dots function do it will do the job.
I maybe need to understand a litle bit more my options here.