21

When I had this problem while working on my Cubes, I found the paper "A Fast Voxel Traversal Algorithm for Ray Tracing" by John Amanatides and Andrew Woo, 1987 which describes an algorithm which can be applied to this task; it is accurate and needs only one loop iteration per voxel intersected. I have written an implementation of the relevant parts of the ...


18

What is a raycast? What are blocking objects and blocking mask? A ray is a mathematical device that starts at an origin point and continues on in a specific direction forever. With a raycast you're casting a ray, cast being used like the word throw. It's like if you threw a rock and it continued on in that direction forever, it wouldn't stop until it hit ...


13

Amit Patel has written a very nice article on 2D ray casting. This involves casting rays to each of the vertices inside the range of the light source to build a light mesh. All of the visual examples are interactive in the post and very easy to understand. You don't have to limit yourself to a box either, the perimeter you trace can be any shape you like....


12

This really is just the way things are with collision. Not just 3D, but also 2D. Take the following example: The green and red AABBs are colliding, and the contact manifold is the blue area. The contact points will be in the blue area somewhere (exactly where can vary with algorithm, but the corners where the blue/red/green meets are ideal-ish). What ...


10

Break movement into two steps Select a position on a 2D X/Z plane located at the entity's current Y position. Set the offset (up or down) from that plane. This is similar to how Homeworld, one of the first 3D space RTS games handled movement. Works really well. Breaking movement into two steps provides the most control, simply because the mouse is a 2D ...


8

Ahh yes. I threw my math at it and I think I hit it. You're correct it does involve the Pythagorean theorem and some scaling. You start with your normalized vector that represents your ray. It has an x component and a y component. First we want to see how long it is when it travels one unit in the x direction. So what do we do? We want to scale the entire ...


8

You could try casting "shadow arcs" to cover larger areas at once. While the actual details are a bit involved, Eric Lippert has a very in-depth explanation (with live Silverlight demo) at http://blogs.msdn.com/b/ericlippert/archive/2011/12/12/shadowcasting-in-c-part-one.aspx.


8

It seems you include a square when you can see any point in it while the eye is at the single fixed point. However, you should only include a square when you can see the middle of it (assuming the "eye" is also in the middle). Or you can draw rays from each corner of the starting square to the corners of the tested square, if you can see any from any then ...


8

Transforming the ray position and direction by the inverse model transformation is correct. However, many ray-intersection routines assume that the ray direction is a unit vector. If the model transformation involves scaling, the ray direction won't be a unit vector afterward, and should likely be renormalized. However, the distance along the ray returned ...


7

Eric Lippert wrote an excellent series on generating line-of-sight in C# with Shadow Casting on a rectangular planar grid.. Amongst other issues, Eric dealt with various questions that must be answered about the line-of-sight requirements, which give different results, and gives examples of a couple of different results. One of the articles deals in depth ...


7

You need to use the range instead of the distance when you cast the ray. The range is the length of the projection of the ray onto the camera plane. See this source. Though IMHO some amount of fisheye distortion is kind of cool to keep around.


7

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


6

Two that you're missing which immediately stand out to me are GJK and MPR. GJK is an algorithm for finding the closest point of two convex polygons. With a little bit of extra work you can use it to find incident points for intersecting objects, and hence calculate a collision manifold. This is done via polygon clipping, same as if using SAT, but GJK ...


6

Real Time Collision Detection does indeed have this information - look at section '5.3.2 Intersecting Ray or Segment Against Sphere', page 178/179 in my copy. I'm not sure if it is okay to reproduce the code but I found many copies of it online (google books, for example) - here it is verbatim from Real Time Collision Detection: UPDATE: The following ...


6

