When to use Vector3:Cross()?

Hello developers,

I want to learn about Vector3:Cross(), but it’s not easy for me to understand.

Is there someone who can explain me about it very well?

And It would be more appreciated if you explain me the detail situations when I should use it.

Any helps would be appreciated!

3 Likes

At its simplest, the cross product of two vectors is a vector perpendicular to those two vectors. The most common application of this is to select two vectors on a plane and use the cross product to get that plane’s normal vector (the vector that points out of the plane, perpendicular to it).

2 Likes

Basically goofy numbers, hard to explain just see:

local a = Vector3.new(1, 0, 0) -- X-axis
local b = Vector3.new(0, 1, 0) -- Y-axis
local c = a:Cross(b)

print(c) -- Vector3.new(0, 0, 1), along Z-axis

I also suggest not to use Vector3:Cross() and instead use vector.cross

Its basically avaraged angle

A----------B
•••••••••••••••|
•••••••••••••••|
•••••••••••••••|C

vector.cross(A,C)-->B
2 Likes

Thank you for replying. Could you tell me the situations I should use it? Just simply.

What is the difference between them?

vector.cross is more optimized (has fastcall) and does not require building namecall, just a function you pass arguments into

1 Like

Any situation where you need a normal to 3 points in space, e.g. to build a rotation axis, to work out the angle of impact to a plane (cross gives you normal vector, dot with impact vector for angle), to figure out which side of a plane the player is on.

1 Like

Sorry, I can’t understand it accurately. Could you give me an simple example? It doesn’t have to be hard; just give me an example related to a game logic. (Just like an object?)

You have 3 balls in space and you would like to spin them round and round in a circle. Pick an arbitrary rotation centre on their plane, make 2 vectors from that point to two of the three balls. Cross those vectors. Apply your rotation to the output.

1 Like

Have you seen this post before?

I think this one is useful

2 Likes

You shouldn’t go out of your way just to find a use for this that you can use once. You’ll rarely need to use most of the vector or cframe methods.

1 Like

Bros on a sigma grindset, he’s trying to learn every single thing (You should only learn things you need to know)

2 Likes

You don’t know what you need to know until you learn them! And something you learn could be useful in a different project, not always in the current one. It could also improve your way of thinking so you make better decisions in the future.

5 Likes

yeah with nuance basically 30charrrrrrrrrrrrrrrrrr

1 Like

Yeah, but I don’t understand when to use them.

1 Like

I really can’t come up with any other use cases other than finding an unknown vector (direction) and finding what angle to rotate an object by.

But I did come across this snipped from a tutorial by EgoMoose

Find this specific snippet in his post


function Placement:CalcCanvas()
	local canvasSize = self.CanvasPart.Size

	local up = Vector3.new(0, 1, 0)
	local back = -Vector3.FromNormalId(self.Surface)

	-- if we are using the top or bottom then we treat right as up
	local dot = back:Dot(Vector3.new(0, 1, 0))
	local axis = (math.abs(dot) == 1) and Vector3.new(-dot, 0, 0) or up
	
	-- rotate around the axis by 90 degrees to get right vector
	local right = CFrame.fromAxisAngle(axis, math.pi/2) * back
	-- use the cross product to find the final vector
	local top = back:Cross(right).unit

	-- convert to world space
	local cf = self.CanvasPart.CFrame * CFrame.fromMatrix(-back*canvasSize/2, right, top, back)
	-- use object space vectors to find the width and height
	local size = Vector2.new((canvasSize * right).magnitude, (canvasSize * top).magnitude)

	return cf, size
end


Notice how Cross is being used to find the RightVector from just one initial vector so that furniture objects are able to snap to any surface.

So the takeaway from all of this is that you should use Cross for angles and finding unknown vectors simply put. I don’t think I can come up with more ways to elaborate further.

2 Likes