Making group rotate around their centre

Hi all

I’m using a button to rotate a group of objects, and so far have gotten it to work to a certain extent.

The issue I’m having is that I’m trying to rotate a group of objects around the group’s centre - so no matter how far apart the objects are, they rotate around the centre of the group.
|
so - - becomes | when rotated

  • |

Can anyone help with this please?
Thanks in advance [import]uid: 40538 topic_id: 8040 reply_id: 308040[/import]

My little example came out wrong and won’t let me edit it >.<
The same question still remains - how do I rotate a group of objects based on their centre point? [import]uid: 40538 topic_id: 8040 reply_id: 28650[/import]

If you’re using just display objects then put them in the same display group and set it’s rotate property.

If you’re using physics (I’ll assume gravity set to 0,0) you can use weld joints to attach them to a central object. Then use applyAngularImpulse, though I don’t have experience with that.

http://developer.anscamobile.com/content/game-edition-physics-bodies#body:applyAngularImpulse [import]uid: 8271 topic_id: 8040 reply_id: 28744[/import]

I’ve tried all sort but it still doesn’t seem to work! Please can someone have a look at this code:

localGroup = display.newGroup() -- Using Director Class....  
  
--[Code for 1st group here]  
  
localGroup2 = display.newGroup()  
localGroup2:setReferencePoint(display.CenterReferencePoint);  
local outerFrame = display.newImage("images/outerframe.png")  
localGroup2:insert(outerFrame)  
  
local block = display.newImage("images/block.png")  
block.x = 100; block.y = 200  
physics.addBody( block, "static", 0)  
localGroup2:insert(block)  
local button = display.newImage( "images/button.png" )  
button.x = display.stageWidth / 2  
button.y = display.stageHeight - 50  
localGroup2:insert(button)  
local rotateIt = function( event )  
 if 1 == event.numTaps then  
 localGroup2.rotation = 90  
 end  
end  
  
button:addEventListener( "tap", rotateIt )  
return true -- end group 2  
end --end group 2  
return true -- end group 1  
end --end group 1  

As you’ve probably noticed, I am using director class. Also, I’m not sure if the groups are nested correctly (I’m used to tables coded like in HTML, so I’m not exactly sure about these).
Thanks! [import]uid: 40538 topic_id: 8040 reply_id: 29027[/import]

*** Duplicate Post *** [import]uid: 40538 topic_id: 8040 reply_id: 29028[/import]

i’ve tried a litte bit.

the problem was the referencePoint. in the API (http://developer.anscamobile.com/reference/index/objectsetreferencepoint) was the info, that you must update the refPoint after changes to the groupmembers.
so in this example, i put the setReferencePoint after the two inserts.

ah, and i don’t insert the button. was a little bit funny, to tap on an rotating button…
[lua]local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 0 )

localGroup2 = display.newGroup()

local outerFrame = display.newImage(“images/crate.png”)
outerFrame.x = 50; outerFrame.y = 100
localGroup2:insert(outerFrame)

local block = display.newImage(“images/enemy.png”)
block.x = 100; block.y = 200
physics.addBody( block, “static”, 0)
localGroup2:insert(block)

local button = display.newImage( “images/button.png” )
button.x = display.contentWidth / 2
button.y = display.contentHeight - 50
–localGroup2:insert(button)

localGroup2:setReferencePoint(display.CenterReferencePoint);

local rotateIt = function( event )
print( "R! center: ", display.CenterReferencePoint, block.CenterReferencePoint )
if 1 == event.numTaps then
localGroup2:rotate( 5 )
end
end

button:addEventListener( “tap”, rotateIt )[/lua] [import]uid: 42078 topic_id: 8040 reply_id: 29232[/import]

Thank you so much sidasa!! You don’t understand how much this piece of code has frustrated me…

The only issue left now is that when the group is rotated, the block leaves some sort of “ghost block” which is dynamic - is there anyway to remove this aswell?

Many, many thanks again!

And I’m glad you found the rotating button funny lol [import]uid: 40538 topic_id: 8040 reply_id: 29238[/import]

It’s best not to use Reference Points with physics objects as there are known bugs, so you can never be sure if it’s you or the SDK that’s the issue. While at first I thought this could be a show stopper kind of bug, you can actually get by connecting your physics objects to invisible objects using joints, and then moving the invisible object in the motion you desire.

For instance, you could create a invisible circle, add it to physics, and then connect your objects to the circle using joints where you want them. Then, rotate the invisible circle, which rotates all the objects connected to it.

Good luck!
[import]uid: 36054 topic_id: 8040 reply_id: 29589[/import]

I’ve been contemplating using joints - what would you say is the best type of joint in this situation? I’ve been looking at the joint APIs for Corona, but they don’t seem to apply to what I want.

A perfect example would be a windmill, where the middle bit of the fan would be the invisible object, and the fan blades are the objects needing to be rotated.
Thanks for your help!
Edit: Actually that’s probably a bad example! Erm…say if the fan blades were a bit of a distance away from the centre, not directly touching the middle bit… [import]uid: 40538 topic_id: 8040 reply_id: 29601[/import]

