Problem changing vector3f direction X amount of degrees, help?

Hi!

I’m trying to make a method that changes a direction given in vector3f by x amount of degrees.

So if I give the vector 10,-10,2

then it should be able to change vector to point slightly a diffrent direction, tho not much.

Maximum x radians is it allowed to change on all axies…



So I wrote these methods:

[java]private Vector3f shake(Vector3f dir, float v) {

Vector3f randomAmount = new Vector3f(getRandomFloat(),getRandomFloat(),getRandomFloat());

float x_A = Vector3f.UNIT_X.angleBetween(new Vector3f(dir.x,0,0));

float maxX = (float) (Math.tan(x_A+v)*Math.cos(x_A)-Math.sin(x_A)*Math.cos(x_A));



float y_A = Vector3f.UNIT_Y.angleBetween(new Vector3f(0,dir.y,0));

float maxY = (float) (Math.tan(y_A+v)*Math.cos(y_A)-Math.sin(y_A)*Math.cos(y_A));



float z_A = Vector3f.UNIT_Z.angleBetween(new Vector3f(0,0,dir.y));

float maxZ = (float) (Math.tan(z_A+v)*Math.cos(z_A)-Math.sin(z_A)*Math.cos(z_A));



dir = dir.add(new Vector3f(maxX,maxY,maxZ).mult(randomAmount));



return dir;

}



private float getRandomFloat() {



return (float)(Math.random());

}[/java]



First I try to calculate the maximum distance on each axis that its allowed to move.

Then I multiply that with a random vector so that it can, on each axis, move a max distance multiplied by a value from 0 to 1.



This is for the vector of bullets from a shotgun or other weapon.

Im doing something wrong…

Heres a picture over the calculations I’v done on how to calculate max distance:

http://i.imgur.com/g4SvY.jpg

The variables does not really matter. This is calculated for one axis, could be x z or y.

The x variable is the amount that your allowed to move your axis.

A is the angle between the vertical plane and your axis

The hypotenus of z and y is 1. It is your axis normalized.

v is the amount of max spread degree. The maximum angle change.



What am I doing wrong? Any smart mathematician that can solve this?

i have a naive question, why dont you just rotate the bullet by 0-45 degrees ? with node.rotate(angleX,angleY,angleZ); since the bullet move only “forward” it will go at the direction you want (where it is looking).

1 Like

Okej great!

Now I can rotate the bullet random amount like spread.

Problem is, I need a vector3f called direction.

See the bullets lookAt was changed, but it will still move in the vector called direction.

So I must not rotate the dir vector and I have no idea on how to do that…

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

[java]newBullet.lookAt(dir, Vector3f.UNIT_Y);

System.out.println("Max: "+maxSpread);

Vector3f rot = new Vector3f(getRandomFloat(maxSpread),getRandomFloat(maxSpread),getRandomFloat(maxSpread));

newBullet.rotate(rot.x,rot.y,rot.z);

dir = newBullet.getWorldRotation().mult(dir);[/java]

Will not change the direction vector at all :frowning:

Yes, thats where I got that mult method from.

I continued reading and then tried:

[java]newBullet.lookAt(dir, Vector3f.UNIT_Y);

System.out.println("Max: "+maxSpread);

Vector3f rot = new Vector3f(getRandomFloat(maxSpread),getRandomFloat(maxSpread),getRandomFloat(maxSpread));

newBullet.rotate(rot.x,rot.y,rot.z);



Quaternion rota = new Quaternion();

rota.fromAngleAxis(newBullet.getWorldRotation().getX(), Vector3f.UNIT_X);



Quaternion rota2 = new Quaternion();

rota2.fromAngleAxis(newBullet.getWorldRotation().getX(), Vector3f.UNIT_Y);



Quaternion rota3 = new Quaternion();

rota3.fromAngleAxis(newBullet.getWorldRotation().getX(), Vector3f.UNIT_Z);



Quaternion rota4 = rota.mult(rota2).mult(rota3);





dir = rota4.mult(dir);[/java]



Exact same result, nothing :confused:

… read it?

Get this stuff straight in your head. On the one hand you got rotations and on the other hand you got direction vectors that you want to “point” in a certain direction, don’t mix that up. The world rotation / local rotation quaternion of a spatial is a relative rotation from y-up z-forward. You should not expect a quaternion to accumulate rotations around the axes or something.

[java]newBullet.rotate(rot.x,rot.y,rot.z);

Quaternion rota = newBullet.getWorldRotation();

dir = rota.mult(new Vector3f(0,1,0));[/java]

was my last shot at this.

I’m so dam tired I can’t think straight. Sorry if my attempts make less sence than a retarded monkey :stuck_out_tongue:

I gotta go to bed now, barley got time to work on this game this week due to work. -.-

thats what happens when you dont reuse your code. You forget how jme works.

Forward is the “dir” you want.

[java]

/*Creates a Vector that tells where is the forward vector of this object/

public final Vector3f getForward(Node node)

{

Vector3f forward = new Vector3f(0,0,1);

node.localToWorld(forward,forward);

return forward;

}

[/java]

1 Like

Exacly!

I need to set the rotation using the Z!

[java]Quaternion rota = newBullet.getWorldRotation();

dir = rota.mult(Vector3f.UNIT_Z);[/java]

THERE is the direction for the lookAt at a model…