Cutting down Magnitude

So I was wondering if anyone knew the math behind doing something like this when using magnitude?

image

Basically I was wondering for the future a method in wich I can cut down the area of magnitude while still using magnitude to then check that smaller area.

2 Likes

So you would only want to check for magnitudes in that smaller red sector instead of the entire sphere?

yes, I’m trying to get better at the math behind this type of stuff but I’m not sure lol

1 Like

If you want to make a sort of cone-like sector from the sphere, you can take the math.acos of the dot product of the cone’s preferred direction and the actual direction of the magnitude check and see if the resulting angle difference is within a certain range.

local preferredDirection = -- e.g. HRP.CFrame.LookVector

local deltaPosition = -- e.g. target.Position - HRP.Position

loca actualDirection = deltaPosition.Unit
local distance = deltaPosition.Magnitude

local angleDifference = math.acos(actualDirection:Dot(preferredDirection))

-- if the target is 10 studs away
-- and the angle difference between directions is less than 30 degrees,
if distance < 10 and math.deg(angleDifference) < 30 then
	-- do something cool
end

It might help to test math.deg(angleDifference) and see if it’s within expectations. I also haven’t tested this.

There are other ways to interpret the sector like taking a semi-sphere, but this seems like the easiest to calculate in my opinion.

3 Likes

Just some advice, magnitude checks are dirty cheap because they are an O(1) math operation. So trying to optimize it is pretty much useless and not worth your time

It’s not to optimize it, I’ts a method I’m trying to learn as a different way to check a certain area relative to a player instead of GetTouchingParts with a cone or any other Hitbox method.

Oh ok mb then, maybe try using the dot product to get a “cone” like area between the player and the target. Then check if its inside that cone area

This is a pretty good video illustrating what I mean

Thankyou for taking the time to explain all that, so I’m still trying to learn math.acos/math.sin/math.asin and Dot since I’m fairly new to those math functions.

Can you explain what getting the actualDirection and using Dot on it is doing exactly aswell as the point of using .Unit on it would be? Because actualDirection is the distance between me and the the end of the circle but why would you want to use the normalized vector of that or 1? Sorry I’m new to this so I’m pretty confused with these commons math functions.

1 Like

Thankyou that video is a good visualization of this, It’s still really hard to understand how to put this into practice though but I will be needing this method for multiple things in the future so I really want to understand these math functions and the method of doing this step by step even if it takes a good while

Whenever you write the dot product of vectorA and vectorB,

vectorA:Dot(vectorB)

each component of both vectors are pulled into a function like this and returned.

vectorA.X * vectorB.X
	+ vectorA.Y * vectorB.Y
	+ vectorA.Z * vectorB.Z

That is actually the same as calculating this.

vectorA.Magnitude 
	* vectorB.Magnitude 
	* math.cos([[angle between A and B]])

(I don’t exactly know why, they’re just equal)

As you can see, if we normalize both vectorA and vectorB, their magnitudes would be 1 and we would just be left with the cosine between the vectors. Calling math.acos on this will remove the cosine function and leave us with the angle.

Basically, math.asin is the inverse function of math.sin, math.acos is the inverse of math.cos, and math.atan is the inverse of math.tan. Doing math.acos(math.cos(x)) will simplify to just x.

function name inverse
math.asin arcsine math.sin
math.acos arcosine math.cos
math.atan arctangent math.tan

And if you don’t know what math.sin, math.cos and math.tan are, they are trigonometric functions that help translate between angles and positions in space.

I don’t have enough expertise in how trig functions work to explain beyond that, but there are a ton of resources on the internet for it, same for dot products too.

2 Likes

Thankyou been reading this over and over and testing in studio trying to understand, this is what happens when you skip algebra 1 2 and geometry and trig lol but I’m really trying to learn it.

2 Likes

Ok so I’ve learned how to find the sin cos and tan of a right triangle by researching some basic trig videos and am now watching some videos based around the thing you showed me regarding the magnitude hitbox, In a video the guy said to compare two vectors with :Dot you need to normalize 2 vectors by doing (vectorA - vectorB).Unit but gave no reason? I don’t really get why you have to normalize the vectors with .Unit in the first place? Also what is :Dot returning I know it’s the difference but if the difference is -1 compared to 0.50 how do you know if your within the correct region, and why is it needed to get rid of the cosine with math.acos and not just leave it instead?

https://gyazo.com/f89cddfa0954ffca9dcca7f5c2844bc6

I’m still having trouble understanding even though I believe I can acomplish my ideas behind this now by altering it bit by bit, It’s just the Dot product and Unit still confuses me?

Ok I’ve figured out my other question but I still don’t get why you need to normalize the vector what in detail is normalizing it with .Unit doing? Also what is :Dot giving you exactly I know you gave me equations but in words what is it giving you? I know it’s the difference but ehhh I’m confused. I got working what I wanted to and know exactly how to do it now but I still don’t understand it that great.

The Dot Product between two vectors will return you the magnitude of vector A * the magnitude of vector B * the angle between both of those vectors. If you normalize both vectors, this will ensure that the magnitude of each vector is equal to one. Thus, the Dot Product between two normalized vectors will return you the angle between both of those vectors.

3 Likes

This is a good explanation, although it should be clear that its the cos(angle) [cosine of the angle] not the angle.

1 Like

How does normalizing the vectors ensure you get the angle back but not normalizing them doesn’t? Also what is “Magnitude” really mean I know it’s the length of a vector but in what sense? Does Part.Position.Magnitude return where the part is in the world or what is it returning different than just position I don’t get it?

.Magnitude is different than position in that it returns one value, which is the hypotenuse of a 3D triangle.

The reason for normalizing the vectors is you only care for direction, not distance. So by normalizing them you make the distance 1, which is a very nice number to be using with doing multiplication because it doesn’t change the result of the final calculation.

1 Like

When you do HRP.CFrame.LookVector what does it return I know it’s a Direction but what is the difference between a Direction and a Position? I was wondering because I’ve heard that you can also use velocity as a direction so how does that all work?