Collision Not being respected

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.

What does the collision shapes look like?
In the second hit it looks like it hits a corner of a box and then gets resolved down through the paddle.

You shouldn’t move objects using go.set_position() it’s not a “physical” way, so it breaks physical interactions.

1 Like

Here’s a pinball example created using dynamic physics. The flippers are connected with hinge joints and forces are applied to move them:

4 Likes

Thanks but I can’t use dynamic physics in making it rogue like so I need to control interactions for various changes to different balls and upgrades.

What would be the best way to do it then? I can’t use dynamic physics as I need complete control over the ball.

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.

What control are you looking for here, that isn’t possible with dynamic physics?

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.

Will this update in 1.7.1 allow me to control a dynamic physics object?

Added Box2d Lua module by JCash · Pull Request #8712 · defold/defold (github.com)

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.

:point_up: 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.

1 Like

Ok so I’m going all in on dynamic collisions. I started with your sample code even just to keep it simple.

The ball occasionally gets stuck in the flipper though. How can I prevent this?

image

Edit. It just happened again right after I posted this…this time left flipper:
image

I added a bunch of spheres in the paddle. Is this the best way to prevent tunneling?
image

Interesting. What kind of changes did you do?

Or make it a solid shape (Collision shapes)

Have you set the “bullet” property to the ball ?

Yes I did.

Next, I’d test with a convex collision shape.

1 Like