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
I would look at clamping the velocity to a go property. Might help with the velocity and bulleting that’s happening. Also would mimic the slant and friction of a pinball machine.
I just looked at @britzl 's sample and I actually used the same exact collision shapes he did :-). But I also tried convexhull shape as well. Basically the flipper is a circle on each end and two boxes for the long side. But then I threw in more circles as I’ve found that helps sometimes when the initial collision is “missed”. I think @agulev is correct in that go.set_position() is the issue. if the set_position happens to put the ball inside the flipper or even through the flipper as it is moving. Not sure how to resolve this though. I can’t use the dynamic physics though as I need complete control over the ball during gameplay.
It’s going to be a rogue like so I need to be able to change physics in the game. Let’s say I want to give a powerup, but it also has a chance of negative effects…so the ball reflects differently for a while. Like instead of an expected bounce off a bumper it may do something very odd and go right back to the paddle/flipper very fast. Also there may be mulitple balls one with low gravity, one with high gravity and one with random reflections/physics.
Also same applies to the flippers. I may have odd bounces on the flipper if you have a negative power up at the moment.f
Or a special power that allows you to line the ball up and make it hit that point.
I don’t see anything in your description that can’t be done also with dynamic physics. Remember that you will get collision and contact point events with a lot of useful information. And you can apply forces to the balls to achieve all kinds of effects.
Apply a force in the direction of the paddle
low gravity - apply a constant upwards force
high gravity - apply a constant downwards force
The purpose is to give you a close mapping to the native Box2d API. If you can do it in Box2D then you should be able to do it with the b2d Lua module.