I am currently trying to make som polygons rotate for my Asteroids game! :)
In order to do that, I'm using the AffineTransform setToRotation() method, however I am very confused about the meaning of the parameters. For setToRotation(a, b, c) I understand that b and c are the x and y coordinates of the point, the shape revolves around. a somehow makes the shape rotate, but it doesn't appear to be in degrees. So what else is it? And what to the other two setToRotation methods (setToRotation(a, b) and setToRotation(a)) do? I dont understand them AT ALL.
Thanks for every answer!
As in the documentation:
theta - the angle of rotation measured in radians
All of the trigonometric functions in java.lang.Math either accept or return radians.
You can convert from degrees to radians using Math.toRadians.
The other method overloads are also described in the documentation. Unless you can describe what about them you don't understand, there is no point in trying to explain them again, as that explanation could be in the same terms that you don't understand.
So what else is it?
It is in radians. You can see its documentation here. To convert from degrees to radians, just divide by 180 then times by π, so π radians is 180 degrees for examples. Or you can use Math.toRadians.
And what to the other two setToRotation methods (setToRotation(a, b) and setToRotation(a)) do?
Those are also very well documented. See this and this.
Basically, the one that takes one parameter is equivalent to calling setToRotation(a, b, c) but with b and c all equal to 0, and the one that takes 2 parameters is equivalent to calling setToRotation(a) with the inverse tangent of the quotient of the two parameters (setToRotation(Math.atan2(a, b))).
Related
Firstly lets say I have forgotten most of my maths long ago. I do not need to understand it in depth. The question was asked and answered (see links) but I do not want to derive my own function, is there an existing one ? The image below show a Matrix which I have rotated 45 degrees. Is there a way I could plug in the 0.707 numbers and get 45 back ? At the moment I am keeping track of the rotation on my own (simple solution), but I would prefer a function to derive back the 45 degrees
question 7291053
Matrix Rotation
The function is called "arcus sinus" or arcsin(x). For arcsin(0.707107) = 45 with a bit of rounding error.
In Java Math library you must additionally translate the result from RAD to DEG like this:
Math.asin(0.707) * 180d / Math.PI
Note that you get back something between -90° and +90° as described here.
If you want to know which axis you actually rotate and on which part (lower, upper) of the circle you are, then you must take a look at all 9 values. See here how the matrices look like for each axis.
For the angles of each axis x,y,and z
[0][1][2]
[3][4][5]
[6][7][8]
double x = Math.atan2([7], [8]);
double y = Math.atan2(-[6],Math.sqrt((Math.pow([7],2)+Math.pow([8],2)));
double z = Math.atan2([0], [3]);
then multiply the one you choose by 180/PI
Ok, I am supposed to calculate the angle of a right triangle using Java. The measurements of the sides are a = 3 and b = 4 with the hypotenuse being c = 5. If I use Math.sin, it does not calculate the angle created by a and c. Is there another Math function I am not aware of? I have spent a long time trying to figure this out to no avail. Thanks in advance.
The probably source of the problem is that sin works in radians, rather than degrees.
To find the angle bounded by the side a and the side c, try using Math.toDegrees(Math.acos((double)a/c)). The passed value d to Math.acos(d) will have to be double.'Math.acos(d)' will give the arc cos in radian. Go through Math documentation
What is the difference between rotating a geometry in JMonkeyEngine using the rotate method:
float r = FastMath.DEG_TO_RAD * 45f; // convert degrees to radians
geom.rotate(r, 0.0f, 0.0f); // rotate the geometry around the x-axis by 45 degrees
and rotating a geometry using a quaternion:
Quaternion roll045 = new Quaternion(); // create the quaternion
roll045.fromAngleAxis(45*FastMath.DEG_TO_RAD, Vector3f.UNIT_X); // supply angle and axis as arguments)
geom.setLocalRotation(roll045); // rotate the geometry around the x-axis by 45 degrees
This is confusing for me because the result is the same for both. So I'd like to find out the difference and when to use one over the other.
The book that I'm reading says that the first method is relative, and the second using a quaternion is absolute but I'm still fuzzy on what that means.
Difference between using a quaternion and using Euler angles
To the question in your title there is no functional difference between using quaternions and angle representation, in fact internally the .rotate() function is
public Spatial rotate(float xAngle, float yAngle, float zAngle) {
TempVars vars = TempVars.get();
Quaternion q = vars.quat1;
q.fromAngles(xAngle, yAngle, zAngle);
rotate(q);
vars.release();
return this;
}
In other words, whether you use a quaternion directly or not you are using a quaternion.
Difference between .rotate() and .setLocalRotation()
However the two functions you are using are not equivalent, in fact there are both .rotate(angles) and .rotate(quaternion) (although the .setLocalRotation() is only available for quaternions). So the second part of your question is whats the difference between .rotate(anything) and .setLocalRotation(anything). Again looking at the source code gives us our answer
public Spatial rotate(Quaternion rot) {
this.localTransform.getRotation().multLocal(rot);
setTransformRefresh();
return this;
}
public void setLocalRotation(Quaternion quaternion) {
localTransform.setRotation(quaternion);
setTransformRefresh();
}
So, .rotate() rotates the object (in its local frame) by an amount from where it is now whereas .setLocalRotation() changes the rotation irrespective of where it is now.
Conclusion
If your object currently has no rotation the two functions are identical, however if the object has already been rotated then they are equivalent to "as well as the current rotation" and "instead of the current rotation".
Quaternions have many advantages over the standard angle approach; the most obvious of which is avoiding gimbal lock. Where you can use quaternions do use them. The angles methods are all convenience methods to help you if you need them.
I'm creating a very very simple game for fun. Realizing I needed the trajectory of an object given an angle and a velocity, it seemed logical to use this parametric equation:
x = (v*cos(ø))t and y = (v*sin(ø)t - 16t^2
I know that this equation works for a trajectory, but it isn't working with most ø values I use.
Do java angles work any differently from normal angle calculation?
My goal is for the object to start from bottom left of the window and follow an arc that is determined by the speed and angle given. However it tends to go strange directions.
The value of ø should be horizontal at 0 degrees and vertical at 90, and in the equation it refers to the angle at which the arc begins.
This is my first ever question post on this site, so if I'm missing anything in that regard please let me know.
Here is the calculating part of my code
not shown is the void time() that counts for each 5ms
also I should mention that the parX and parY are used to refer to the x and y coordinates in an unrounded form, as graphics coordinates require integer values.
Any help is much appreciated, and thank you in advance!
public void parametric()
{
parX = (float) ((speed*cos(-ø))*time);
gravity = (time*time)*(16);
parY = (float) ((float) ((speed*sin(-ø))*time)+gravity)+500;
xCoord = round(parX);
yCoord = round(parY);
}
Do java angles work any differently from normal angle calculation?
You only need to read the docs
public static double cos(double a)
Parameters:
a - an angle, in radians.
I guess you are using degrees instead of radians?
what I want to do is the following: I have an object (blue point) and I want to point it to other object no matter where it is located around it (green point). So I need to know the angle between these two objects to do what I want right?
http://s13.postimage.org/6jeuphcdj/android_angle.jpg
The problem is, I don't know what to do to achieve this. I've already used atan, math.tan and so many other functions but without any good results.
Could you help me? Thanks in advance.
Calculate a dot product of object vectors. Use Math.acos on the value you get. That will give you an angle in radians.
So, say your blue dot is at vec1 = (50, 100) and green one at vec2 = (100, 400).
A tuple (x, y) as a two dimensional vector describes object's position and distance from (0, 0) on your screen. To find the angle between these two vectors, you do a standard, binary dot product operation on them. This will get you a scalar (a value, cos(Theta)), but you want the inverse of it (acos) which is the angle you're looking for.
You can get a better understanding on the matter here
Suppose the coordinates of the blue and green points are (xblue, yblue) and (xgreen, ygreen) respectively.
The angle at which the blue point sees the green point is:
double angleRadians = Math.atan2(ygreen-yblue, xgreen-xblue);
If you want the angle in degrees:
double angleDegrees = Math.toDegrees(angleRadians);