I want to include backflips and frontflips but I'm having trouble understanding the CFrame Trigonometry

In my game downhill rush, if you perform a jump by going on a ramp and you press any key:
Spacebar, Shift, E or Q that will let you perform a roll or a backflip.

The problem is that pressing these only rotates the car about a fixed CFrame rather than the carts CFrame and it’s a bit tricky trying to figure out what the cart’s CFrame is.

This is the code

-- cv means ControlVariables  (information here is shared in a modular system)

local x, y, z = 0, 0, 0 -- these are going to change the carts CFrame
		if cv.injump then -- if the cart is in a jump or in midair and we input with our keyboard
		-- desiredpitch is meant to spin the cart back and forth
	        -- desiredroll is meant to roll the cart side to side
		x,y,z = cv.DesiredPitch, 0, cv.DesiredRoll  -- at the start of the jump, these are all 0, pressing Q or E will add these values. 
		end


		--	**This is the issue here**
                -- the desiredCF sets the car's primarypart facing somewhere relative to cv.face.
                -- cv.face is a part that is aligned with the road that aligns players to the road direction. Don't worry about it
                -- We multiply this alignment to the road with angles. 
                     -- PitchAngle is 0 by default,
                     -- YawAngle is either -7 or 7 degrees or 0 depending if you are pressing A or D
                     -- cv.Angle is 0 if the cart is on a flat road and at an angle if the cart is on a curved road (this makes it smoother)

              -- The angles x, y, z are for performing stunts. 
		cv.DesiredCF = cv.face.CFrame * CFrame.Angles( ((cv.flight and math.rad(6)) or 0) + cv.PitchAngle , cv.YawAngle, cv.Angle) *  CFrame.Angles(x, y, z)
		
		-- the problem is that x, y , z rotate the CFrame about cv.face rather than the carts. 
                -- so my idea is to get components for the angles like.
                -- if the cart is already rolled at 45 degrees then desiredpitch is a cosine of 45 and sine of 45

What i’m trying to get is that

DesiredRoll should be relative to the cart’s CFrame alignment
DesiredPitch should be relative to the alignment of the cart too.

and one Idea I had was to simply use the x,y,z and convert those into values that can multiply with the cv.face in order to appear as if the cart is doing backflips

This is going to allow players to perform cool backflips and whatnot.

I also plan on doing a system that keeps track of how many rolls or backflips that the cart has performed.

1 Like

I’m confused by all these variables.

What is your actual question?

it’s a bit tricky trying to figure out what the cart’s CFrame is.

Why? Can’t you just read it directly?

What’s preventing you from just doing cart.CFrame * CFrame.Angles(roll, 0, pitch)?

1 Like

cv.DesiredCF is what sets the carts CFrame.

I’m following a method where cv.DesiredCF is set in a .heartbeat event

and then cart.BodyGyro.CFrame = cv.DesiredCF is set in the .Stepped event

That’s just how i’m currently doing it.

AS for what’s stopping me? If I multiply the cart.CFrame then it won’t align with the road’s CFrame.
Also desiredRoll is an increasing value rather than a constant one.

If I hold E for 1 second my desiredRoll may be let’s say 10 degrees. The next 2 seconds it may be 20 degrees.
If I multiply the increasing value by the cart’s CFrame the cart will just spin incredibly quickly.