How would I combine these two CFrames Properly? (Multiplying doesn’t work)

I’m making a Sonic engine, and I have 2 CFrames that work excellently separately, but start violently fighting and throwing things at each other the second I try and combine them (figurative).

For simplicity, I’m just gonna name these 2 CFrames “Horizontal” and “Vertical”, and the end product will just be called “end product”.

Here’s videos of Horizontal and Vertical on their own.

Horizontal:

Vertical:

Horizontal on its own can only move along the X, and Z axes; like the default Roblox controller. However, Vertical on its own can walk on walls; you can only move forwards though, you can’t turn at all (when grounded).

Combining these two would give the player the ability to walk on walls, AND turn; but every time I try to combine them. This is the outcome:

I’ve tried multiplying Horizontal * Vertical, Vertical * Horizontal, using ToObjectSpace, ToWorldSpace, but nothing works, here’s the logic.

local hitNormal = ray.Normal
			local currentRightVector = self.hrp.CFrame.RightVector
			local upVector = hitNormal
			local newFacialVector = currentRightVector:Cross(upVector)
			
			local Rot = CFrame.fromMatrix(self.hrp.Position, currentRightVector, upVector, newFacialVector) + self.hrp.Position
			local Rot2 = self.alignOrientation.CFrame:ToObjectSpace(CFrame.lookAt(self.hrp.Position, self.hrp.Position + Vector3.new(self.move_Direction.Unit.X, 0, self.move_Direction.Unit.Z)))
            --rot is Vertical, rot2 is vertical
			if self.humanoid.MoveDirection ~= Vector3.new(0, 0, 0) then	
				self.alignOrientation.CFrame = Rot2 * Rot
			end

IVE TRIED EVERYTHING I CANT DO IT I NEED HELP PLEASE HELP ME HELP ME HELP ME HELP ME

Thanks in advance!

1 Like

you are adding them correctly, the issue is in Rot2. You are composing Rot2 using alignOrientation, align orientation is itself composed via Rot2. I believe this is a compounding error where every frame Rot2 is being constructed using a CFrame that already has the offset of the last frame baked in.

You need to either not use alignOrientation while constructing Rot2 or restructure entirely. Personally I think it would be easier to just create a single CFrame with .fromMatrix to pass into alignOrientation. You already have an up vector from the raycast, you can get a forward vector from the desired move direction, you can cross to get right and construct a CFrame with HRP position and then just pass it into alignOrientation.

Hey, I know that it may be troublesome to share your work, but could you share anything at all about how you made your own physics for this engine? I’ve been trying to make some custom physics for a Sonic asym game to be similar to Sonic’s physics, but haven’t been able to apply anything from Sonic Retro since I’m inexperienced with creating my own physics

So far I’ve just been breaking stuff down and watching those Behind The Scenes videos on how classic Sonic physics work, and applied it to 3D, for example, Sonic 1 uses “raycasts” to detect and position Sonic on the ground - this is how he can walk up walls - so I did that for 3D.

Check out Youtube for breakdown vids, like this, or this and this, and most importantly, this 2-video series, it helps break down the logic behind the physics alot, they don’t give you the exact 3D physics or anything like that, but it can really help to get started on making a sonic engine