Learning Vector Math

Hey guys!

So I’ve been scripting for a while but I’ve never actually advanced much into the math part of scripting. I’m not too great with math in general, however I’m trying to improve. I’m looking for some guidance on learning how to script with vector math. I’ve tried using the wiki to learn all the different APIs and how to do some basic math, but I want to understand what I’m exactly doing. I don’t think that the wiki has been very useful for me. Does anyone have any tips or links that could help me improve? I’m not sure where to get started!

Thank you :slight_smile:

Anne

2 Likes

@AxisAngle where is that giant imgur picture you have explaining cframes?

7 Likes

To understand how vectors work in general, you’re best bet is to use resources not necessarily for Roblox, so that you can understand the premise behind it - I recommend:

Next, you can apply this new knowledge to Roblox. Using the wiki page,

you can experiment with different things.

A summary of what you need to know about Vectors

A Vector3 is a essentially a point in Roblox space, consisting of 3 coordinates, x, y and z.
Edit:

image
These points can be assigned to various objects within Roblox, mostly objects known as “BaseParts” - giving them a direct position in the “world”.
The size of a part is defined using Vector3 as well.
In Roblox, Vectors are defined using studs. If you made a part with a Vector3 size of (1,1,1), it would be 1 stud tall, 1 stud wide and 1 stud deep. (a cube).
Likewise, if this part was placed at a Vector3 of (5,0,0), it would be placed 5 studs to the right of the origin (the origin being 0,0,0)

Example code to demonstrate:

local part = Instance.new("Part") -- creating the part object
part.Parent = workspace -- parenting the part to the game's workspace, in the 'world'
part.Size = Vector3.new(5,5,5) -- a cube of 5 by 5 by 5 studs.
part.Position = Vector3.new(0,10,0) -- placed 10 studs above the origin in the air.

What is Vector3.new you ask?
When creating a new Vector3, you use Vector3.new() to create a blank Vector3 of x = 0, y = 0, z = 0.
Vector3.new() takes 3 optional arguments (x,y,z) - x, y and z can be basically any float

A problem with Vector3 however, is it doesn’t allow rotation.
To implement rotation, you will need to use CFrame:

Similarly, to create a CFrame, you use CFrame.new(x,y,z)

part.CFrame = CFrame.new(0,10,0) -- the first 3 arguments are a vector3
-- but how would I rotate it? --
part.CFrame = CFrame.new(0,10,0, Vector3.new(5,10,0)) -- to rotate it to look towards a point 5 studs to its right.
-- to rotate it by degrees --
part.CFrame = CFrame.new(0,10,0) * CFrame.Angles(0,90,0)
-- Alternatively, you can rotate *only* using Vector3, using the property "Orientation" --
part.Position = Vector3.new(0,10,0)
part.Orientation = Vector3.new(0,45,0) -- rotated 45 degrees clockwise.

Unfortunately, I can’t cover everything you need to know for Vector3 and CFrame, so various tutorials and especially the Wiki are your best bet. Otherwise, just practise and experiment, eventually it will become clear. Hope this helped!

All of this code is completely untested and just an example coded directly here so it isn’t guaranteed to work out of the box.

3 Likes

This playlist

2 Likes

(rip headphone users)
Yeah that seems like a decent video after a quick skim, though the use of C++ may result in more confusion than necessary depending on the prior experience of the op, but the rest of the video remains useful.

If you’ve used OOP in Lua it shoudn’t be too hard to understand even in CPP. And him talking about what he is doing kind of helps guide. But you can always ignore that

The static you can ignore at least with my headset. By video 8 the static get cuts down and eventually he uses a higher quality mic where there is none

I believe everything linear algebra is useful for deepening knowledge of vector math and matrices. Sadly I didn’t really study for my linear algebra class and barely got a passing grade, but it’s definitely worth a shot checking it out.

Just a disclaimer that I haven’t gone through this yet but plan on doing so… whenever I’m free… (which is probably never)

1 Like

Good lord, it’s AQA, god forbid.

Nice explaination though.

1 Like

Probably good to clarify that vectors are not points. They are directions with a length. This is useful knowledge when dealing with say a unit vector which is often used just for direction and would make no sense if you just considered them points in 3D space.

5 Likes