Hey there !
I’m building a toothed gear shaped :

It’s made of several sahpes :
-
The gear itself, a circular shape at the center.
-
4 rectangles, creating the “tooth bar”.
-
8 circles so that the tooth are rounded-shape.
All those shapes are then joint using several “weld” joints.
I’m also creating a physical square at the center, set as a sensor, in order to “pin” the gear.
I want the gear to turn ot itself when something collides on it. For this exemple, I’ve got a function which is spawning small little cubes at the top of the screen.
It works almost fine : the gear turns on itself. But the tooth of the gear don’t seem to collide with the spawned cubes.
From my previous test, it seems that using a “weld joint” somehow “breaks” the collision detection.
What is weird is that if I don’t specify the density of the small cubes, the collisions seems to work fine.
Here’s the full code (copy/past in a new main.lua file and it will work fine)
physics = require"physics" physics.start() physics.setDrawMode("debug") local gearGroup = display.newGroup(); ---------------------------------------------------------------------------------------------- -- SOME INFO DEFINING THE GEAR PROPERTIES ---------------------------------------------------------------------------------------------- self = {} self.xpos = display.contentWidth\*0.5 self.ypos = display.contentWidth\*0.5 self.size = 256 self.rotation = 0 ---------------------------------------------------------------------------------------------- -- CREATION OF THE GEAR ---------------------------------------------------------------------------------------------- -- CREATING THE CENTER ROUDNED SHAPE OF THE GEAR self.image = display.newCircle(0,0,256) self.image.anchorX = 0.5; self.image.anchorY = 0.5 self.image.health = 1; self.image.x = display.contentWidth\*0.5; self.image.y = display.contentWidth\*0.5; self.image.width = self.size; self.image.height = self.size; self.image.rotation = self.rotation gearGroup:insert(self.image) local physicSize = self.size\*0.5 - 95\*(self.size/512) physics.addBody(self.image, "dynamic", {density= 2, radius=physicSize, friction=10, bounce=0.1}) -- CREATING A PHYSICAL OBJECT, WHICH WILL BE SET AS A SENSOR TO "PIN" THE GEAR self.image.center = display.newCircle(0,0,50) self.image.center.x = self.xpos self.image.center.y = self.ypos gearGroup:insert(self.image.center) -- Pinning the physics.addBody(self.image.center, "static") self.image.center.isSensor = true self.image.center.jointure = physics.newJoint("pivot", self.image.center, self.image, self.xpos, self.ypos) -- Loop, creating 4 "tooth bar" self.image.tooth = {} for i=1, 4 do -- Creating the bar self.image.tooth[i] = {} self.image.tooth[i].image = display.newRect(0,0,100,100) self.image.tooth[i].image.x = self.xpos self.image.tooth[i].image.y = self.ypos self.image.tooth[i].image.width = 68 \* self.size/512 self.image.tooth[i].image.height = 512 \* self.size/512 - (69\*0.5\*self.size/512)\*2 self.image.tooth[i].image:setFillColor(0,1,0) self.image.tooth[i].image.rotation = -45 + i\*45 + self.rotation gearGroup:insert(self.image.tooth[i].image) -- Adding physics physics.addBody(self.image.tooth[i].image, "dynamic"); -- Creating a joint between the tooth bar and the "center" of the gear self.image.tooth[i].jointure = physics.newJoint("weld", self.image, self.image.tooth[i].image, self.image.x, self.image.y) -- Creating the rounded tooth of the "bars" local rayon = self.image.tooth[i].image.height\*0.5 - 3 local angle = -45 + i\*45 + self.rotation self.image.tooth[i].roundA = display.newCircle(0,0,68\*0.5\*self.size/512) self.image.tooth[i].roundA:setFillColor(1,0,0) self.image.tooth[i].roundA.x = self.xpos + rayon\*math.cos(math.rad(angle)) self.image.tooth[i].roundA.y = self.ypos + rayon\*math.sin(math.rad(angle)) self.image.tooth[i].roundB = display.newCircle(0,0,68\*0.5\*self.size/512) self.image.tooth[i].roundB:setFillColor(0,0,1) self.image.tooth[i].roundB.x = self.xpos - rayon\*math.cos(math.rad(angle)) self.image.tooth[i].roundB.y = self.ypos - rayon\*math.sin(math.rad(angle)) -- Adding the rounded tooth to the gear layer gearGroup:insert(self.image.tooth[i].roundA) gearGroup:insert(self.image.tooth[i].roundB) -- Add physics to the rounded tooth physics.addBody(self.image.tooth[i].roundA, "dynamic", {radius=68\*0.5\*self.size/512}) physics.addBody(self.image.tooth[i].roundB, "dynamic", {radius=68\*0.5\*self.size/512}) self.image.tooth[i].roundAJointure = physics.newJoint("weld", self.image.tooth[i].roundA, self.image, self.image.tooth[i].x, self.image.tooth[i].y) self.image.tooth[i].roundBJointure = physics.newJoint("weld", self.image.tooth[i].roundB, self.image, self.image.tooth[i].x, self.image.tooth[i].y) end ---------------------------------------------------------------------------------------------- -- SPAWNING CUBES AT THE TOP OF THE SCREEN ---------------------------------------------------------------------------------------------- spawnItemList = {} spawnItemCount = 0 local function spawnItems() spawnItemCount = spawnItemCount + 1 local xpos = math.random(display.contentWidth) spawnItemList[spawnItemCount] = display.newRect(0,0,50,50) spawnItemList[spawnItemCount].x = xpos spawnItemList[spawnItemCount].y = 0 physics.addBody(spawnItemList[spawnItemCount], "dynamic") end local spawnTimer = timer.performWithDelay(500, spawnItems, 0)
With this line, collision don’t work :
physics.addBody(spawnItemList[spawnItemCount], "dynamic", {density=10})
With this one, it works fine :
physics.addBody(spawnItemList[spawnItemCount], "dynamic")
Unfortunately, in my case, I really don’t wanna remove the density of whatever enters in collision with the gear.
Do you have any idea on how I can fix that ? I really need to be able to collide with the tooth gear ! Or maybe I should build my gear in an other way ?
Thanks a lot in advance
But I’m being a little bit lazy and I’d like to find the fastest and easiest solution…