Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with constructing CFrames using lookVectors and normals?

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.

12 comments

1

You should use particles for the sparks -- it would be way more efficient.

0

Particles would still clip through though.

0

And it's considerably harder to direct particles where you want them to do.

0

go*

0

I'm just talking about efficiency. They would still clip, of course, but I don't think directing them is so much harder.

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.

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

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.

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.

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.

0

I could accomplish it with particles, but I'd still need the above cframe in order to direct my particles.

0

Exactly. Hopefully bluetaslem or some similarly-smart-math-person can answer this well.

1 answer

✓ Accepted answer

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'

3 comments

0

Let me test this now.

0

"cross is not a valid member of Vector3" for the reflection:cross line.

0

Nevermind, the method needs a capital "C" but seems to be working fine. Thanks a lot.

This discussion is preserved as it was. New answers, comments, and votes are closed.