what method could i use to compare vectors because just doing
(vector1 < vector2)
returns a error
what method could i use to compare vectors because just doing
(vector1 < vector2)
returns a error
Are you trying to compare the magnitude of the vectors?
not really,
i trying to check if a position is within a area, and im checking if the position is within 1 corner and the other
Then youll need to check if the position X is greater then the corner X and same with the Z, youll probably have to take into account the size aswell
In this case you might want to use dot products:
Vector3 | Roblox Creator Documentation
You would do this with each corner.
i didnt open studio for a while so ill use this as a excuse why i didnt answer back.
how would i use dot? i dont fully understand what its telling me from the documentation
is this the only way of doing it?
i feel like thsi would take alot to do
I assume you want to compare the length of the two vectors.
In math, you can just do:
|vector1| > |vector2|
but with roblox’s Vector3s you need to do:
vector1.Magnitude > vector2.Magnitude
(Magnitude means the size/length of the vector.)
Edit:
You can use this to check if the point is within a distance of another point by doing:
(vector1 - vector2).Magnitude < distance
You need more information to do this because the result is undefined.
Think of making rectangles with two points. If all the rectangles go the same direction, only one rectangle can be made. But for each rotation, there is a new rectangle that can be made.
If you want all of the spaces to go along the XYZ axises, you can simply check that all the xyz values of the vector being compared are between the corners. If you want to have a space not aligned to xyz you need (not including more information than corners) complicated linear algreba or I can find a module someone made to do that.
i dont get what you are saying maybe because im a little dumb but i do wanna understand what you are saying
information to do what? checking the position? and is this where you are getting the undefined result from
rotation of the points or rotation of the shape
confused on this
but is it something like
If Vector.X > OtherVector.X then
what is a space that isnt aligned to xyz?
So you’re checking if a point is within two corner points, but there are multiple boxes that have corners at the same points (like the picture above). By undefined I mean there isn’t a single answer.
Rotation of the shape, since points can’t rotate. The two green dots above represent your corners, and the red and blue boxes are boxes. If you want to check if a point is within a box, you need to define more information about the box, since their are multiple boxes that can be made with two points.
You’re checking if a point is within a box formed by two corners right? If the box is rotated 45 degrees (like half sideways) then it wouldn’t be aligned to xyz.
I assume you just want to check a box that’s aligned to xyz based on your code. You can do that by comparing the individual components of the corner.
Here is an example:
local function within(point, corner1, corner2)
local maxX
local minX
do
if corner1.X > corner2.X then
maxY = corner1.X
minY = corner2.X
else
maxY = corner2.X
minY = corner1.X
end
end
local maxY
local minY
do
if corner1.Y > corner2.Y then
maxY = corner1.Y
minY = corner2.Y
else
maxY = corner2.Y
minY = corner1.Y
end
end
local maxZ
local minZ
do
if corner1.Z > corner2.Z then
maxY = corner1.Z
minY = corner2.Z
else
maxY = corner2.Z
minY = corner1.Z
end
end
local withinX = (point.X >= minX) and (point.X <= maxX)
local withinY = (point.Y >= minY) and (point.Y <= maxY)
local withinZ = (point.Z >= minZ) and (point.Z <= maxZ)
-- If the point is between the min and max values of X, Y, Z
return withinX and withinY and withinZ
end
(You can make the code a lot smaller with math.max, math.min, and combining the true/false stuff, but I thought it would make it a little confusing)
tl;dr:
Compare the individual X, Y, and Z values if the box is aligned to xyz.
If you want something advanced, you can define a rotated 3D space as a CFrame and a Vector3.
You can check if a point is within the space by converting the position CFrame:PointToObjectSpace(checkPosition) and then comparing that vector to the Vector3 (size vector).
Here’s a visual screenshot of how I did it:
Here’s the script:
local space = workspace["3D Space"]
local check = workspace.Check
do
local cf = space.CFrame
local sizeh = space.Size/2
local function p(v)
local part = Instance.new("Part")
part.Anchored = true
part.Size = Vector3.new(1,1,1)
part.CFrame = cf - cf.Position + v
part.Name = "p"
part.Parent = workspace
return part
end
p(cf:PointToWorldSpace(sizeh))
p(cf:PointToWorldSpace(Vector3.new(sizeh.X, sizeh.Y, -sizeh.Z)))
p(cf:PointToWorldSpace(Vector3.new(sizeh.X, -sizeh.Y, sizeh.Z)))
p(cf:PointToWorldSpace(Vector3.new(sizeh.X, -sizeh.Y, -sizeh.Z)))
p(cf:PointToWorldSpace(Vector3.new(-sizeh.X, sizeh.Y, sizeh.Z)))
p(cf:PointToWorldSpace(Vector3.new(-sizeh.X, sizeh.Y, -sizeh.Z)))
p(cf:PointToWorldSpace(Vector3.new(-sizeh.X, -sizeh.Y, sizeh.Z)))
p(cf:PointToWorldSpace(Vector3.new(-sizeh.X, -sizeh.Y, -sizeh.Z)))
space.Transparency = 0.8
end
local function inBounds(v, min, max)
return v.X > min.X and v.Y > min.Y and v.Z > min.Z and v.X < max.X and v.Y < max.Y and v.Z < max.Z
end
local isInBounds = false
local cf = space.CFrame
local sh = space.Size/2
while wait() do
if inBounds(cf:PointToObjectSpace(check.Position), -sh, sh) then
if isInBounds == false then
isInBounds = true
print("IN BOUNDS")
end
else
if isInBounds == true then
isInBounds = false
print("OUT OF BOUNDS")
end
end
end
You can compare two vectors in Lua by comparing their individual components (x, y, and z) separately.
Here is an example of how you can compare two vectors:
local vector1 = Vector3.new(1, 2, 3)
local vector2 = Vector3.new(1, 2, 3)
if vector1.x == vector2.x and vector1.y == vector2.y and vector1.z == vector2.z then
-- vectors are equal
else
-- vectors are not equal
end
You can also create a function that compares vectors and returns true or false.
function areVectorsEqual(vec1, vec2)
return vec1.x == vec2.x and vec1.y == vec2.y and vec1.z == vec2.z
end
if areVectorsEqual(vector1, vector2) then
-- vectors are equal
else
-- vectors are not equal
end
Additionally, you can compare the magnitude of the vectors.
if vector1.magnitude == vector2.magnitude then
-- vectors have the same magnitude
else
-- vectors have different magnitudes
end
You could also compare the dot product of two vectors and check if they are the same direction.
if (vector1:Dot(vector2) > 0) then
-- vectors have the same direction
else
-- vectors have different directions
end
Keep in mind that comparing floating-point values for equality can be tricky. You may want to use a small tolerance value, such as 0.01, when comparing the values.
I hope this helped.
Read through this and it now makes alot of sense, since im using parts to define “the zones” and only using position i wouldn’t be able to rotate them or the area would be vastly different to the actual area i want
(i could’ve worded this better but im to lazy)
also looked back in some older code i have and found this
local function RangeCheck(Position, Corner1, Corner2)
--TempDebugPart(Position, Corner1, Corner2)
local Check1
local Check2
local PosX, PosY, PosZ = Position.X, Position.Y, Position.Z
local Corner1X, Corner1Y, Corner1Z = Corner1.X, Corner1.Y, Corner1.Z
local Corner2X, Corner2Y, Corner2Z = Corner2.X, Corner2.Y, Corner2.Z
if PosX < Corner2X and PosY < Corner2Y and PosZ < Corner2Z then
Check1 = true
end
if PosX > Corner1X and PosY > Corner1Y and PosZ > Corner1Z then
Check2 = true
end
if Check1 and Check2 then
return true
end
end
Havent tried it yet but it may work
Damn this is confusing
lemme go over some parts i dont get all to well
Im guessing v is a vector, but whats with the math on the parts CFrame?
(as in what is it doing)
Its probably because ive never used PointToWorldSpace but why is there so many of them? what do they do
im guessing this is checking if its in the defined area, not sure how yet though.
looking at it how exactly is -sh, sh, which ion know what it truely is min and max?
Also have no clue what ToObjectSpace is either, nor whats the differnece beteween ToWorldSpace and ToObjectSpace
the hardest part has to of been
spent hours trying to figure out what this could mean
oh so if i just wanted to compare 2 vectors i would have to manually go through X, Y, and Z
so dot is just comparing 2 vectors and seeing if they have the same direction?
then also how does it give me numbers like 2353 (not a real number, but the one time i tried it with 2 random parts positions it gave me something big)
and as always i apologize for being late on the responses to everyone
This function:
local function RangeCheck(Position, Corner1, Corner2)
--TempDebugPart(Position, Corner1, Corner2)
local Check1
local Check2
local PosX, PosY, PosZ = Position.X, Position.Y, Position.Z
local Corner1X, Corner1Y, Corner1Z = Corner1.X, Corner1.Y, Corner1.Z
local Corner2X, Corner2Y, Corner2Z = Corner2.X, Corner2.Y, Corner2.Z
if PosX < Corner2X and PosY < Corner2Y and PosZ < Corner2Z then
Check1 = true
end
if PosX > Corner1X and PosY > Corner1Y and PosZ > Corner1Z then
Check2 = true
end
if Check1 and Check2 then
return true
end
end
Is the same as this function:
local function inBounds(v, min, max)
return v.X > min.X and v.Y > min.Y and v.Z > min.Z and v.X < max.X and v.Y < max.Y and v.Z < max.Z
end
inBounds(vector, corner1, corner2)
-- will be the same as
RangeCheck(vector, corner1, corner2)
If you look at the screenshot, cf is the CFrame of the very large transparent part. space
is the very large transparent part, and the 8 parts on the corner of space
are created by calling this function 8 times.
By subtracting the position it sets the position to 0,0,0. Then by adding the vector v, you set the position to v. However by doing it this way, you keep the same rotation as the space part. The 8 times I called this function are for each corner of the space.
this sets the transparency of the big part to where you can see through it.
It makes it so you can see through the part.
A value of 1 would make it invisible.
A value of 0 would make it normal looking, or solid.
Think of MainCFrame:ToObjectSpace(OtherCFrame) as going from a rotated OtherCFrame to an unrotated OtherCFrame.
Similarly, MainCFrame:PointToObjectSpace(OtherPosition) is like unrotating the position.
Here’s an example:
local space_cf = space.CFrame -- a rotated part's CFrame
local part = workspace.MoveThisPart -- a part we will move 2 studs along the X axis of space_cf
local unrotated_pos = space_cf:PointToObjectSpace(part.Position)
unrotated_pos = unrotated_pos + Vector3.new(2, 0, 0)
part.Position = space_cf:PointToWorldSpace(unrotated_pos)
Also the size vector of a part provides you the “corners” of the space it takes up.
The lower and upper corners are as follows:
local part = workspace.Part
local size = part.Size
local pos = part.Position
local lowerCorner = pos - size/2
local upperCornerr = pos + size/2
This is for an unrotated part.
That will work as long as the corners are the correct corners of the box, aka the “top” corner has the greater x, y, and z values and the “bottom” corner has the lesser x, y, and z values.
If the corners are the right ones it’s the same as the code example I gave, but it’s usually better to get the min and max component values so any corners can be chosen. Otherwise it’s really easy to add bugs to your code and really hard to always pick the “right” corners.
tl;dr:
That code sort of works but I would use code that gets min and max values the code from the example above.
Ok, looking through this and trying some things out for myself i just want some reassurance that what im getting is correct
:PointToObjectSpace
acts as if whatever CFrame? is 0, 0, 0 and finds the vector offset from that remade 0, 0, 0 from a position, not sure how this works with rotated stuff but i got some of it (hopefully) which counts for me.
and since its basically 0, 0, 0 (the :PointToObjectSpace
) all you need is the size positive and negitive to determine area?
I almost get it except how :PointToObjectSpace
works with rotations
Also Not sure what :PointToWorldSpace
exactly does but im sure thats not truely needed right?
A little off focus here but whats the difference between :PointToObjectSpace
and just :ToObjectSpace
? is it just that one uses CFrame and the other uses position?
yeah never took in that i may rotate parts, so the code hkeps providing is going to help alot
again, sorry for the delay in responding, i forgot to check this
I’m not sure if you got it.
A CFrame represents both a position and a rotation.
Parts also have a size vector (.Size)
If you change the CFrame of a part (.CFrame) then it will also change the Position property (.Position)
local localPos = mainPart.CFrame:PointToObjectSpace(somePosition)
So imagine if somePosition was the position of somePart which is welded to mainPart.
Now
If I rotate mainPart to where it’s orientation is 0,0,0 then somePart will rotate along with it.
And lets say I also move mainPart to position 0,0,0 then somePart will also move along with it.
Basically, the position of somePart will be the same as localPos after moving and rotating mainPart to 0,0,0 and 0,0,0.
You might ask, but what is somePart’s CFrame now that mainPart it is moved and rotated to 0?
The new CFrame is as follows:
local localCFrame = mainPart.CFrame:ToObjectSpace(somePart.CFrame)