Currently I am working on a magic system for my game. Currently projectiles to be specific. I’m trying to figure out a good way to prevent a possible exploit.
I can fire a remote to cause the projectile to explode where I want. the solution I have for it is simple to write down but I can’t think of the math needed. basically I need the math to be able to calculate if the explosion point is between the start and end point
here is a function I tried to make but it doesn’t work that well and sometimes returns false when its supposed to be true. I have tried looking up solutions but I get answers for problems different than my own.
function IsPointBetween(Point1, Point2, PointBetween)
local Discrep = 5
local function CheckPoint(Point1Axis, Point2Axis, PointBetweenAxis)
if Point1Axis < 0 then
Point1Axis = Point1Axis + (-1*Discrep)
else
Point1Axis = Point1Axis + (Discrep)
end
if Point2Axis < 0 then
Point2Axis = Point2Axis + (-1*Discrep)
else
Point2Axis = Point2Axis + (Discrep)
end
local BetweenPoint = false
if Point1Axis > Point2Axis then
if PointBetweenAxis <= Point1Axis and PointBetweenAxis >= Point2Axis then
BetweenPoint = true
end
elseif Point1Axis < Point2Axis then
if PointBetweenAxis >= Point1Axis and PointBetweenAxis <= Point2Axis then
BetweenPoint = true
end
end
return BetweenPoint
end
local X = CheckPoint(Point1.X, Point2.X, PointBetween.X)
local Z = CheckPoint(Point1.Z, Point2.Z, PointBetween.Z)
if X and Z then
return true
else
return false
end
end
range = (B-A).unit:Dot(C-A) / (B-A).magnitude
isBetween = range >= 0 and range <= 1
You have to reduce your component A, B, and C to put them in terms of A so that you have direction vectors and not position vectors. Then you can get the dot from B-A unit vector to C-A, which will be scaled from 0 to the length of C-A. Then just divide by the length. 0 means C=A, 1 means C=B.
If you want to check if they are perfectly between each you could just compare the directional vectors, but there might be floating point errors. For example try do this:
local PointInBetweenDirection = (PointInBetween - StartPoint).Unit
local EndPointDirection = (EndPoint - StartPoint).Unit
print(PointInBetweenDirection, EndPointDirection)
You will see that the directional vectors are almost the same but the floating point errors will throw it off so you can’t compare them directly but you could do one decimal place.
Hi there! I think I found another of checking if the point lies between and I wanted to get your input on if its valid, it seems to work perfectly fine but are there any edge cases that could make it fail? (I can’t think of any)
local PointA --this is the start point
local PointB --this is the end point
local PointC --this is the point between
print(((PointC - PointA).Unit - (PointB - PointA).Unit).Magnitude <= 0.01)
That’s only checking if the directions are similar. It will give false positives if C before A or past B. You don’t really want to compare the units either because your tolerance will be in relative angular space instead of global 3d space - which means your tolerance will be too low when the points are close and too high when the points are far.
oh yes you are correct thank you very much, and btw would you mind explaining the StackOverflow solution, since I will be implementing moving projectiles in a project of mine, so I need to verify the raycast hit point on the server. (nvm figured it out)