I'm having a little trouble calculating this angle and I'm hoping one of you geniuses can help me.
I have a game with a cannon that can be at any spot in the game world. Using OpenGL's matrix transformations, I want the cannon's texture to rotate to face in whatever direction the player puts his finger. To do this, I need to calculate an angle to send to the rotation matrix.
Currently I'm having a little trouble calculating the angle correctly.
See the figure:
Legend:
A) A constant unit vector that always points toward the top of the screen.
B) A point that is set based on where the user clicks the screen.
theta) the angle I need to measure
As you can see, I'm using a constant unit vector that always points up as a baseline (A). What my algorithm needs to do is correctly measure the angle (theta) between A and B.
Here's the code that sets the target's position:
public void setTarget(Vector2 targetPos) {
//calculate target's position relative to cannon
targetPos = sub(targetPos, this.position);
//replace target
this.target = targetPos;
//calculate new angle
//This is broken
this.cannonAngle = findAngleBetweenTwoVectors(POINT_UP, target);
The "findAngleBetweenTwoVectors" method is what doesn't seem to be working. It's code is here:
public static float findAngleBetweenTwoVectors(Vector2 baseVec, Vector2 newVec) {
//first, make copies of the vectors
Vector2 baseCopy = new Vector2(baseVec);
Vector2 newCopy = new Vector2(newVec);
//next, ensure they're normalized
baseCopy.nor();
newCopy.nor();
//the arc-cosine is the angle between the two vectors
//this is used as the "cannonAngle" value (does not work)
return (float) Math.acos(newCopy.dot(baseCopy));
}
I know this is likely a vector math problem, I just can't seem to get the angle calculation right.
Thanks in advance for the help.
Get the coordinates of B and subtract the coordinates of the cannon from them, giving a vector pointing in the desired direction. The Java atan2 function can be used to get the angle of the vector in radians. To get the angle relative to the up vector, going clockwise, pass the arguments to atan2 in the order x,y rather than y,x (which gives a result going anticlockwise from a right-pointing vector).
So you need something like this:
double dx = b_x - cannon_x;
double dy = b_y - cannon_y;
double angle_in_degrees = Math.toDegrees(Math.atan2(dx,dy));
This answer assumes that the A vector points up, as you said, and is thus (0,1). If the A vector is arbitrary, then your original answer looks almost correct, but you need to convert to degrees as a commenter said, and possibly also check that your answer is clockwise or anticlockwise.
Related
So I'm attempting to calculate if a point is inside of an angle. While researching, I came across many terms I am unfamiliar with. Essentially from a point (A) with a 120° angle protruding from point (A). I want to test if a secondary point (B) would be inside of the angle. All that is known is the degree of the angle and the degree at which the angle is facing and the X and Y values of both points. This will all be done in Java so any and all help is appreciated!
To better explain it:
There is a point with two vectors protruding from said point. The angle that is known is the angle that is created by the protrusion of the two vectors.
First of all, an angle is not defined for two points -- only for two lines.
Define a line that is your 0° 2D-space. For a line, you need a point and a direction.
Calculate the normal-vector for your line (Turn your directional vector by 90°); normalize both your directional and normal vector so that sqrt(x^2+y^2) = 1.
Calculate the distance vector between your initial point and the other point, this is your second line, sharing the same initial point.
Calculate the dot-product of a and b:
a = distance vector × normal vector
b = distance vector × directional vector
Use simple trigonometry to calculate the angle. It's the arctangent of (a/b) or (b/a).
You probably wanna take the absolute value of the result as well if you don't care about left and right.
I have polygon from class Polygon. How can I make it bigger or smaller. I'm looking for function like grow(int,int) for rectangle.I haven't got any idea how can I do that.
Thanks in advance!
#sorry for my stupid question without any details.
I'm making polygon using:
private int[] xCoord = new int[50], yCoord= new int[50]; //Arrays containing the points of the polygon
private int pointCt=0; //The number of points that have been input
I'm clicking mouse on a certain place, then draw a line
xCoord[pointCt] = arg0.getX();
yCoord[pointCt] = arg0.getY();
pointCt++;
private void putLine(int x1,int y1,int x2,int y2)
{
Graphics g= getGraphics();
g.drawLine(x1, y1, x2, y2);
g.dispose();
}
polygon=new MyPolygon(xCoord,yCoord,pointCt);
I dont exactly know which line of my code should I post but I hope its enought. I just need an advice how to make bigger polygon having cusps in a table.
Thanks!
These are operations that are best covered by linear algebra. You'd be well served by brushing up on translation, rotation, and scaling of a body. But...
If you have all the vertex data, and the object is at origin (0, 0, 0) you can just multiply all vertices by a number - 2.0 will make the object twice as big, 3.0 will make it three times as big, 0.5 will shrink it by half, etc. Make sure to do this to all components, so if you have a two-dimensional object, make sure that you multiply both the X and the Y values of each of its vertices by the same number.
If the object is not at origin, you can find the center of the object (by getting the average of all vertices), translate that to origin (by subtracting the center from each of the vertices' position), perform your scaling (by multiplying as in the previous paragraph), and add the center back to each vertex.
This is the best way to do what you're after - no need to much around with distances or matrices.
If you have access to the position data for the vertices then just calculate the distance between all the vertices and multiply it by whatever you want the scale to be. (Ex. 20% larger multiply all distances by 1.20). You can do this using the Distance Formula.
Math.sqrt((x2-x1)^2 + (y2-y1)^2)
Once you get the new distance just add the difference between the old distance and new distance to x2 and y2 and you have your new vertex coordinates, and do this for every vertex of the polygon. If your polygon is at the origin (0, 0) you can just multiply the vertices by whatever you want the scale to be, but otherwise I'd recommend the scaling based on vertices distances.
Sorry about the bad title, but I could not think of anything else.
Let me explain: I have got 2 ships. The ship in the collision world, and the ship in the real world.
Now the ship in the real world is rotating, however I am unable to rotate the ship in the collision world (Or move it), which means that the collision objects must be rotated around the ship in the collision world.
When a CannonBall is near the ship, I get the distance from the CannonBall to the ship (for x, y, and z) and then compare that to a triple array of coordinates which are the collision points (About 2000+) - If the result from the triple array is "1", then they have collided. In other words, the collision ship does not move, but in the collision code the CannonBall's location is the same as the distance to the real-world ship and the real-world CannonBall.
Now here is the problem: If the ship is rotated, I need to rotate the CannonBall around the ship, because the ship cannot rotate.
How would I do this? What sort of mathematical formula?
Note: I have - Ship's rotation, x, y, and z coordinates, and the CannonBall's x, y, and z coordinates.
Thanks if anybody can give me an answer!
Edit: Lets make this more clear.
In the real world, ship1 could fire a CannonBall at ship2. If I refer to "The ship", I mean ship2.
When the CannonBall gets near to the ship, I want it to check for collisions. To do this,(Physics world) I check if array[CannonBall.distance(ship).x][CannonBall.distance(ship).y][CannonBall.distance(ship).z] returns a "1", and if it does, it collides.
However, because I cannot change an array without a large amount of processing, I cannot change the ship's rotation. Therefore, I must rotate the CannonBall around it so that it is relative to the CannonBall's true rotation compared to the ship.
If I got you right, then you are having a ship (e.g. in the middle of the screen) that has a canon ball rotating around it in order to aim towards another ship that is movable. All of that in 3D(?). Assuming that is what you mean, here is an answer ;) :
Lets do that in a 2 Dimensional Space first. What you need first is a formula to calculate a circle. As a function can only have one y-Value for each x-Value, you will actually need two. The smartest thing to do would probably divide the direction your canon ball is facing from the static ship into the common 360°, then have the formula calculate x and y coordinates. One approach could work with the formula for a circle (+/-r²=x²+y², with r being the radius), but I think a approach with cos and sin would be easier. Therefore:
You might want to have an input of an angle. If this angle is 90°, then your cannon ball is to the right of the ship. 180° would be underneath your ship, and 270° to the left, 0°/360° would be above. I would then take that input and add 90° to it, so that you can start to the left of your ship (IMPORTANT for cos and sin to work!). What we know now is the angle of the cannon ball, and the radius of the circle. Assuming that you know the coordinates of the middle of the ship, you can then imagine yourself in a coordinate system, with your ship being the origin. Now you need to calculate x. This can be done by:
x = -cos^-1(ANGLE)*radius. The result will be your x-Coordinate, that you have to subtract from the middle of the ship coordinate. Then, with x and the Angle being known, you can calculate y= x/tan-1(ANGLE). If the original angle is greater then 90°, you have to multiply the y coordinate with -1.
The method for this would be:
public void calculateCordinates(int degrees, int r){
degrees = degrees+90;
while(degrees>=360){degrees=degrees-360;}//ensures that degree < 360°
double x;
double y;
x = Math.acos(Math.toRadians((double)degrees))*r;
y = x/Math.atan(Math.toRadians((double)degrees));
}
The Coordinates are now in double as the results are most of the time not going to be whole numbers. Round them to ints before you subtract them from the origin coordinates.
If you want to do this in 3D, you can calculate the y coordinate by using the same method. This time, the angle you enter is your 3rd Dimension angle, so "the angle that you look at as in into the screen, out of the screen, with your right ear on the screen and so on" (sorry, didn't know how to describe that better). Of course you can combine the Methods to one as well, would look something like that:
public void calculateCordinates(int degrees, int degrees3D, int r){
degrees = degrees+90;
while(degrees>=360){degrees=degrees-360;}
double x;
double y;
double z;
x = Math.acos(Math.toRadians((double)degrees))*r;
y = x/Math.atan(Math.toRadians((double)degrees));
z = x/Math.atan(Math.toRadians((double)degrees3D));
}
I hope this is what you asked for, and I hope it helps!
I'm aware of Quaternion methods of doing this. But ultimately these methods require us to transform all objects in question into the rotation 'space' of the Camera.
However, looking at the math, I'm certain there must be a simple way to get the XY, YZ and XZ equations for a line based on only the YAW (heading) and PITCH of a camera.
For instance, given the normals of the view frustrum such as (sqrt(2), sqrt(2), 0) you can easily construct the line (x+y=0) for the XY plane. But once the Z (in this case, Z is being used for depth, not GL's Y coordinate scrambling) changes, the calculations become more complex.
Additionally, given the order of applying rotations: yaw, pitch, roll; roll does not affect the normals of the view frustrum at all.
So my question is very simple. How do I go from a 3-coordinate view normal (that is normalized, i.e the vector length is 1) or a yaw (in radians), pitch (in radians) pair to a set of three line equations that map the direction of the 'eye' through space?
NOTE:
Quaternions I have had success with in this, but the math is too complex for every entity in a simulation to do for visual checks, along with having to check against all visible objects, even with various checks to reduce the number of viewable objects.
Use any of the popular methods out there for constructing a matrix from yaw and pitch to represent the camera rotation. The matrix elements now contain all kinds of useful information. For example (when using the usual representation) the first three elements of the third column will point along the view vector (either into or out of the camera, depending on the convention you're using). The first three elements of the second column will point 'up' relative to the camera. And so on.
However it's hard to answer your question with confidence as lots of things you say don't make sense to me. For example I've no idea what "a set of three line equations that map the direction of the 'eye' through space" means. The eye direction is simply given by a vector as I described above.
nx = (float)(-Math.cos(yawpos)*Math.cos(pitchpos));
ny = (float)(Math.sin(yawpos)*Math.cos(pitchpos));
nz = (float)(-Math.sin(pitchpos)));
That gets the normals of the camera. This assumes yaw and pitch are in radians.
If you have the position of the camera (px,py,pz) you can get the parametric equation thusly:
x = px + nx*t
y = py + ny*t
z = pz + nz*t
You can also construct the 2d projections of this line:
0 = ny(x-px) + nx(y-py)
0 = nz(y-px) + ny(z-pz)
0 = nx(z-pz) + nz(x-px)
I think this is correct. If someone notes an incorrect plus/minus let me know.
I'm trying to understand how the quaternion rotations work, I found this mini tutorial http://www.julapy.com/blog/2008/12/22/quaternion-rotation/ but He makes some assumptions that I can't workout, like how can I do "work out the rotation vectors around each axis, simply by rotating the vector around an axis." and how does he calculate angleDegreesX, angleDegreesY and angleDegreesZ?
Can some one provide a working example or explanation?
The shortest possible summary is that a quaternion is just shorthand for a rotation matrix. Whereas a 4x4 matrix requires 16 individual values, a quaternion can represent the exact same rotation in 4.
For the mathematically inclined, I am fully aware that the above is super over-simplified.
To provide a little more detail, let's refer to the Wikipedia article:
Unit quaternions provide a convenient
mathematical notation for representing
orientations and rotations of objects
in three dimensions. Compared to Euler
angles they are simpler to compose and
avoid the problem of gimbal lock.
Compared to rotation matrices they are
more numerically stable and may be
more efficient
What isn't clear from that opening paragraph is that a quaternion is not only convenient, it's unique. If you have a particular orientation of an object, twisting on any number of axes, there exists a single unique quaternion that represents that orientation.
Again, for the mathematically inclined, my uniqueness comment above assumes right-handed rotations. There is an equivalent left-handed quaternion that rotates in the opposite direction around the opposite axis.
For the purpose of simple explanation, that is something of a distinction without a difference.
If you'd like to make a simple quaternion that represents rotation about an axis, here's a short series of steps that will get you there:
Pick your axis of rotation v = {x, y, z}. Just for politeness, please pick a unit vector: if it's not already of length 1, divide all the components by the length of v.
Pick an angle of rotation that you'd like to turn about this axis and call that theta.
The equivalent unit quaternion can be computed using the sample code below:
Quaternion construction:
q = { cos(theta/2.0), // This is the angle component
sin(theta/2.0) * x, // Remember, angle is in radians, not degrees!
sin(theta/2.0) * y, // These capture the axis of rotation
sin(theta/2.0) * z};
Note those divisions by two: those ensure that there's no confusion in the rotation. With a normal rotation matrix, rotating to the right 90 degrees is the same as rotating to the left by 270. The quaternions that are equivalent to those two rotations are distinct: you can't confuse one with the other.
EDIT: responding to the question in the comments:
Let's simplify the problem by setting the following frame of reference:
Pick the center of the screen as the origin (we're going to rotate around that).
X axis points to the right
Y axis points up (top of the screen)
Z axis points out of the screen at your face (forming a nice right handed coordinate system).
So, if we have an example object (say an arrow) that starts by pointing to the right (positive x axis). If we move the mouse up from the x axis, the mouse will provide us with a positive x and positive y. So, working through the series of steps:
double theta = Math.atan2(y, x);
// Remember, Z axis = {0, 0, 1};
// pseudo code for the quaternion:
q = { cos(theta/2.0), // This is the angle component
sin(theta/2.0) * 0, // As you can see, the zero components are ignored
sin(theta/2.0) * 0, // Left them in for clarity.
sin(theta/2.0) * 1.0};
You need some basic math to do what you need. Basically, you rotate a point around an axis by multiyplying the matrix representing that point with a rotation matrix. The result is the rotated matrix represantation of that point.
The line
angleX = angleDegreesX * DEGTORAD;
just converts the degrees representation into a radians reprensentation by a simple formular (see this Wikipedia entry on Radians)
You can find some more information and examples of rotation matrizes here: Rotation around arbitrary axes
There are probably tools in your programming framework to do that rotation work and retrieve the matrices. Unfortunately, I cannot help you with the quaternions but your problems seem to be a little bit more basic.