On collision you need to set the x/yReference points of the non-base object to the same as the base object. And forget about putting them in the same group, put them in separate groups. So:
otherGroup.xReference = baseGroup.xReference
otherGroup.yReference = baseGroup.yReference
then apply a rotation to each object (you have to because they’re in separate groups now). This makes the groups rotate around the same point.
Full code (using location-based collision, not physics engine, but it shouldn’t matter)
[code]---------------------------------------------------------------------
– CREATE DISPLAY OBJECTS
–> Create base group and insert a display object for it
local baseGroup = display.newGroup()
local base = display.newCircle(baseGroup,0,0,50)
base:setFillColor(255,0,0)
base:setReferencePoint(display.CenterReferencePoint)
base.x = 0; base.y = 0
–> a box in the center to help see rotation
local baseIndicator = display.newRect(baseGroup,0,0,20,20)
baseIndicator:setFillColor(255,255,255)
baseIndicator:setReferencePoint(display.CenterReferencePoint)
baseIndicator.x = 0; baseIndicator.y = 0
baseGroup:setReferencePoint(display.CenterReferencePoint)
baseGroup.x = 240; baseGroup.y = 160
–> Create otherObject group and object
local otherGroup = display.newGroup()
local otherObject = display.newRect(otherGroup,0,0,20,20)
otherObject:setFillColor(0,255,0)
otherObject:setReferencePoint(display.CenterReferencePoint)
otherObject.x = 50; otherObject.y = 0
otherGroup:setReferencePoint(display.CenterReferencePoint)
otherGroup.x = 400; otherGroup.y = 160
–> collision by position not physics engine
function collisionCheck()
if otherGroup.x <= 290 then
–> THIS IS WHERE THE MAGIC HAPPENS
–> change reference point of otherGroup to that of baseGroup
otherGroup.xReference = baseGroup.xReference; otherGroup.yReference = baseGroup.yReference
–> then start rotating it. This will rotate it *once*
transition.to(otherGroup,{time = 1000, rotation = -360})
–> and be sure to remove the collision listener
Runtime:removeEventListener(“enterFrame”, collisionCheck)
end
end
Runtime:addEventListener(“enterFrame”, collisionCheck)
– LOOP ANIMATION FOR BASE OBJECT
function startAnim()
rotateIt()
transition.to(otherGroup, {time = 1000, x = 290})
end
function keepRotating ()
baseGroup.rotation = 0
rotateIt()
end
function rotateIt()
transition.to(baseGroup, {time = 1000,rotation = -360,onComplete = keepRotating})
end
startAnim()[/code] [import]uid: 41305 topic_id: 7451 reply_id: 26487[/import]