Vector math error?

Ideally, this positions a corner of the region behind and to the left of the player, then another in front, above and to the right, but it doesn’t work.

function Contest:createRegionFromPrimary()
	local char = self.primaryObj
	local hrp = char.HumanoidRootPart
	local lookVector = hrp.CFrame.LookVector
	local vec1 = lookVector:Cross(Vector3.new(0,1,0))
	local topRight = 4*vec1+7*lookVector+hrp.Position + Vector3.new(0,10,0)
	local  bottomLeft = -2*vec1-2*lookVector+hrp.Position
	local extraContest = Region3.new(bottomLeft,topRight)
	return extraContest
end

Here you can see the result of several attempted region3s, where they were created with the player facing towards the basket: https://gyazo.com/e98377e3e29debcda4122f697cf43d42
https://gyazo.com/b3e28ad6fb462c1768ee708b98690cf7

In the back left corner it was successful somehow, https://gyazo.com/44cca4c7d86f6c2ce82f16acaec56694.
Please help.

If you meant you wanted to rotate them, you cannot rotate Region3s.

No, I’m creating a new region, not rotating the current one.

I don’t think you need to go off the lookVector, you should be able to just offset the position from the CFrame itself.

CFrame * CFrame

I would just get where I want the center of the region to be in my generation, then offset the two required corners by equal amounts in opposite directions. Keep in mind you can’t rotate regions, but you can always use @EgoMoose’s rotated region3 module.

2 Likes

That’s a good point. So you’re saying I should predefine the region’s size and then set the CFrame at some offset relative to the player?

That is some interesting behavior. I think you got the directions of one of your vectors wrong. vec1 should be a left-facing vector relative to the rootpart, assuming of course that your character is standing up straight, but in that case the Region3 should still work. Have you verified that the base vectors are what you’re expecting them to be?

However, there are easier and less error-prone ways to achieve what you’re trying to do. I suggest you read the wiki page for CFrames as well as the tutorial on understanding CFrames for more information.

You can actually get the upwards, right-facing and backwards unit vectors from the CFrame itself. A CFrame is essentially nothing more than a rotation matrix with a position, and a rotation matrix in 3D space is essentially nothing more than three vector3s giving those directions. You can get the numbers making up all four vectors (directions and the position) using CFrame:components():

image

To put this into code:

local x, y, z,
	m00, m01, m02,
	m10, m11, m12,
	m20, m21, m22 = yourCFrame:components()

local position = Vector3.new(x, y, z)
local right    = Vector3.new(m00, m10, m20)
local up       = Vector3.new(m01, m11, m21)
local back     = Vector3.new(m02, m12, m22)

From there you can get any of the corners you want. For example:

local topRight = position + 4*right - 7*back + 10*up
local bottomLeft = position - 2*right + 2*back

However, it can get even simpler than that. CFrames actually have pre-made methods to do these spatial transformations for you. In your case, you’d want to look into CFrame:pointToWorldSpace(), which takes a Vector3 position relative to that CFrame and converts it into a Vector3 position in the world. For example:

local topRight = yourCFrame:pointToWorldSpace(Vector3.new(4, 10, -7))
local bottomLeft = yourCFrame:pointToWorldSpace(Vector3.new(-2, 0, 2))

Note that the X direction is to the right, the Y direction is upwards and the Z direction is backwards (not forwards).

Hopefully one of these approaches will help you solve your problem. :slight_smile:

EDIT:

This might actually be a better approach to the problem, because using coordinates relative to the player causes the Region3 to have a different size depending on the orientation of the player.

2 Likes

Anna, that tutorial is way more helpful than I thought it would be considering that I already took linear algebra. Thank you to both of you.

1 Like