Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

Is it possible to measure the Magnitude of an object from a look vector? And how?

Asked by 9 years ago

If I have a part, and want to measure its "Studs per second" from its front face (and if it goes backwards the magnitude would be negative) could I do so with a lookvector even though that's a CFrame/vector term?

0
Can you explain a bit more? I don't understand what you're trying to do. Goulstem 8144 — 9y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

I am interpreting your question as this:

I want to know how fast a part is moving in the direction of its front face

Meaning this speed would be 0 if the object is traveling directly to its left, right, top, or bottom, but it would be 1 if it's traveling at 1 stud per second in the direction of its front surface, and -1 if traveling at 1stud/sec in the direction of its back face.


This is quite easy! This is essentially asking for a vector projection of part.Velocity onto part.CFrame.lookVector. When the vector you're projecting onto is a unit vector (which lookVector is) this computation is very simple:

The velocity in the direction you're going forward is part.Velocity:Dot( part.CFrame.lookVector ) * part.CFrame.lookVector. If you only care about the "size" of that (with sign), then you just need part.Velocity:Dot( part.CFrame.lookVector ).

The dot product essentially measures how "similar" two vectors are. For two unit vectors u and v, u:Dot(v) will the the cosine of the angle between them (meaning 0 when perpendicular, 1 when the same and -1 when opposite).

For two Vector3s u and v, u:Dot(v) is the same as u.x * v.x + u.y * v.y + u.z * v.z

0
Thank you. Orlando777 315 — 9y
Ad

Answer this question