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.
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]
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 )
local rotateIt = function( event )
print( "R! center: ", display.CenterReferencePoint, block.CenterReferencePoint )
if 1 == event.numTaps then
localGroup2:rotate( 5 )
end
end
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]
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]
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]