Tag Info

Hot answers tagged

42

Radians are used in math because They measure arc-length on the circle, i.e. an arc of angle theta on a circle of radius r is just r * theta (as opposed to pi/180 * r * theta). When trig functions are defined in terms of radians, they obey simpler relationships between each other, such as cosine being the derivative of sine, or sin(x) ~= x for small x. If ...


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.


10

Here's my simplified, branchless, compare-free, no min/max version: angle = 180 - abs(abs(a1 - a2) - 180); Removed the modulo, as the inputs are sufficiently constrained (thanks to Martin for pointing that out). Two abs, three subtracts.


10

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 ...


7

Calculate a vector from B to A, normalize it (divide by the vector's length), then multiply by the circle size: vx = A.x - B.x vy = A.y - B.y length = sqrt(vx*vx + vy*vy) C.x = vx / length * size + A.x C.y = vy / length * size + A.y For the angle you can use the atan2 function, if your language has it.


6

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 ...


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 ...


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

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)


4

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);


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

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 ...


3

"dot" is a cosin not an angle, can not be compared to "fovRad" that is an angle You can get angle of "dot" with an arccosine function, or get the cosine of "fovRad" to compare them.


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

you can use atan2. for example: float radians = Math.atan2(object2.y - object1.y, object2.x - object1.x); and then if you want the degrees its like this: float degrees = radians * 180 / PI


3

Nathan's answer is very concrete. I'd like to supply a more general view: The most complex mathematical concept that is natively implemented in most processing units are floating point numbers as models for the field of real numbers ℝ. Visual geometriy is based on the three dimensional real vector space ℝ³. Coordinates are real numbers. Geometric quantities ...


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

Since your particles have a velocity you must have a velocity vector. Let's call the components vxand vy. You can then get the angle using: atan2(vy, vx)


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

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 ...


2

Basically you will want to do something like this: deltaX = event.x - playerX deltaY = event.y - playerY normDeltaX = deltaX / math.sqrt(math.pow(deltaX,2) + math.pow(deltaY,2)) normDeltaY = deltaY / math.sqrt(math.pow(deltaX,2) + math.pow(deltaY,2)) ... bullet:setLinearVelocity( normDeltaX * speed, normDeltaY * speed )


2

The Red is: atan2(vectorA.y - vectorB.y, vectorA.x - vectorB.x) The Green is: atan2(vectorB.y - vectorA.y, vectorB.x - vectorA.x) The Blue which I think is what you are looking for: atan2(vectorA.y, vectorA.x) - atan2(vectorB.y, vectorB.x) You can use abs() if you want the absolute value like I think you do. Sometimes you will get a value that is ...


2

It looks like your code to mirror the bullet's angle just flips the sign of the angle. This works great for reflecting an angle about the x-axis. 45 degrees becomes -45 degrees, etc. Now imagine reflecting off a wall that itself has an angle of 90 degrees. In this case, 85 degrees is reflected to 95 degrees; 45 degrees becomes 135 degrees, etc. Basically ...


2

to complete the answer: angle = atan2((C-A).y, (C-A).x) + PI/2;


1

This depends on your camera. So if the camera is directly facing the object, you can do this. The surface normal is lets say (x,y,z). z meaning the depth. Then just make z = 0, as if you are projecting it in to plane. Then angle is simply atan2(y,x). This is for the simplest case though. IF the camera is not aligned with the object then you have to figure ...


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 ...



Only top voted, non community-wiki answers of a minimum length are eligible