Tag Info

Hot answers tagged

36

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


32

The problem with a quad/octree in nearest-neighbor searches is that the closest object may be sitting right across the division between nodes. For collisions, this is okay, because if it's not in the node, we don't care about it. But consider this 2D example with a quadtree: Here, even though the black item and green item are in the same node, the black ...


29

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


26

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


21

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


20

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

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


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


16

EDIT Disclaimer: For convenience in this answer vectors with w==0 are called vectors and with w==1 are called points. Although as FxIII pointed out, that is not a mathematically correct terminology. However, since the point of the answer is not the terminology, but the need to distinguish both types of vectors, I'll stick to it. For practical reason this ...


16

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


16

The reason for this is Pythagorean Theorem, and it's probably the bit of math I use most often in games. Even when working with a full-featured engine, there are times when knowing this math has helped me get the gameplay and look I wanted. Don't worry though, it's very simple once you've used it a few times. :) What this bit of code does is ensure that ...


15

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


15

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


15

sqrt() is monotonic, or order-preserving, for non-negative arguments so: sqrt(x) < sqrt(y) iff x < y And vice versa. So if you only want to compare two distances but are not interested in their actual values you can just cut out the sqrt()-step from your Pythagoras-stuff: pseudoDistanceB = (A.x - B.x)² + (A.y - B.y)² pseudoDistanceC = (A.x - C.x)² ...


15

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


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


15

The dot product of two vectors can tell you if they face each other or not. First vector can probably be the enemies view direction the second one should be a vector pointing from player's position to the enemies position. https://www.youtube.com/watch?v=Q9FZllr6-wY


14

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

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

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


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

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

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


12

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


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


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


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



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