Fastest way to determine line of sight on a grid - unity3d

I am improving a video game of mine where I currently have the problem that NPCs can shoot through walls. In order to fix this issue, I have decided to completely re-do my work regarding ground combat.
I use a tight grid for finding paths from any point A to B. I store my grid as an image-like structure and use Jump Point Search for the pathfinding itself. My goal is to find a location that is as far away from the enemy as possible (limited by the firearms's range) that also has a direct line of sight to the enemy.
In my latest approach, I determined all nodes in my grid that are within weapon range and that are connected to the enemy's location (colored in green in the image below). The final missing part is filtering these points so that only the points with a valid line of sight remain.
I can also detect the border lines (colored in blue) very fast. I could check for every node, if a line from this node to the enemy would intersect any border line. If that is not the case, I have clear line of sight. However, I assume this to be neither fast nor the optimal solution.
Do you have any ideas or suggestions? I am glad for any hint!
I visualized the grid in the following image to give you an idea of what I am talking about. If a character would stand in the lower right green area, he could not shoot the enemy in the upper left area because of missing line of sight.

Related

Holes in 2D circle collider

I try to begin new game development that has rotating circle that has one or many holes as in the attached image , the problem is I need to use circle collides for the big circle., in addition I need to use small collides for the smaller holes to prevent and particles(smaller circles ) input the circle and to count no of collisions occurred
enter image description here
The circle collider itself does not have this functionality since it can only check collisions between perfect circles. (Behind the scenes all circle colliders just check the distance between points and these distances do not work with holes) The best solution I can think of would be to use a polygon collider and make a rough circle that you can then remove parts of (would require some advanced geometry but not impossible). I have tried simillar things in 3D and with the mesh collider, removing parts of it using a method known as CSG (Wikipedia) and something simillar should be possible in 2D however it is really, really hard and even experienced programmers struggle to come up with a system that can handle every scenario.
All hope is not lost though, depending on what you need the collision for, there might be other ways. If you just need to check if two objects intersect and don't want rigibodys and stuff to interact with the collider just checking the distance between the big circle and the object (too figure out if it's close enough) and then checking the distance between the object and the holes (too see if it's inside a hole on thus not colliding) is the best way to solve the issue.
I'm sure someone who have done more in this field than I have will be able to help you out but without more information of what you need it's impossible to say. It all depends on the circumstances!

Can you do pathfinding based on the pixelgrid of a .png file in Unity?

TL;DR: Can someone please help with pathfinding with no obstacles, fixed and known starting points, and edges based on transparency of the pixel grid of a .png file.
I'm trying to make a simple app for my students to teach them the correct stroke order and direction of the Chinese strokes.
So far I have achieved this by layering "start" and "end" game objects with CircleCollider2D components on top of the PolygonCollider2D generated by the sprite to check if they started the stroke, stayed within the stroke, and exited it correctly.
It does the job, yes, but it doesn't animate the fill in process like you'd expect from such an app, not to mention that I need to manually add "start" and "end" points myself.
Ideally I could just provide the stroke sprite, tell it which way I want the stroke to go (left to right, right to left etc.) and let the program create the ends based on the first/last 10% of the pixels, and of course animate it to fill in once completed correctly.
But baby steps!
First, I'd be grateful if someone could please tell me how to even get the pixel grid to begin with so I can perhaps attempt an A* approach.
Thank you!
This is the same case for validating AI racers if they are in racing in the correct way. You will have to indicate some sort of waypoint system that has is ordered by the way of strokes you want.
Imagine you're teaching them to write the number 2. You will have to create an array of nodes starting from the upper left most of the number until you get to the other end. You can validate the strokes if their fingers pass through the correct order or not.
No need for a complicated A* algorithm.
However, this won't do if you want to automate everything. This will require you to do some sort of image processing, editor tool, and loads of validations. I wouldn't suggest the automated one though.

I want to check if a line between two vector points falls into a particular area or a set of vector coordinates?

Note : I am coding in Unity, using C# script. I cannot use trigger hit detection using Raycasting because there are a lot of trigger colliders in between the target and source which detect touches and the sort. So the ray hits the other triggers before even reaching it's target, which is not desirable.
What I want to accomplish basically is return a boolean value if a vector line crosses or intersects a particular set of vector coordinates or an area.
For example: Detecting a laser entering into a fog in between it's path when shooting at it's target. The fog is a trigger collider based game object.
Edit: Another example would be to check if a line crosses a 2D box area in a 2D graph. Keep in mind, I cannot use collision detection or Raycast hit here.
There is no need for code, just explain the concept of how it could be accomplished. Though a code snippet is also welcome. Thankyou!
[...] So the ray hits the other triggers before even reaching it's target, which is not desirable.
What about putting that on Layers? You can specify LayerMasks for Raycasts.
You could consider the edges of the 2D box as lines i.e 4 lines and check for intersection of a line with these 4 edges using line to line intersection technique. If the line however is small enough to fit inside the box, and you want to treat it as a valid intersection, then check if the two points of the line is inside the bounds of the box.

Preventing an object from landing on its side; ensuring it always lands face-up or face-down

Currently I am doing a project of shell toss in Unity. The shell is made in Blender and imported in Unity. I attached a single box collider to the shell.
Now when I apply the random force to the shell to flip it, it sometimes rests in the vertical up position when either a result of heads (face-up) or tails (face-down) should have occurred.
I have tried changing the axis, increasing the gravity. But none of these solutions worked.
What I have done for now is when the shell is in a resting position after it lands, I rotate the shell 90 degrees - but this is not a great solution, since it is slow and takes time to check whether the shell has come to rest or not.
I am looking for the better idea so that the shell should only rests in a heads or tails state.
If you use a box collider for your shells, you are going to end up in this problematic situation where the flipped shell sometimes ends up sitting sideways, instead of face-up or face-down.
In this situation, I would recommend making use of Capsule Colliders, which are cylindrical colliders with a rounded top and bottom. If a capsule collider lands on either end, it'll fall over sideways:
However, one capsule collider isn't enough - otherwise, the shell will start rolling around after it falls over. I suggest 2 or 3, oriented in a cross such that their tips correspond to the sides of the shell:
Now, if the shell lands on any of its sides, it will topple over onto one of the faces of the cross - either face-up, or face-down. To add capsule colliders to your GameObjects, just click on Add Component, and select Physics > Capsule Collider. Then modify the properties to get them into the desired positions:
The Direction you choose should probably be X-Axis and Z-Axis. Once you have the correct orientation, change the Center value to move the colliders to the right positions to make a cross through the shell. Then, alter the Radius of the colliders to affect the thickness of the cross, and the Height so their tips line up with the edges of the shell.
Hope this helps! Let me know if you have any questions.

iOS - Dragging objects along curved paths

I am tearing my hair out trying to figure out what seems to be a very easy problem. I know a lot of this stuff has been talked about tangentially, so apologies if this treads on well-covered ground, but I can't find anything specific to my solution (believe me, I've looked).
Basically I want to drag an object/sprite along a pre-defined, curved path (not just move it, but DRAG IT). Think of the iPhone's "Slide to unlock" thing, but instead of just dragging the slider left-to-right, make the path an arc or a wavy line.
My basic thinking was:
define a bezier path, set the object at the start point.
if the object is touched, check for hit detection on the bezier path in touchesMoved (or some similar function). if touches stay on the path, advance the sprite along the path until the path ends (in which case, task is finished) or the user's finger goes off the path (in which case, the object should go back to the beginning).
None of this is trivial (at least, that's how it seems). For example:
Doing hit detection on a Bezier path is a royal pain since you actually need to do it on the stroked portion, not the fill portion. And even then, I can't seem to find a way to do it on a path of any width -- only on the 1-point-wide path of the Bezier.
Moving an object partially along a path doesn't even seem possible: all of the animation methods move the sprite along the ENTIRE path. Also, doing this requires you to find the point on the path closest to the user's touch, which, if you've ever looked this up involves astoundingly complicated math.
I've thought of using rigid bodies to occupy all of the space EXCEPT the path, so the object can only move in the path. However, this requires the definition of curved rigid bodies some of which must be concave. Dead end.
Am I making this too hard? It doesn't seem that complicated. I don't need a whole solution, just a new way to think about this and kick in the right direction. Any help would be really appreciated.
How about this?
Consider the X Axis of your bezier
path.
Each time the user taps or interacts with the screen just look at the x portion of the touch
Map that X Coordinate with your path and move the object to the right position.
Yes, you are making this too hard.
Take the simplification suggested above (or along a circle, line, etc) if it works for, or if you really want to do it against a bézier curve, consider the following:
Look at the definition of the bézier curve
What you're looking for is to define a new object position P' from a current position P and a change in touch position D.
If you rephrase the original P(x,y) in terms of t (bézier curves are parametric), then the problem becomes finding how much t offset to add based on D.
Something involving the differential of the bezier fn at P might be a good way to do that. Ie, how much t would have been added had the curve just been a straight line coming from point P along the curve.
EDIT:
Transition between segments:
If each segment has t in [0,1), then you can detect t >= 1 and move on to the next segment, setting P to the end of the previous segment, and evaluating the movement again in relation to that point. There might have to be some heuristics involved if you have a lot of small points, etc.