You would create a large enough invisible circle so that the location of the “joint” would be inside the circle (that resolves your concern about the blades being offset from the very center).

If you want the blades of the windmill not to move even if objects collide with them, you would use a weld joint. If you wanted them to be able to move a certain extent, you would use a pivot joint and then adjust the range of rotational motion allowed. [import]uid: 36054 topic_id: 8040 reply_id: 29603[/import]

Cheers for that blasterv - I’ve managed to get the invisible circle to rotate, but it’s not taking the connected object with it.

Here’s the code:

  
localGroup2 = display.newGroup()  
  
local block = display.newImage("images/block.png")  
block.x = 200; block.y = 200  
physics.addBody( block, "static", 0)  
localGroup2:insert(block)  
local circle = display.newCircle( 80, 20, 250 )   
circle:setFillColor( 0, 0, 0 )   
physics.addBody( circle, "static", 0)  
localGroup2:insert(circle)  
  
myJoint = physics.newJoint( "pivot", circle, block, 80,200 )  
myJoint.isLimitEnabled = true  
myJoint:setRotationLimits( -20, 20 )  
lowerLimit, upperLimit = myJoint:getRotationLimits()  
local button = display.newImage( "images/button.png" )  
button.x = display.contentWidth - 50  
button.y = display.contentHeight - 75  
local rotateIt = function( event )  
 if 1 == event.numTaps then  
 circle:rotate( 90 )  
 end  
end  
   
button:addEventListener( "tap", rotateIt )  
  

Trying to get it to rotate 90 degrees :S [import]uid: 40538 topic_id: 8040 reply_id: 29607[/import]

There are a couple things I see on here I don’t like. I’ve never seen zero used as the third argument when adding a body. Not sure if that makes a difference though. I usually just use the first two arguments if I don’t have any mass/collision information to pass to the physics object.

The second thing is that the circle should be a static object, which you did, but the block should be dynamic, which it is not.

Do either of those help? [import]uid: 36054 topic_id: 8040 reply_id: 29609[/import]

The removal of zero didn’t make any difference - from what I can see anyway.
The object has to be dynamic, otherwise it just, sorta, flops. I need to object to hold itself up, ie. making it static. [import]uid: 40538 topic_id: 8040 reply_id: 29611[/import]

You have two objects, a block and a circle. The circle needs to be static so that it doesn’t move. The block should be dynamic (in your code above it is not). When you put the two objects together with the joint, it prevents the dynamic object from moving at the point the joint is set, which means it won’t move unless the circle moves (if welded) or if an object collides with it, it will react as much as it can without moving from the joined position (if pivoted) [import]uid: 36054 topic_id: 8040 reply_id: 29616[/import]

Only thing is, that I don’t want the pivot to be an object which is collidable, and if it’s gonna be an extra large circle to hold the other object in, it’s not something I want really.

The block, if dynamic, is still moving - swinging to be exact, so I think it’s my code which needs tweaking if anything. [import]uid: 40538 topic_id: 8040 reply_id: 29620[/import]

You would then use “weld” instead of “pivot” in the first argument of the newJoint call. [import]uid: 36054 topic_id: 8040 reply_id: 29623[/import]

This is driving me nuts!

Just changing “pivot” to “weld” does not fix it for some reason.

In hybrid mode, when I use “pivot” I am seeing the joint & block rotating, but the block is swinging around the point where the joint meets the block.

However, when I use weld, the joint is rotating, but the block is staying put! [import]uid: 40538 topic_id: 8040 reply_id: 29637[/import]

The only thing I can think of doing at this point is showing you a working example of what I think you want:

local physics = require("physics")  
physics.start()  
  
--Set Background  
background = display.newRect( 0, 0, display.contentWidth, display.contentHeight )  
background:setFillColor( 255, 255, 255, 255 )  
  
local circle = display.newCircle( 125, 200, 100 )   
circle:setFillColor( 0, 0, 255, 255 )   
physics.addBody( circle, "static")  
  
local block = display.newRect(circle.x,circle.y,50,50)  
block.x = circle.x + 100  
block.y = circle.y  
block.rotation = 45  
block:setFillColor( 0, 0, 0, 255 )   
physics.addBody( block, "dynamic")  
  
myJoint = physics.newJoint( "weld", circle, block, block.x, block.y )  
   
local rotateIt = function( event )   
 circle.rotation = circle.rotation + 1   
end  
   
Runtime:addEventListener( "enterFrame", rotateIt )  

Let me know if this explains what you need to do. [import]uid: 36054 topic_id: 8040 reply_id: 29651[/import]

Appreciate your help blasterv…

The code works on it’s own, but unfortunately is not what I’m looking for.

The objects are rotating constantly like a gear - my objects need to be rotated when a button is pressed.
Also when I put the above code, in the code I already have, I just get a black screen.
[import]uid: 40538 topic_id: 8040 reply_id: 29707[/import]

The problem here, is like horacebury already hinted, the use of the physic engine and normal property setting. that won’t work.

this should help:
https://developer.anscamobile.com/forum/2011/02/04/rotating-physics-body

[import]uid: 42078 topic_id: 8040 reply_id: 29752[/import]