How to code a pinball flipper?

Hi,

I’m very new to Defold and aren’t much of a coder anyway so forgive the potential simplicity of this question. I have set-up my game object for a flipper (as in the flipper on a pinball machine) and have been able to get it to rotate, but I have not been able to get it to rotate from its side rather than the middle where the whole thing spins round. Once I can figure that out my plan is then to get it moving up then down to say a 45 degree angle.

I tried using AI to help and it gave me the below but it isn’t doing anything. If anyone has any suggestions or can point me in the right direction I’d much appreciate it. I haven’t found any material that shows how to rotate from one side. I’ve been stuck on this for a few days now.

– Define the flipper rotation speed
local rotation_speed = 180 – Adjust the rotation speed as needed

function init(self)
– Initialize the flipper rotation state
self.is_flipping = false
self.current_rotation = 0
end

function update(self, dt)
– Check for flipper rotation
if self.is_flipping then
self.current_rotation = self.current_rotation + rotation_speed * dt
go.set_rotation(vmath.quat_rotation_z(math.rad(self.current_rotation)))
end
end

function on_input(self, action_id, action)
if action_id == hash(“touch”) and action.pressed then
– Toggle flipper rotation when the screen is touched
self.is_flipping = not self.is_flipping
end
end

Ah ignore me - I’ve just realised the game object will always rotate in the centre so just move the sprite. I’ll leave this here just in case its useful for someone in the future

3 Likes