Tag Info

Hot answers tagged

29

You're trying to calculate the Torque. Torque depends on the applied force F, the point of application, and the center of mass of the object. 1) Center of Mass. Define the center of mass of the object. 2) Point of Application: Define the point at which the force acts on. 3) Moment Arm: The distance between the two points defined above. Point ...


28

Don't let a math major hear you calling Vectors points or coordinates! A 2D vector has an x and y component, not coordinate. Vectors do not define a position, they define a direction and a magnitude. I can't tell you why people are intimidated by them, likely the same reason people are intimidated by math in general, because everyone says it's hard before ...


26

Compute a vector V from A to B, and normalize it. V = (B - A) / |B - A| Since the vector is normalized, it will have a length of one, and it will indicate the direction of B relative to A. If you then scale the vector by d you will have the displacement from A to C V' = d * V which you can simply add to A to yield C: C = V' + A


25

What are vectors? Vectors are sets of coordinates of varying dimension. Each coordinate in a vector represents some absolute position in that direction of the space the vector is in. A 1-D vector would be {1} . This could be, for example, a position at X = 1. Or a time t = 1. A 2-D vector would be {-4,3}. This could be, for example, a position at -4 on ...


24

Yes, you can simplify this. First, stop calling them vectors. They are points. Let’s call them A, B and C. So, you want this: dist(A, B) < dist(A, C) Replace distances with distances squared, then with dot products (from the definition of the Euclidean length. Replace AC with AB + BC (now these are real vectors). Expand, simplify, factor: dist(A, B)² ...


19

It's much faster to use a 2d cross-product. No costly trig function involved. b2Vec2 target( ... ); b2Vec2 heading( ... ); float cross = b2Cross( target, heading ); if( cross == -0.0f ) // turn around if( cross == 0.0f ) // already traveling the right direction if( cross < 0.0f) // turn left if( cross > 0.0f) // turn right If you ...


18

It sounds like you want to keep the speed of the object at some constant value over the entire curve - knowing the arc-length won't help you do this. It will help you calculate at what time the object would reach its end-point if it were going at that speed, so it will be better than what you have now (the object will have the same average speed between all ...


17

Yes. Assuming your distance function uses a square root, you can simplify this by removing the square root. When trying to find the larger (or smaller) of a distance, x^2 > y^2 still holds true for x > y. However, further attempts to simplify the equation mathematically are likely pointless. The distance between vector1 and vector2 is not the same ...


15

Shoot a ray from the camera through the center/reticle into the world. Find out where in the world it hits. Fire the bullet from the gun's muzzle at that point instead of straight out of the gun. Bonus points for animating the hands and gun to point in that direction while aiming around so the bullet still looks like it's firing straight out of the muzzle ...


14

The angle you need to rotate by is the the angle your velocity vector makes with the positive x-axis. This angle can be calculated using the inverse tan of the slope of the vector. In XNA, we use the Math.Atan2 function. Give the function the y coordinate and the x coordinate of the velocity vector (in that order). Atan2 will return an angle between +PI/2 ...


14

The simplest way is probably to get the angle of the vector using atan2(), as Tetrad suggests in the comments, and then scale and round it, e.g. (pseudocode): // enumerated counterclockwise, starting from east = 0: enum compassDir { E = 0, NE = 1, N = 2, NW = 3, W = 4, SW = 5, S = 6, SE = 7 }; // for string conversion, if you can't just do ...


13

A 2D example are screen coordinates, it identifies a pixel on the screen and has an x- and an y-component [x, y] i.e. Left upper screen position [0, 0] Another example: Imagine a text scrolling from right screen border to the left screen border. Now you need to define the velocity of the scrolling text in pixel per second, i.e. [-20, 0] which means the text ...


13

LERP - Linear Interpolation I gave this answer for a similar problem some days ago, but here we go: Linear Interpolation is a function that gives you a number between two numbers, based on the progress. You could actually, get a point between two points. The Great Formula - How to calculate it The general LERP Formula is given by pu = p0 + (p1 - p0) * ...


13

You are looking for the wondrous atan2. // v1 moving object float boxX = this.mScene.getLastChild().getX(); float boxY = this.mScene.getLastChild().getY(); // v2 user touch float touchX = pSceneTouchEvent.getX(); float touchY = pSceneTouchEvent.getY(); double theta = 180.0 / Math.PI * Math.atan2(boxX - touchX, touchY - boxY); Normally it is used ...


13

Is there any notable performance between Vector2s and Vector3s, for example when adding or multiplying them, or when calling Normalize, Transform, or Distance? Yes, you have one more coordinate so you will use more CPU cycles. But it is very unlikely that it will ever give you any trouble. XNA 4 is using SIMD extensions for vector math (EDIT: on ...


13

A texture mapping is the mapping between points on the 3D surface and their corresponding points on a texture image. If you have a 1:1 texture mapping, then every point on the 3D surface maps to a specific and unique point in the texture image (though the reverse would not need to be true. Some locations in the texture would not necessarily map to locations ...


13

dot(A,B) = |A| * |B| * cos(angle) which can be rearranged to angle = arccos(dot(A,B) / (|A|* |B|)). With this formula, you can find the smallest angle between the two vectors, which will be between 0 and 180 degrees. If you need it between 0 and 360 degrees this question may help you. By the way, the angle between two parallel vectors pointing in the ...


11

You seem to be trying to implement the Painters Algorithm. I'm guessing you're trying to write a rasteriser from scratch as a learning exercise, as most modern 3D hardware uses what Bart has mentioned (the Z/Depth buffer). For the painters algorithm to work in all cases, you'd need to be prepared to subdivide the surfaces as they're rendered to solve ...


11

If I understand your problem properly, you should just have a direction Vector2 representing the direction you want to move in inside your sprite class. Like this: public Vector2 Direction { get; set; } This is the normalized vector(which means it has a length of 1) showing where you want to go. Then, add a Speed float property, which says how fast the ...


11

From MathWorld: Given the plane Then the normal vector is The normal unit vector n is given by: Therefore, for the plane 5x+2y+3z-1=0, The normal vector N is N = [5,2,3] The magnitude |N| is |N| = sqrt(5^2 + 2^2 + 3^2) |N| = 6.1644 The normal unit vector n is therefore approximately: n = N / |N| n = [0.8111, 0.3244, ...


10

This is a simple operation of vector difference. In XNA you don't even need to create a specific method for it - vectors already support subtraction. If you want the velocity of Body B from the perspective of Body A, you subtract Body A's velocity from Body B. So in C# with XNA, using your example: Vector2 MovingBody = new Vector2(1, 0); Vector2 Impact = ...


10

Think about the problem differently. You want to object always to "face" the player, which means you want its "forward" vector rotated around to be parallel to the vector from itself to the player. Assuming its "forward" vector is normally at obj.Rotation = 0, the proper rotation is basically the arctangent of Vector2.Subtract(playerPos, objectPos). Most ...


10

Blue vector can be calculated easily: red - black (the sign between vectors is minus). But if you want just to interpolate between black and red vector, you don't have to calculate it. Linear interpolation is just linear combination. So you can just take: alpha * black + (1 - alpha) * red, where alpha has to be from interval <0,1>. If alpha will be 1, ...


10

Generally speaking, a Normal vector represents the direction pointing directly "out" from a surface, meaning it is orthogonal (at 90 degree angles to) any vector which is coplanar with (in the case of a flat surface) or tangent to (in the case of a non-flat surface) the surface at a given point. A Tangent vector is typically regarded as one vector that ...


10

Vector3 vT = v2 + headingNorm * 3; Be careful though, if v2 and v1 happen to be closer than 3 units away this will put you on the far side of v1. Maybe you want this to make the unit step back to make room for the attack. But then again be careful, because that means as you approach that attack point you will overshoot then correct and overshoot the ...


9

The native Vector2 class doesn't seem to have half of this stuff, but it is used in a lot of places. The Vector3 has all this stuff, but has an extra dimension I don't need. I believe you're mistaken; Vector2 and Vector3 both have extensive functionality. Note that this functionality is exposed as static methods to the Vector2/Vector3 class, rather than ...


9

There are a few things to consider here. The first is that a face is not necessarily rotated just because its normal is not aligned with an axis. The second is that you can't obtain Euler angles (x,y,z rotations) from just a normal. You would need to know at least 2 non collinear vectors to do that as you need three perpendicular vectors (a basis in R3) to ...


9

Use arcsin of the 2D cross product (i.e the z component of the cross product vector). That'll give you -90 to 90 which will let you know whether to go left or right. Be careful because A cross B is not the same as B cross A. Another strategy (but probably not as straight forward) is to calculate the "heading" of the two vectors using atan2 and then ...


9

My answer to this question would be the same as my answer to this other question: When should vector/list be used? Basically, cache-coherency-related performance gains from having everything be contiguous in memory is more valuable than "Big O" algorithm theoretical performance from linked lists.


9

I fear the subject is quite tricky, few multi platform solutions seem to have launched, and even fewer seem to have survived on their own. I was looking into the subject a few months ago. I had a constraint as I needed the engine to run on iOS and Android. Didn't find anything that suited me really at the time. But a few pointers from what I remember: each ...



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