# Tag Info

33

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

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

10

You have to do spatial partitioning, in this case you make an efficient data structure (usually an octree). In this case each object is inside one or more spaces (cubes) And if you know in which spaces you are you can look up O(1) which spaces are your neighbors. In this case the nearest object can be found by first iterating over all objects in your own ...

7

You'll want to use 3D picking. Here's some code I use in my game. First I cast a ray from my camera. I'm using the mouse, but if you're just using where the user is looking, you can just use the center of the window. This is that code from my camera class: public Ray GetPickRay() { int mouseX = Mouse.getX(); int mouseY = WORLD.Byte56Game.getHeight()...

6

Instead of D3DXVec3Unproject() use XMVector3Unproject(). It accpet float parameters for viewport: XMVECTOR XMVector3Unproject( [in] XMVECTOR V, [in] float ViewportX, [in] float ViewportY, [in] float ViewportWidth, [in] float ViewportHeight, [in] float ViewportMinZ, [in] float ViewportMaxZ, [in] XMMATRIX Projection, [in] XMMATRIX ...

5

The view matrix transforms from world space to view space. Your mouse is in view space and you want to transform it into world space. That's the opposite of what the view matrix does. But the solution is simple - just multiply by the inverse view matrix instead. Matrix inverseViewMatrix = Matrix.Invert(camera.ViewMatrix); Vector2 worldMousePosition = ...

5

How do you want this to behave? There are different ways to do this. A simple option is to just move the object by some fixed number of world space units for each screen space unit (pixel, say) that the mouse moves. Another option is to take the vector of mouse movement and project it onto the axis of movement through the normal projection/camera matrices....

4

This is known as 3D picking, and usually involves some kind of raycast from a point in screen space (the cursor position) in the camera's forward vector direction. For some Ogre tutorials, check those links: http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Intermediate+Tutorial+3 http://www.ogre3d.org/tikiwiki/ThreeDeeObjectPicking http://www.ogre3d.org/...

4

Instead of using a collider for your terrain, you can also use a plane to perform a raycast. For example, if your grid is situated on the XZ plane, you can do something like this: static Plane XZPlane = new Plane(Vector3.up, Vector3.zero); public static Vector3 GetMousePositionOnXZPlane() { float distance; Ray ray = Camera.main.ScreenPointToRay(...

4

Your depth buffer does not contain "entities". It contains the stuff you rendered. So if you read the depth buffer, you will read the depth for the closest stuff you rendered. So either you make a special depth-only rendering pass that does not contain "entities", or you're going to have to do something else to convert from the user's selected location to a ...

3

OK, so I found your problem. I don't, however, know why it's the problem. I plugged your code into mine, everything was the same except the matrices. The matrices retrieved from OpenGL did not match the ones I was generating. So, if you would, try upgrading your camera class to generate those matrices for you. There's a few functions you'll need: Matrix4f ...

3

It depends on how you implement picking. It may not matter how you draw it, because you won't be detecting a face that's been drawn already. Since it's already been sent to the graphics card, it's usually more work to get information back from the drawn image than just checking your objects directly. If you implement picking in the way I've described here, ...

3

If you want do it this way (render colors for color picking and use texture for texturing in one rendering), I think you have to use frame buffer objects or / and shaders. You can use only frame buffers but you have to render your scene twice - first use only colors and store output image (render) to frame buffer, then draw scene normally (with textures) ...

3

When I was debugging mouse picking in my game, I had it set up to draw a line from my camera to where ever the picking function ended. It lets you know if the ray is going the right direction, and if the ray is ending too soon, or too late. In your case, I'd have it draw a line between nearPoint and farPoint. Make sure those are the rays you want. I ...

3

You can treat this as a conversion from spherical coordinates to Cartesian coordinates. Assuming the ray should have some length r, your x, y, and z components can be calculated like this: x = r * sin(mPitch) * cos(mYaw) y = r * sin(mPitch) * sin(mYaw) z = r * cos(mPitch)

3

The technique you're looking for is called "picking" or "3D picking." There are several ways to do it; one of the more common ones is to transform a 2D point on the screen into eye space by using the inverse of the projection transformation. This will allow you to generate a ray in view space, which you can use to test for collision with the physical ...

3

I think you may be confusing some terms. Theme Hospital is diamond shaped from what I can tell. That's the shape of the tiles, not the entire world. You can shape the entire would however you like. The shape of the world would be the same as the shape of the tiles if the world was square (meaning equal width and height), but you don't need to make it that ...

3

If your hit test is really that simple, then instead of doing more complicated triangle intersection as Byte56 suggests you could do simple AABB-ray intersection test. The intersection test will tell you the point that was hit. This point in model space will lie on one of the faces (so one of X, Y, or Z will be +/-h, where h is the half-width of your cube)....

3

First of all, you need to attach a collider to your terrain in order to get hits from raycasts. If you're using a Terrain, it should already have a Terrain Collider attached to it. In your script you can then do something like this: // create a ray from your mouse-position Ray r = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(...

3

When you have your mouse-picking working all you need to do is overlap your map with the object you wish to place. This problem would be the same for an isometric grid as for a rectangular grid. Take this exmaple map: And this building: On this map the green squares are meant to as grass, the brown squares as mountain and the blue squares as water. ...

3

Pixel picking is effective if you need to do lots of raycasts from one source. You bear the overhead of rendering and readback once, then get all your many picks at low incremental cost each. To get just one ray result you've already had to calculate thousands or millions whether you use them or not. If you're only doing a few raycasts at a time (say a ...

2

Pardon the brevity as I'm posting from my phone. Unproject to get your ray. Transform the ray by the transpose of your view matrix to get its position in world space. Now, either: A. For each model, transform the world ray by the transpose of the model's world matrix and test the meshes for intersections, or B. Test the ray against the transformed ...

2

I recently attempted to implement picking, and this thread was invaluable in getting things to work. One problem that I ran into though is that I don't think that the accepted answer generates the right view matrix in all cases. Let me suggest an alternative for obtaining it, which is much simpler and more likely to be right - it just pulls the matrix ...

2

If you are rendering with one different single color for each pickable item, then just compare the color under the mouse against the background color.

2

This isn't a complete answer in itself, but this wouldn't fit into the comments below your question. If believe this part of the Intersects function is what should be disallowing reverse facing triangles to be given as a result: float triangleU; Vector3.Dot(ref distanceVector, ref directionCrossEdge2, out triangleU); triangleU *= inverseDeterminant; if (...

2

It is hard to say exactly what is broken, but I can make a few suggestions. Generally, the front buffer is what is currently shown, and drawing calls that are being processed are drawn to the back buffer. Once the entire scene is rendered, the back buffer is copied to the front buffer. Therefore I think that you should read from the back buffer, because ...

2

As Steven Stadnicki suggested in his comment, and what is also suggested in a related question over at stackoverflow, just do a linear search without any hierarchy.

2

I'm not used to coding in Java, but I managed to fix it for you using code from Horde3D. // Algorithm courtesy of Horde3D // h3dutPickRay public static Ray getPick(int mouseX, int mouseY) { // Mouse position in NDC [-1, 1] Vector2f mouse_ndc = new Vector4f( (float)(mouseX / 960f) * 2f - 1f, // TODO: Hardcoded viewport (float)(...

2

I ended up changing the reticle to be rendered by the game instead of the GUI. All is well now and glad my math was solid from the beginning.

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