Ball Physics collision bug

Hi, I’ve made a ball physics module for a Blue Lock game and it mostly works apart from the fact that there is a random chance for the ball to phase through the baseplate. The amount that it is common for this to happen is random and I would like to know what a solution would be to eliminate the chance at all.

Here is my code:

local BallPhysics = {}
BallPhysics.__index = BallPhysics

local RunService = game:GetService("RunService")

function BallPhysics.new(ball)
	local self = setmetatable({}, BallPhysics)
	self.Ball = ball
	self.Velocity = Vector3.zero
	self.AngularVelocity = Vector3.zero
	self.Friction = 0.981
	self.RotationalFriction = 0.956
	self.Gravity = Vector3.new(0, -9.81, 0)
	self.IsMoving = false

	RunService.Heartbeat:Connect(function(DeltaTime)
		self:Update(DeltaTime)
	end)

	return self
end

function BallPhysics:ApplyForce(Force)
	self.Velocity += Force
	self.IsMoving = true
end

function BallPhysics:ApplySpin(SpinForce)
	self.AngularVelocity += SpinForce
	self.IsMoving = true
end

function BallPhysics:Update(DeltaTime)
	if self.IsMoving then
		
		self.Velocity += self.Gravity * DeltaTime

		local maxVelocity = 50
		if self.Velocity.Magnitude > maxVelocity then
			self.Velocity = self.Velocity.Unit * maxVelocity
		end
		
		self.Velocity *= self.Friction
		self.AngularVelocity *= self.RotationalFriction

		self.Ball.Position += self.Velocity * DeltaTime
		if self.AngularVelocity.Magnitude > 0 then
			local Rotation = CFrame.fromAxisAngle(self.AngularVelocity.Unit, self.AngularVelocity.Magnitude * DeltaTime)
			self.Ball.CFrame *= Rotation
		end

		if self.Velocity.Magnitude < 0.1 and self.AngularVelocity.Magnitude < 0.1 then
			self.Velocity = Vector3.zero
			self.AngularVelocity = Vector3.zero
			self.IsMoving = false
		end
	end
end

function BallPhysics:CollisionHandler(Collided)
	local Direction = (self.Ball.Position - Collided.Position).Unit
	local Rebound = Direction * self.Velocity.Magnitude * 0.3
	self:ApplyForce(Rebound)

	local SpinForce = Vector3.new(math.random(-5, 5), math.random(-5, 5), math.random(-5, 5))
	self:ApplySpin(SpinForce)
	print("Velocity: ",self.Velocity)
	print("Position: ",self.Ball.Position)
end

return BallPhysics

turns out the problem was using the custom gravity and multiplying it so i removed it now it works fine

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.