Rotation problem

My idea is to simulate the move between 2 coordinates.

Here is a part of codes:

[java]/* calculate the angles */

public void angleXZBetweenDots(Vector3f start, Vector3f target) {

Vector2f start2D = new Vector2f(0f, 0f);

Vector2f targetXZ = new Vector2f(target.getX() - start.getX(),start.getZ() - target.getZ());

angleXZ = start2D.normalize().angleBetween(targetXZ.normalize());

Vector2f targetXY = new Vector2f(target.getX() - start.getX(),target.getY() - start.getY());

angleXY = start2D.normalize().angleBetween(targetXY.normalize());



pivot.rotate(0, angleXY, angleXZ);

}



public void update(float tpf) {

float x = FastMath.cos(angleXZ) * tpf * 1.5f;

float y = (x / FastMath.cos(angleXZ)) * FastMath.tan(angleXY); // incorrect, I think

float z = FastMath.sin(angleXZ) * tpf * 1.5f;

pivot.setLocalTranslation(pivot.getLocalTranslation().add(x, y, -z));

}[/java]





But I have 2 problems now:

  1. the rotation is incorrect with [java]pivot.rotate(0, angleXY, angleXZ);[/java].
  2. the movement is not synchronal. The movement on Y-axis is faster than the movements on X- and Z-axes.



    Can someone help, please?

I don’t get it, why the rotation?

why not just linearly interpolate between the 2 coords?

There is a convenience FastMath.interpolateLinear method to do so.

Yeah, the calculations you do are really funny :wink:

If there was not the convenience method Nehon suggested, simply subtracting Vector A from Vector B will give you the vector “in between” the two. Then you can simply multiply that vector by 0…1 and add it to Vector A. This would give you all points on A-B.

First, thx for your answer.



But I am new with JME3 and cant understand completely what you said. Can you please explain more in detail?



What I need is: first of all, rotate to the direction of the target, then move slowly to the target. (2 coordinates of start and target are known.)



thx again.

PS:

it would be best with an example.

something like

[java]Vector3f from; // already known

Vector3f to; // already known

Vector3f travel = to.substract(from);

Vector3f step = travel.mult(1/10); // 10 steps



Vector3f = new location(from);

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

// travel from a to b in 10 steps

location.add(step);

}



[/java]Doing this from memory so don’t be too harsh :wink:



(edit: I think you can also let physics do the moving for you)

This can solve the movement-problem.



How about the rotation?

Maybe you can use the lookAt function of the node to rotate it into the right direction. ( message me in icq) :smiley:

What rotation? You already have your movement vectors. thats where you need to go. Just point your nose there.

[java]Spatial bullet;

ParticleEmitter smoketrail;



smoketrail.setInWorldSpace(false);

Node pivot = new Node(“pivot”);

rootNode.attachChild(pivot);

pivot.attachChild(bullet);

pivot.attachChild(smoketrail);

pivot.setLocalTranslation(start);

pivot.lookAt(target,Vector3f.UNIT_Y);[/java]



I want to turn the bullet with smoketrail behind towards target.

With “pivot.lookAt(target,Vector3f.UNIT_Y)” I can only turn the bullet to the target, but the smoketrail doesn’t turn with the bullet together.

Do you know why?

That makes more sense, you should have begin with that. :stuck_out_tongue:



try to attach the bullet and the emitter to a node. use the lookat on the node, and interpolate the position of the node, this should work with almost no math :wink:

You mean FastMath.interpolateLinear(scale, startValue, endValue)?

What’s the Vector3f als return? What does “scale” mean?



Can you explain it in detail? Thank you!

Its what I have explained. Scale is 0…1 for the single points on the way from a to b.

Yep, thank you all for the help! Now the movement problem is solved.



Can you help me more with the rotation?

Like I said before:



With “pivot.lookAt(target,Vector3f.UNIT_Y)” I can only turn the bullet to the target, but the smoketrail doesn’t turn with the bullet together.

Can anyone help me more with the rotation?