I’ve been messing around with the system for a little bit now, and at this point my head is just running in circles, so I could really use some help.
The system is for a bouncing object simulation. When the object travels perfectly forward, and then strikes perfectly flat ground, I can find the angle of the bounce by simply doing:
--SurfaceNormal is gathered from a raycast, in this example it is exactly (0,1,0)
--pos is the Vector3 where the hit occurred
local CrossSection = Velocity:Cross(surfacenormal)
local Result = -(CrossSection:Cross(surfacenormal))
-- if there was no bounce, result is the direction it'll roll
local OG = CFrame.new(pos,pos+Velocity)
local NewFlat = CFrame.new(pos,pos+Result)
local X,Y,Z = (OG:ToObjectSpace(NewFlat)):ToEulerAnglesXYZ()
In that flat ground example, the angle of attack is always math.deg(X).
However, things get tricky when you start hitting things that aren’t perfectly level and flat, since Y and Z will starting being things other than 0.
How can I transform my CFrames/Vectors to get 1 number that represents the angle of the hit? Preferably, the X angle specifically.
This might not be what you’re looking for, but I was able to use this post by @sleitnick to learn how to retrieve the reflection angle at which any “thing” striking a plane based on a Raycast’s returned normal.
The one-liner I can give you to retrieve reflection in terms of a unit vector (as a direction) is this:
local reflectionDirection = (Direction - (2 * Direction:Dot(Result.Normal) * Result.Normal))
If you’re raycasting, you should be able to get the current direction in which the object is traveling (if it’s falling, the direction would be 0, -1, 0) and then calculate the force at which your ball bounces back towards that reflection’s direction, and more specifically, the X axis within that direction unit vector.
Alternatively, the .rbxl file in this post and the post by @EgoMoose itself are pretty great teachers of a lot of really cool physics-based movement techniques based on real mathematics, so those two things could also be helpers to what you’re looking for.
Again, I’m not the most well-versed in this kind of thing, it certainly isn’t my strongsuit, so I hope either what I’ve said or the resources I’ve linked could help you figure out what you’re looking for.
You should be able to get the angle of reflection as a direction and then use what axis you need, since you’re working in two dimensions, convert radians to degrees, and set your ifs after that.
Converting the direction vectors to angles is the problem… I’m looking for 1 singular number to represent the angle of reflection, the angles are almost always 3 numbers representing XYZ, since the reflection and initial projectile are on the same plane, and only one angle changes, that’s the one I need.
How about using inner product formula? dot product of normal and ray and divide multiplication of two vector’s size that cos value of angle will be induced.