|
| 31 Oct 2017 07:55 PM |
I've looked it up and haven't been able to find anything. I'm trying to make it so an event would fire when a part touches another part head on but not when it just grazes the corner. Is there a way to do this?
|
|
|
| Report Abuse |
|
|
|
| 31 Oct 2017 08:09 PM |
| Basically after the hit event you will want to use CFrame:FromWorldSpace() to get a its location in relation to that object then just compare the two angles. |
|
|
| Report Abuse |
|
|
|
| 31 Oct 2017 09:02 PM |
Could you link me to a wiki article or give an example? I couldnt find anything for FromWorldSpace
|
|
|
| Report Abuse |
|
|
Quasiduck
|
  |
| Joined: 28 Sep 2008 |
| Total Posts: 2437 |
|
|
| 31 Oct 2017 09:10 PM |
Approaching this problem has nothing to do with their locations. You will want to compare orientation to see if two parts have their faces line up.
You basically want to compare if one part's CFrame is just the other part's CFrame rotated by 0/90/180/270/etc. degrees. This tells you if their faces will line up.
I've implemented this below which you can use. (I recommend adding a debounce.)
local p = script.Parent
function RelativelySimilarOrientation(c1, c2, threshold) --threshold between 0 and 1 local c1rv = c1.rightVector local c1uv = c1.upVector local c1bv = -c1.lookVector local c2rv = c2.rightVector local c2uv = c2.upVector local c2bv = -c2.lookVector local pv1, pv2, pv3 = math.abs(c1rv:Dot(c2rv)), math.abs(c1uv:Dot(c2uv)), math.abs(c1bv:Dot(c2bv)) return (pv1 < threshold or pv1 > math.sqrt(1-threshold^2)) and (pv2 < threshold or pv2 > math.sqrt(1-threshold^2)) and (pv3 < threshold or pv3 > math.sqrt(1-threshold^2)) end
p.Touched:Connect(function (hit) if RelativelySimilarOrientation(p.CFrame, hit.CFrame, 0.1) then print("Hit fairly head on") end end) |
|
|
| Report Abuse |
|
|
cabbler
|
  |
| Joined: 19 Jun 2015 |
| Total Posts: 735 |
|
|
| 31 Oct 2017 09:20 PM |
| ^ Good script but I don't think rotations are very relevant to checking if "head on"? |
|
|
| Report Abuse |
|
|
Quasiduck
|
  |
| Joined: 28 Sep 2008 |
| Total Posts: 2437 |
|
|
| 31 Oct 2017 09:25 PM |
I mean, technically, he asked for corner points grazing to not count as hits and for things to hit "head"-on meaning they are facing their target straight.
This reduces the likelihood significantly of corner points grazing to count as hits.
If he also wants small surface areas being hit near the corners to not be included as hits, you would probably want to find the closest point on either cube to the other cube and test when it is in your ideal region on each cube. |
|
|
| Report Abuse |
|
|
|
| 31 Oct 2017 09:33 PM |
Thank you but I was looking more for how much of a part gets hit by another part, not if they are facing each other. The angle that the surfaces are facing each other doesn't matter as long as they touch each other close to the middle.
Its hard to explain so i drew a picture: /92285a5b2191f30ab08de2761cef587b (gyaz)
There you can see how in the first picture they are lined up square but just barely touch. In the second picture they angles are different but they hit directly.
|
|
|
| Report Abuse |
|
|
Quasiduck
|
  |
| Joined: 28 Sep 2008 |
| Total Posts: 2437 |
|
|
| 31 Oct 2017 09:54 PM |
Yes but before you start looking at "how much" of a part gets hit by another part.
The surfaces need to be facing the same directions.
i.e. If their surfaces aren't facing the same directions and they go into one-another... A single point will hit the surface first before the flat part of each surface collides.
So you need to only perform calculations when their surfaces have the same orientations relatively.
Hence, you can build upon what I've already given you.
From there, the problem is fairly easy to solve if both parts have the same size.
And is a lot harder if they have different sizes.
If they have the same size;
You can raycast from center to center. Then the point of intersection will be on the surface that collided.
Not only that, but it will also be the midpoint of the region that overlaps between both surfaces. (Can be proved geometrically.)
If they don't have the same size/ in general;
An application of a collision detection algor may be sufficient. You could use the seperating axis theorem / some variant of it to calculate the area of overlap. |
|
|
| Report Abuse |
|
|
|
| 31 Oct 2017 09:59 PM |
I think i might have to use a hitbox or something
|
|
|
| Report Abuse |
|
|
Quasiduck
|
  |
| Joined: 28 Sep 2008 |
| Total Posts: 2437 |
|
|
| 01 Nov 2017 10:02 AM |
You could also weld small parts/spheres to the corners I guess.
If they are hit before the block hits, then it has been hit in an area you don't want it to be hit.
corner parts hit => debounce
more central region hit (corners not hit) => register hit => debounce |
|
|
| Report Abuse |
|
|