55

This can be explained with the Pythagorean Theorem, which is the following formula: a² + b² = c² In your case, when moving right, you're using (x:1, y:0) which gives us c² = 1 + 0 = 1 c = sqrt(1) = 1.00 When moving up and right, you're using (x:1, y:1) which gives us c² = 1 + 1 = 2 c = sqrt(2) = 1.41 So as you can see, the length diagonally is longer ...


54

No this isn't an engine bug or an artifact of a particular rotation representation (those can happen too, but this effect applies to every system that represents rotations, quaternions included). You've discovered a real fact about how rotation works in three-dimensional space, and it departs from our intuition about other transformations like translation: ...


52

A perfectly straight line would also be the shortest possible line with a total length of sqrt((x1-x2)² + (y1-y2)²). A more scribbly line will be a less ideal connection and thus be inevitably longer. When you take all individual points of the path the user drew and sum up the distances between them, you can compare the total length with the ideal length. ...


36

Does that mean that the magnitude is simply it is distance from the origin point (0, 0, 0)? That's exactly that. Among other things, a vector can represent a point (a position), a direction and/or a velocity, depending on the context. If you have this variable: Vector3 mPosition; It generally represents only the position, i.e. where it is located in ...


34

The solution is actually simpler than expected. The trick is to use Minkowski subtraction before your hexagon technique. Here are your rectangles A and B, with their velocities vA and vB. Note that vA and vB aren't actually velocities, they are the distance traveled during one frame. Now replace rectangle B with a point P, and rectangle A with rectangle C =...


31

Yes, the Manhattan distance between two points is always the same, just like the regular distance between them. You can think of the Manhattan distance being the X and Y components of a line running between the two points. This image (from Wikipedia) illustrates this well: The green line is the actual distance. The blue, red and yellow lines all represent ...


31

This might not be the best way to implement this either, but I suggest a RMSD (root mean square deviation) could be better, than merely the distance method, in cases mentioned by Dancrumb (see first two lines below). RMSD = sqrt(mean(deviation^2)) Note: The sum of the absolute deviations (integral-like) might be better, as it does not average out positive ...


31

You can use Gauss' shoelace formula: You need to take the x coordinate of every point, multiply them by the next point's y coordinate, then subtract the current point's y coordinate multiplied by the next point's x coordinate from the result and add them to the total area. After you did this for every point, halve the total area to get the actual area of ...


29

You simply need to project vector AP onto vector AB, then add the resulting vector to point A. Here is one way to compute it: A + dot(AP,AB) / dot(AB,AB) * AB This formula will work in 2D and in 3D. In fact it works in all dimensions.


28

You could easily find the normal by calculating two vectors, V1 = P2-P1, and V2 = P3-P1, and then find the cross product N = V1 x V2. Then you normalize N. Depending on the ordering of the vertices (clockwise or counterclockwise) you will get a normal facing front or back. You also need to make sure that three three points aren't aligned, if they are you ...


27

Draw a line to infinity and count how many times you cross the shape (even or odd), not counting the segment where the creature lies. Then check whether the creature is going left or right of that line. In this example, we cross the shape twice (so even) and we go to the left. The result is immediate from this table: # Crosses | even | odd Direction ...


27

You've miswritten the formula. x = x * sqrtf(1.0 - (y*y/2.0) - (z*z/2.0) + (y*y*z*z/3.0)); y = y * sqrtf(1.0 - (z*z/2.0) - (x*x/2.0) + (z*z*x*x/3.0)); z = z * sqrtf(1.0 - (x*x/2.0) - (y*y/2.0) + (x*x*y*y/3.0)); You modify the original x and overwrite it. Then you modify y based not on the original x but the modified x. Then you modify z based on the ...


27

If (x,y) is the centre of the rectangle, the squared distance from a point (px,py) to the rectangle's border can be computed this way: dx = max(abs(px - x) - width / 2, 0); dy = max(abs(py - y) - height / 2, 0); return dx * dx + dy * dy; If that squared distance is zero, it means the point touches or is inside the rectangle.


23

Existing answers do not take into account that the end points are arbitrary (rather than given). Thus, when measuring the straightness of the curve, it does not make sense to use the end points (for example, to calculate expected length, angle, position). A simple example would be a straight line with both ends kincked. If we measure using the distance from ...


22

