|
| 22 Oct 2016 04:39 PM |
How would I make an Object point to another object?
Example:
Part1 --What I want to point towards.
PointRotation = ???
Part2.CFrame = CFrame.new(Part2.CFrame.p)* PointRotation
[ Content Sorcus'd ] |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 22 Oct 2016 04:50 PM |
You could use the CFrame constructor that takes 2 V3s:
Part2.CFrame = CFrame.new(Part2.Position, Part1.Position) |
|
|
| Report Abuse |
|
|
|
| 22 Oct 2016 06:03 PM |
| part1.CFrame = CFrame.new(part1.CFrame.p) * (part2.CFrame - part2.CFrame.p) |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 22 Oct 2016 06:06 PM |
| No, that'd make it have the same rotation not point to th e other obj |
|
|
| Report Abuse |
|
|
|
| 22 Oct 2016 06:12 PM |
| Oh. My bad :| Just for reference, how DO you manually calculate it? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 22 Oct 2016 06:32 PM |
There are plenty of ways to do it, the way I think that might be used internally is getting the direction of a towards b ((b-a).unit) and set that to be the -z direction of the CF then calculate the y (being up) and then x. You can just use the cross product to calculate x given z and y (y:Cross(up) will give you a vector perpendicular to both and then you can find 'up').
|
|
|
| Report Abuse |
|
|
|
| 22 Oct 2016 06:36 PM |
| That was a little confusing. Are there any online sources that provide pictures, or anything ROBLOX-specific that offers a step-by-step guide? I'd like to know how to actually apply this. It seems very useful, even if it is offered natively. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 22 Oct 2016 06:46 PM |
There is bound to be something on it, but here's the logic behind it: A CFrame is comprised of 12 components, the first 3 components are the position so we can ignore it
The z axis is the backward axis, so our goal is to make our CFrame's -Z axis (forward) point towards the other part.
The direction would be: zdir = -(otherPos - thisPos).unit That'll return a unit vector that points in the direction of otherPos from thisPos
So now the components look like this so far:
x, y, z r0, r1, zdir.X, r10, r11, zdir.Y, r20, r21, zdir.Z
Negative because, as I said above, the Z axis is the backwards axis so -Z is the forward axis.
Now our goal is to get the rest of the components. Because we have a notation of up being (0, 1, 0), we can dot that with dir to get a vector perpendicular to both:
xdir = zdir:Cross(Vector3.new(0,1,0)) (This might fail if zdir is (0,1,0) but let's assume it's not. if it is just cross with something else)
And to get the part's "up" all we have to do is cross it ydir = zdir:Cross(xir)
So then now we're left with: x, y, z xdir.X, ydir.X, zdir.X, xdir.Y, ydir.Y, zdir.Y, xdir.Z, ydir.Z, zdir.Z
It's really hard to explain without pictures, so this probably won't help |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 22 Oct 2016 06:48 PM |
| That'll return a unit vector that points in the direction of otherPos from thisPos and negate it** (which really is just dir to thisPos from otherPos but that's not really how you wanna think of it) |
|
|
| Report Abuse |
|
|
|
| 22 Oct 2016 06:54 PM |
Alrighty, thanks. I'll keep that in mind. Another thing, could you possibly explain how a matrice is composed? You seem pretty knowledgeable.
I know it's
x,x,x x,x,x x,x,x x,x,x
But I have no idea how to get the components. Where would I put my X, Y, and Z rotation? Where would I put my X, Y, and Z position? It feels much neater now that I know this exists to manually compose it at CFrame.new(x,x,x,x,x,x,x,x,x,x,x,x) instead of CFrame.new(x,x,x) * CFrame.Angles(x, x, x). |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 22 Oct 2016 07:04 PM |
Well you don't actually put angles, you put the direction that specific axis should face
The first 3 are the position, so we can ignore them The next 9 make up the rotation, each next 3 make up the next component (of a unit vector that equals the direction in world space):
px py pz r00 r01 r02 r10 r11 r12 r20 r21 r22
Vector3.new(r00, r10, 20) make up the direction the CFrame's "right axis" is (i.e. cf.rightVector == Vector3.new(r00, r10, r20)). Vector3.new(r01, r11, r21) make up the "up axis" (cf.upVector) Vector3.new(r02, r12, r22) make up the "back axis" (-cf.lookVector)
So if I wanted to position my part at some position (x, y, z) but have it rotated 90 degrees along the y axis, well I know that after rotating it 90 degrees the object's "right" would be the world's "front" and the object's back would be the world's "right" so you'd do:
CFrame.new(x, y, z, 0, 0, 1 0, 1, 0 -1, 0, 0)
And that would be equiv to CFrame.new(x,y,z)*CFrame.Angles(0,math.pi/2,0) except MUCH more efficient |
|
|
| Report Abuse |
|
|