Stick and rotate objects

Hi can anybody advise what’s the best approach to stick object A to object B when colliding and by rotating the object A, also rotate the object B. Kind of like object B would be a new part of object A.

Don’t know If I expressed in a clear manner, sorry this is not my native language :wink:

Thanks in advance!. [import]uid: 8933 topic_id: 7451 reply_id: 307451[/import]

Make A a group, on collision insert object B into group A, then rotate group A. [import]uid: 41305 topic_id: 7451 reply_id: 26371[/import]

Hi Craig, thanks for your suggestion. I tried this approach however when I rotated the main group (ie group A), the both pieces where disappearing from the screen. Then I read that the rotation is different when you do it for a single display object, than when you rotate a group. I think I don’t have quite clear this concept.

Could you be so kind and illustrate with some coding please? [import]uid: 8933 topic_id: 7451 reply_id: 26414[/import]

Be careful with the referencePoint of your group. Try to set it with :

groupA:setReferencePoint (display.CenterReferencePoint)

http://developer.anscamobile.com/reference/index/objectsetreferencepoint [import]uid: 8970 topic_id: 7451 reply_id: 26421[/import]

Hi, just tested it and worked however It’s not exactly what I looking forward. I’m trying to get the both pieces glued and only rotate one of them, for example:

-Piece A is like a base where other pieces will stack

-Piece B will be stacking when colliding with the piece A.

Now if I rotate the base aka piece A, I want it to keep it’s original X,Y position and piece B just rotate on top of piece A.

If I try this within a group and I rotate the group it won’t keep the original position, but depending on the display.“setReferencePoint” method the both pieces will rotate together.

I thank you for your kind advise. [import]uid: 8933 topic_id: 7451 reply_id: 26426[/import]

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]

Hi Craig, I just want to say thank you for your time and advise. I’ve just been messing around with the code you provided and I think I’ve got it!.

Once more thanks for all mate!.

*****