Hot answers tagged angles
14
I believe your intuition was correct, just not your formula.
atan(4 / 3) = 53.1301024 degrees
This ratio can be useful because it forms a Pythagorean triangle, meaning that the length of the diagonal is an exact integer value.
9
Although they compare properly, the difference between them is 315 degrees instead of the correct 45 degrees.
What makes you think 315 is incorrect? In one direction, it is 315 degrees, in the other direction, it's 45. You want to choose whichever is the smallest of the 2 possible angles and this seems to intrinsically require a conditional. You can't ...
6
That vector can be obtained simply by summing up the normals of the boxes faces and normalize. The normal vector even point outside the box so the sum vector points toward the direction where the boxes exit from the intersection. along with the vector you need the intersection segment too (if you still don't have it)
6
You do not need to know the angle, because the difference in X and Y already gives you the desired orientation of the enemy. The only thing that remains to be done is normalise that direction vector (if possible -- otherwise it means the player and the enemy are exactly at the same position), and multiply it by the enemy’s speed:
dx = player_x - enemy_x;
dy ...
6
Two parts: 1. coordinate systems for angles can be...finicky. 2. You don't really need degrees for anything, with the possible exception of outputting their value to the user interface.
Coordinate system angles
So you want an angle from "North", and judging from your example math, that means the -Y direction (in sprite coordinates with the origin in the ...
5
The industry standard for first-person view simulation in most shooters is to have character models and animations distinct from those used for third-person view. There are several reasons for this:
The player has a much smaller field of view upon the world than a real person in the character's situation would, and he lacks other forms of input such as ...
4
GLM's rotation function uses Euler's rotation theorem, which implies that any rotation or sequence of rotations of a rigid body in a three-dimensional space is equivalent to a pure rotation about a single fixed axis.
However consecutive calls to GLMs rotate function just multiply the rotation so rotating a rigid body by Yaw, Pitch, Roll is as simple as ...
3
double angle = Math.atan2(y,x); // Note: keeping angle in radians for cos & sin.
dx = enemy.speed * Math.cos( angle );
dy = enemy.speed * Math.sin( angle );
This will work fine with negative angles.
See also: What are atan and atan2 used for in games?
3
When working with floating point values it's usually not a good idea to use the comparison operator, as even slight inaccuracies will result in inequality. That's why a comparison of floats usually incorporates some sort of "epsilon".. a margin of error.
Example:
if(Math.abs(floatA - floatB) <= epsilon){
// equal
}
Epsilon is the desired ...
3
Let b be the angle between vectors p1p2 and p1p3. Its value can be computed as:
b = pi - atan2(p1p3.y, p1p3.x)
The angle between p1p4 and p1p3 is b-a. Since p1p3p4 is a right-angled triangle, we know that cos(b-a) is the distance p1p4 divided by the distance p1p3.
The answer is then:
a = pi - atan2(p1p3.y, p1p3.x) - acos(r / length(p1p3))
Replacing ...
3
First figure out the direction based on where the particle is in reference to where it came from (the explosion). Then you take the arc-tangent of that to get the angle.
Vector2 direction = particlePosition - explosionPosition;
float angle = Math.Atan2(direction.Y, direction.X);
2
There's always the trick of doing both branches and letting the comparison result pick one:
delta_theta = (angle1 > angle2) * (360 - angle2 - angle1)
+ (angle2 > angle1) * (angle2 - angle1);
I don't know of a way to do it without comparisons, but usually the branch is what makes code slow and long, not the compare. At least in my ...
2
Assuming true evaluates to -1 and false evaluates to 0, and '~', '&' and '|' are bitwise not, and and or operators respectively, and we're working with two's-complement arithmetic:
temp1 := angle1 > angle2
/* most processors can do this without a jump; for example, under the x86 family,
it's the result of CMP; SETLE; SUB .., 1 instructions */
...
2
While your question made no reference of them, I'm going to be working on the assumption that your angle calculation question stems from wanting to know the minimum angle between two vectors.
That calculation is easy. Assuming A and B are your vectors:
angle_between = acos( Dot( A.normalized, B.normalized ) )
If you didn't have vectors and wanted to use ...
2
EDIT: The answer is now correct, you had to add 360 in case of diff negative
You just have to look at the difference between the two angles. It it is between 0 and 180, you turn left (anticlockwise), otherwise right.
int FindTurnSide(int current, int target)
{
int diff = target - current;
if(diff < 0)
diff += 360;
if(diff ...
2
Normally I store all objects as 4x4 Matrices (you could do 3x3 but easier for me just to have 1 class) instead of translating back and forth between a 4x4 and 3 sets of vector3s (Translation, Rotation, Scale). Euler angles are notoriously difficult to deal with in certain scenarios so I would recommend using Quaternions if you really want to store the ...
2
The camera angle used by most "isometric" games is actually 30 degrees (a true isometric view where the x, y, and z axis have the same length is 35.264 degrees). The reason for this angle is so that the width of the tile ends up being twice its height. This way you can get an even 2:1 ratio when drawing the diagonals so the tiles line up neatly without any ...
1
It is common to render the players own character vastly different from other characters in order to avoid these issues.
With a real rifle you have got two mechanics that don't translate well to computer games:
When not aiming you'll typically raise your head higher to get a better view, some games implement multiple different tiers of aiming to simulate ...
1
The function takes angles and calculates vectors that point in directions relative to angles.
For angles, and the operations sin cos and tangent, you'll want to look up trigonometry.
Multiplying an angle in degrees by M_PI*2 / 360 converts degrees to radians. (M_PI = 3.14).
forward is a vector that points in the direction that the angles form to create ...
1
It just computes a (slightly wacky, because of Quake's "entity pitches are negative" thing) 3x3 rotation matrix, with each column (or row, depending on your preference) being one of the forward/right/up vectors. This can then be used to position various objects/effects/etc relative to the view; Quake uses it for pushing muzzleflashes slightly forwards and ...
1
The angles used to build a rotation for each of the three axes are known as Tait-Bryan angles (often confused with Euler angles).
Wikipedia has all the formulas you need to convert Euler or Tait-Bryan angles into a rotation matrix.
Here is some code to build a rotation matrix from three Tait-Bryan angles and the order of the rotations:
/* i, j and k are ...
1
You need to find the delta between your two vectors and just move the sprite along that new vector.
Vector2 startPos = new Vector2(0,0);
Vector2 endPos = new Vector2(100,200);
Vector2 delta = endPos - startPos;
//see if he is at the target. Very rarely will it be Vector2.Zero, so subtract 3 to get you close. If you don't do this your sprite can bounce.
if ...
1
I think this can be transfered to simple ray casting / ray tracing. You shoot ray, find hit, reflect, find other hit, etc.
The easiest way is brute force. You construct ray (position, direction) and calculate collision with all polygons (you should divide polygons on triangles) in scene. And find closest one. Of course you can't take collision behind rays ...
1
What about this?
min( (a1-a2+360)%360, (a2-a1+360)%360 )
The addition of 360 is there in order to avoid negative differences, because a modulo of a negative number returns a negative result. Then you get the smaller of the two possible results.
There is still an implicit decision, but I don't know how to avoid it. Basically you compare the two angles ...
Only top voted, non community-wiki answers of a minimum length are eligible
