Ok, I’ve tried everything I can think of for this. I need to use kinematic collisions so I can control what happens after the ball is hit. However, as you can see it isn’t reliable as the ball goes through the flipper. I’ve tried convex hull and primitive shapes. Same issues occur for both.
I currently moved to the new physics listener model and the same thing happens.
Does anyone know how to reliably get the collisions to behave consistently?
I’m guessing the problem has to be in the ball.script. And most likely with the go.set_position moving the ball through the collision. But I’m not sure what to change to fix it.
-- ball.script
go.property("gravity", vmath.vector3(0, -981, 0)) -- Ensure gravity is defined as a vector3 property
go.property("mass", 1) -- Mass of the ball, ensure this is set based on your game's scale
function init(self)
self.velocity = vmath.vector3(0, 0, 0) -- Initial velocity
self.position = go.get_position() -- Initial position
end
function update(self, dt)
-- Apply gravity continuously
self.velocity = self.velocity + self.gravity * dt / self.mass
self.position = self.position + self.velocity * dt
go.set_position(self.position)
end
function on_message(self, message_id, message)
if message_id == hash("change_direction") then
local incoming_velocity = self.velocity
local normal = message.normal
local elasticity = 1 -- Coefficient of restitution, set to 1 for a perfectly elastic collision
-- Enhanced reflection calculation considering the ball's mass
local reflected_velocity = incoming_velocity - (1 + elasticity) * vmath.dot(incoming_velocity, normal) * normal / self.mass
-- Set the new velocity considering the impact and the ball's mass
self.velocity = reflected_velocity
end
end