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

Help with constructing CFrames using lookVectors and normals?

Asked by 7 years ago

So, my plan is to create a projectile which will send sparks flying off at an angle when it hits something. So far I got this. It works fine, but there is a lot of clipping, unfortunately, which I need to get rid of.

My first idea was to calculate the angle between the reflection lookVector and the normal, and determine a range of spray from that, although I then realised that my angle would only work on a 2D level, and I'd need to take into account the other components.

So now we come to my final idea. I'd like to find the CFrame at the point of collision, facing down the surface in the direction of reflection, in-line with the surface normal. Here are some images below to help you understand.

I'd like the CFrame of the red part...

Green = Surface normal Yellow = Path of projectile + Path of reflected projectile

https://gyazo.com/e87d7dea4d627dbb3bdee515a4af18a6

https://gyazo.com/1d013c2a8f2269e5e816ea25ebad5ff0

I can give you the normal [lookVector], the direction of reflection [lookVector] and the position of collision [Vector3].

I am happy to answer any questions people have about this.

1
You should use particles for the sparks -- it would be way more efficient. Perci1 4988 — 7y
0
Particles would still clip through though. darkelementallord 686 — 7y
0
And it's considerably harder to direct particles where you want them to do. darkelementallord 686 — 7y
0
go* darkelementallord 686 — 7y
View all comments (8 more)
0
I'm just talking about efficiency. They would still clip, of course, but I don't think directing them is so much harder. Perci1 4988 — 7y
0
They would need to form a pattern within 90 degrees. They would need to have a higher particle density towards the direction of reflection and a higher velocity if the angle was closer to the direction of reflection. It would be more efficient, but I can't do all of this with particles. darkelementallord 686 — 7y
0
First off, no one is going to be deeply analyzing your sparks. But you can indeed achieve all this, have you looked at the new particle system? http://wiki.roblox.com/index.php?title=API:Class/ParticleEmitter Perci1 4988 — 7y
0
They won't be deeply analysing my sparks, no. However I don't want people to start firing at a building, and people on the inside getting showered by sparks. It's more a "I'm almost there, why not finish it off?" sort of thing. But it's an interesting challenge none-the-less. darkelementallord 686 — 7y
0
And yes, I have seen the new particle system. I'm using particles in the projectiles, although it's not easy to see due to gif quality. darkelementallord 686 — 7y
0
Right, the clipping the main problem. I'm just trying to give an efficiency suggestion, and I believe you can accomplish it all with particles. Perci1 4988 — 7y
0
I could accomplish it with particles, but I'd still need the above cframe in order to direct my particles. darkelementallord 686 — 7y
0
Exactly. Hopefully bluetaslem or some similarly-smart-math-person can answer this well. Perci1 4988 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Not sure if I'll be able to explain this in proper depth, but I think I got an idea.

You could construct the CFrame completely from scratch by using some vector math. To be specific, you could get all the vectors you need via 2 cross operations.

First you have to understand that CFrame is made up from 4 vectors. The position, top unit vector, back unit vector and right unit vector. So in theory, if I figure out these 4 vectors, I can create a CFrame from scratch.

The cross product of two vectors is another vector, which will always be perpendicular to the previously mentioned vectors. Using this property, I can figure out rest of the unit vectors necessary for CFrame.

local normal, reflection, pos

local right = reflection:Cross(normal).unit
local back = right:Cross(normal).unit

local cframe = CFrame.new(pos.x, pos.y, pos.z,
                            right.x, normal.x, back.x,
                            right.y, normal.y, back.y,
                            right.z, normal.z, back.z)

Edit: Fixed 'Cross'

0
Let me test this now. darkelementallord 686 — 7y
0
"cross is not a valid member of Vector3" for the reflection:cross line. darkelementallord 686 — 7y
0
Nevermind, the method needs a capital "C" but seems to be working fine. Thanks a lot. darkelementallord 686 — 7y
Ad

Answer this question