The four-variable representation of a plane is the coefficients in the equality ax + by + cz = d This can be seen as N = (a, b, c) being a normal vector and d being a distance from the coordinate origin (in units of the-length-of-N), and we can also write this equation as N·P = d, where P = (x, y, z). This representation does not allow defining a specific ...


22

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


20

Does that mean that the magnitude is simply it is distance from the origin point (0, 0, 0)? The tl;dr answer may be: Yes, you can imagine it like that. But I'm not sure whether this might not lead to a wrong understanding. A vector is not a point, and there is a crucial difference between the two! The fact that a vector is usually represented as an "...


17

First of all, in the case of axis-aligned rectangles, Kevin Reid's answer is the best and the algorithm is the fastest. Second, for simple shapes, use relative velocities (as seen below) and the separating axis theorem for collision detection. It will tell you whether a collision happens in the case of linear motion (no rotation). And if there's rotation, ...


16

A simple way to build a procedural generator is: Randomly build things Run a function that checks whether the output is good If the output is not good, go to step 1 Even if it takes thousands of runs to complete, most simple generators get by just fine with this approach. The advantage is that there's not a lot of smarts required in the generator, and ...


15

With i going from 0 to n-1 inclusive: pointX[i] = ( sin( i / n * 2 * PI ) * radius ) + xOffset; pointY[i] = ( cos( i / n * 2 * PI ) * radius ) + yOffset; Edit: As Lars Viklund mentioned in the comments, this is only safe in languages like javascript in which integer division returns a floating point number rather than a integer. In other languages you ...


15

It seems to me that having the center of your space be (0, 0, 0) is better. Assuming you are using a signed format to represent positions in space, a center of (0, 0, 0) allows you to use both the negative and positive parts of your format, which can do wonders for precision for floats and range for signed integers. This may not matter for small scales, but ...


15

The circumcenter of a triangle can be found by the following formula, which I mined from an old posting by Jonathan Shewchuk from the Geometry Junkyard $$\begin{align} &\text{Triangle in } \Bbb R^3\text{:}\\ &m = a + \frac {\lVert c-a\rVert^2 \left[(b-a) \times (c - a) \right] \times (b-a) + \lVert b-a\rVert^2 \left[(c-a) \times (b - a) \right] \...


15

Sphere-Sphere Intersection Let's start with the more obvious one - sphere-sphere. It's almost identical to the circle-circle case in 2D. We can project down on any plane containing the line between the spheres' centers to get an identical 2D picture: Here the first sphere has center c_1 and radius r_1, the second c_2 and r_2, and their intersection has ...


15

I'll assume your t goes from 0 to 1. (If not, just multiply to scale it appropriately.) Figure out what proportion (0–1) each side is of the perimeter. (side length / total perimeter) To find how much of every side is “filled in” at time t, iterate through sides, subtracting their proportions until t is depleted to a negative value. That last edge (which ...


15

First, we can convert your source rectangles to cells in your underlying grid, to make the input more uniform. (Effectively rasterizing the problem) This will let us find optimizations that might not be obvious when working directly with the source rectangles - particularly when it involves splitting multiple source rectangles to recombine them differently. ...


14

The problem here is that Populous maps are not spheres at all. They're a Real Projective plane, more analogous to a flat torus manipulated on screen to look like a sphere than an actual sphere. For example: Populous maps are squares when viewed outside of the populous engine Populous maps ingame are visibly warped to show/hide the curvature by moving from ...


14

Given a "root" curve, here's how you might generate block vertices. The root curve is in the middle, in black. Its control points are shown with red Xs. In short: I made a Bézier and sampled it (at a configurable rate). I then found the perpendicular vector of the vector from each sample to the next, normalised it, and scaled it to to a (configurable) half-...


14

Actually no, the 'job' of the geometry shader (GS) is primative evaluation. Geometry shaders can tesselate, but they are limited by a) an in-process upper bounds on the number of output elements, and b) execution within a single shader...of course shader instancing aleviates the 2nd issue, but overall geometry shaders are more effective at primative ...


13

One way to do it, considering you want a 90° angle, is to find the cross product of the normal and gravity, normalize it, then cross that with the normal again. In your diagram, the first cross will produce a vector pointing into the screen, and the second cross will produce the flow vector. An interesting side-affect of using cross products is that ...


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