How to make realistic bag physics with body thrust?

Currently making a punching bag that moves realistically in response to punches. I was told to use bodythrust to achieve the result but in my attempt below, the bag spasms uncontrollaby instead of moving realistically in accordance with the punches.
The epileptic attempt:

The script excerpt containing all the relevant parts:

local LeftHand = Character:WaitForChild("LeftHand")
local RightHand = Character:WaitForChild("RightHand")
local Limbs = {LeftHand,RightHand}
local PunchingBag = workspace.PunchingBag.Union
local PreviousPosition = nil
for _,Limb in pairs (Limbs) do
	PreviousPosition = Limb.Position
end

local Attack = Animator:LoadAnimation(Animations[Count])
Attack:Play()
Attack.KeyframeReached:Connect(function(End)
	Attack:AdjustSpeed(0)
end)

PunchingBag.Touched:Connect(function(Hit)
		for i,Hands in ipairs(Limbs) do
			if Hit == Hands and Hit:IsDescendantOf(Character) then
				local BodyThrust = PunchingBag:FindFirstChild("BodyThrust")
				while RunService.Heartbeat:wait() do
					local CurrentPosition = Hands.Position
					local Velocity = CurrentPosition - PreviousPosition
					BodyThrust.Location = CurrentPosition
					BodyThrust.Force = Velocity.Unit * 35
					wait(3)
					BodyThrust.Force = Velocity.Unit * 0

So basically, I loop through a table containing the left and right hands of the player’s character before the punch is thrown then tracked their position in the “PreviousPosition” variable. I then connect the punches thrown to a touch function where I once again loop through the limbs table to detect hand contact. Then I tracked the hand position at the bag in a heartbeat loop in the “Current Position” variable,as I was told this had to run every frame, then subtracted the prev position from it to get the velocity. Assigned the location property to the current position and the force to the velocity variable.unit multiplied by force.

But as you can see, the result sees the bag having an apoplectic episode. How do I make it so that the bag swings naturally from the position of my punches with accurately calcuated velocity with the hands?
Plz help with the physics ting because there is nothing about this on the net.

It seems like using BasePart:ApplyImpulse() might be a better idea than BodyThrust here. Body movers can have really janky results and are deprecated. If you want realistic physics, you can calculate the force of the hit (F= m*a) and apply it using the impulse function.

However, roblox’s internal physics simulator basically already does this for all collisions, so I think making a whole script to apply forces from collisions which the engine already does is kind of redundant (if you calculate based on the hand movement anyways). If you can’t get the bag moving enough using roblox’s physics engine, you can always change its weight/density by enabling “CustomPhysicalProperties” on the part and turning its density down.

2 Likes

What additional calculations would I have to do or is that it( Since you said the f=ma is redundant)? Just do :ApplyImpulse on the bag and turn down the density? That sounds a little too simple doesn’t it? Do you have any links to relevant topics or aticles that might help?

If you make the arms collidable (or make something to attach to the fists to be collidable), the roblox physics engine could simulate the collision entirely for you, without any impulses, calculations, or scripting necessary for you, as it’s built to handle exactly this kind of in-game simulation between parts in a (reasonably) realistic manner.

I don’t have links to any specific articles other than documentation, but since this would be handled by roblox’s physics engine, the implementation would be very simple anyways. You can customize how roblox’s engine would simulate the collision through the use of CustomPhysicalProperties for both the fist and the bag.

Changing the elasticity and density values can get you the desired results for any punch. Here is some documentation that tells you what each of them does: PhysicalProperties | Roblox Creator Documentation. To make a more powerful punch with the physics engine, it’s as simple as changing the animation to move faster! (as a more powerful punch would in reality). Making the hands/fists more dense would also impart more force due to F=ma if you want to make them magically hit harder or softer without caring about the animation. All these values can be changed during runtime with the PhysicalProperties.new() constructor, letting you change the power for different moves, hits, etc. while the game is running, effectively simulating your “bodythrust”'s power changing for you.

I’ve tried relying on the arms alone for collision(With cancollide turned on of course) but it doesn’t work. The hands just go straight through the bag. I want to reproduce this effect:
https://streamable.com/dz6v70

It looks like this welds some parts to the hands (the boxing gloves) and makes them CanCollide true. Why not just make the hands CanCollide true? Fun fact: the default Roblox humanoid resets every part except the torso and head to CanCollide false every frame, nuking anything you might have set the value to. This is why you have to weld something to the hands instead.

I don’t know, I think i’m kinda successful? Because it’s not as controlled as in the clip.

1 Like