There is a (optional) parameter for that in Physics.RayCast which is available in Unity 5.2 and up: public bool RaycastNoTriggers(Vector3 startPosition , Vector3 direction, float distance) { Ray ray = new Ray(startPosition, direction); RaycastHit hit; return Physics.Raycast(ray, out hit, distance, DefaultRaycastLayers, QueryTriggerInteraction....


5

I have implemented the algorithm suggested by Jimmy. Video of the code in action here: https://youtu.be/lIlPfwlcbHo /* What this code does: Rasterizes a single Field Of View octant on a grid, similar to the way FOV / shadowcasting is implemented in some roguelikes. Clips to bitmap Steps on pixel centers Optional ...


5

Assuming you've benchmarked this and are sure this is a bottleneck keep reading. If not stop. Don't worry and be happy :). It's true that you will need to run the collision check algorithm every time you fire a bullet. And depending on how long it takes the bullet/laser to disappear you will have to do it multiple frames. However when you implement a solid ...


5

You can calculate the ray from camera for any pixel px,py with the following formula: ray.origin = camera.getPosition(); relativeX = px * 2 / screenWidth - 1; // [0, screenWidth] -> [-1, 1] relativeY = py * 2 / screenHeight - 1; // [0, screenHeight] -> [-1, 1], might need to be flipped depending on the coordinate system ray.direction = vector(...


5

Writing up an answer from my comments as I believe it's not such a crazy idea after all. Assumptions The "Up" direction should not matter in these calculations You have found the object in focus and you can ray cast from its front just like you did with the character The "Up" direction is Z. (This could be Y depending on who you are talking to or what ...


5

I've had this happen with the 3D raycast in the past, and it seems to be because the raycast functions have so darned many optional parameters, and LayerMasks aren't picky about what type they're treated as. From the docs: public static RaycastHit2D Raycast( Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, ...


5

It's called a flood fill, and it's what you see in paint programs. It's very fast. Pseudocode: declare visited list //the results you want declare unvisited list add current element (where red dot is) to unvisited list while unvisited list not empty get current element from unvisited list add current element to visited list for all (8) neighbours ...


5

I'd recommend testing empirically to confirm, but the second answer at this link says that triangle data for submeshes is indexed sequentially in order of submesh index. That means all the triangles for submesh 0 have lower indices than those in submesh 1, etc... So, you can iterate over the submeshes until you find the one your triangle belongs within: ...


5

Physics.OverlapSphere() and Physics2D.OverlapCircleAll() will return all colliders within a radius of a point. Those colliders are in range, but possibly not in view. If you are going to do this often, create an array once, and pass it to Physics2D.OverlapCircleNonAlloc(), instead. To see if they are in view, check the dot-product of the normalized collider-...


5

You could use a Dictionary. Store all Transforms as a key with reference to each Car. In this example, we have a god Game class that holds references to all Cars. (just make sure your Script Execution Order has Game execute before Car does) Each Car class adds itself to the Game's Car Dictionary upon Awake. Then in your Raycast, you get the Car directly ...


5

SphereCast methods take a sphere and slide it along a line to see where it hits objects along its travel. The radius parameter is the size of the sphere. The maxDistance parameter is how far the sphere should travel in the given direction. To simply check for objects near your player, you want CheckSphere or OverlapSphere / OverlapSphereNonAlloc instead. ...


4

Figured it out. What you have to do is raycast from your point in any direction you want. Then, count how many times the raycast intersects with your mesh. If the count is an odd number, it is inside of the mesh. If it is even it is outside of it. Since unity doesn't raycast on inside sides, you have to flip the faces in your 3d authoring tool. Then you ...


4

A Wolfenstein-style raycaster is really simple. You basically shoot beams horizontally in an arc from one side of the player view to the other edge. When a beam encounters a wall, you get the wall slices height on screen from the distance (e.g. scale_factor / distance). E.g. for this height calculation you don't need to understand anything about matrices etc,...


4

I think this is what you want: Physics.SphereCast From the docs: Casts a sphere against all colliders in the scene and returns detailed information on what was hit. This is useful when a Raycast does not give enough precision, because you want to find out if an object of a specific size, such as a character, will be able to move somewhere ...


4

Your problem is not the ray cast, but that your grid cell doesn't reflect its actual state or in other words it might contain objects that it doesn't know about. When a certain object intersects a grid cell, the grid cell should know about it, this way when an object intersect multiple grid cells each grid cell should have a reference to that object, this ...


4

The physics raycast is a different system and doesn't see UI elements. So you can not cause the UI to block physics raycasts. However you have options: What you can do is cast two rays. One raycast for your UI elements and one for your physics objects. If your UI raycast detects a UI element, you know a UI element is in the way, and you shouldn't cast your ...


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