I need to determine via script if there exists any line between the red point and the blue sphere that is not obstructed by the green block. Is there a way to do this without hundreds of random raycasts?
not exactly. Lets use the image attached to the post as an example, and lets say the green rectangle is directly in between the red dot and the blue circle. Drawing a line from the red point to the center of the blue circle will be obstructed by the green rectangle, but drawing a line from the red point to an edge of the blue circle will be unobstructed. I need a way to tell if the blue circle is completely obstructed by the green rectangle, i.e. there is no line that can be drawn from the red point that will reach the blue circle.
I’m sure there exists a method of calculating which face/s on the green block are obstructing the red point’s view, to then find the 4-6 vertices that only 1-2 faces share (coloured orange) and then imagine the shape they would create (coloured yellow) to then use some sort of calculation to find if a point is within the yellow shapes’ area, but this doesn’t work for a 3D shape like a sphere so I don’t really know how it would apply and it sounds like it would be pretty expensive if you had alot of “green” parts
You’ve left your use-case vague so I’m not sure if this solution will help but I remember seeing a similar video where instead of casting several rays together they instead used one ray every frame on a random point on the target’s visible surface; should be better for performance but involves randomness if the ray lands on the green part several times in a row, so you would need to finetune the amount of rays you want to fire every frame + the amount of times rays need to come back as obstructed in succession before you assume it is reasonably obstructed.
The use-case is for projectile validation. I have a PVE game where i want to handle projectile registration on the client, and verify it on the server. In this case, the red dot is the player and the large circle is the enemy. I need to perform a LOS check to ensure the player can actually damage the entity, however. In this case, the sphere would be a very large entity. The actual collider for the entity is on the client, and the hit would be verified on the server. Since the server does not have the client’s collider, it gives the enemy a spherical collider that is roughly the size of the collider on the client.
In short: Client hits visible collider >> client tells server >> server makes a roughly sized sphere around the enemy >> server checks if there is LOS between the player and the sphere >> server removes health from the enemy.
The sphere check is necessary because the enemy may be partially behind a wall, but part of it may still be visible.
Fair then, the second solution kinda banked on the validation being real-time so it wouldn’t work here I was gonna explain how there might be alternative way to use the first method but it involved getting the orange points and then seeing whether they would be within the circle of the sphere’s visible face but obviously all the orange points can still be outside this area while still leaving the sphere partially unobstructed; (and the calculation might need to account for screen size and FOV? not sure)
For that to work you would need to calculate whether the orange points were outside the circle AND if any of the edges of the aforementioned yellow shape intersected with the circle, but frankly this method seems unviable too.
You might also want to reconsider the LOS collider shape if possible; you could instead use a cube to predict LOS and fire rays from the player to the “orange” vertices of this cube instead to check for obstruction? This seems more feasible, again I’m not sure how you would calculate these orange vertices, but finding the faces I do know because once you have the player’s view direction to the cube’s origin, you could then use Dot products on this view ray and the cube’s LookVector, UpVector and RightVector. For example, if ViewDirection:Dot(LookVector) > 0 then you know the cube is looking away from the player and the backside is visible, else the front will be visible, (maybe omit the side if the dot product is too close to zero though, this would mean the cube is facing perpendicular to the ViewDirection so the front/backside would hardly be visible) and so on.
Forgive me for asking ChatGPT but I racked my brain too much with the sphere scenario, got curious if I was missing anything and I wanted to mention that I asked them and they responded with the same random sampling solution
Client side:
Player shoots at mob and does a raycast from their gun to see if it hits, this reports a raycast hit position if successful. Tell the server the player hit the mob at the given position.
Server Side:
Get the position of the gun and check it is within a tolerance distance (e.g. arms length) of the player root part.
Get the position of the client raycast hit position and check it is within a tolerance distance (e.g. radius of sphere) of the mob.
This prevents a cheater moving the fire / hit positions on a large scale to somewhere with line of sight (OK, they can still cheat a bit, but at least they can’t just shoot everything on the server).
Raycast between the hit position and the gun, excluding the player and the mob. If no hit is reported then there was indeed a line of sight between the two.