Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Moving group of phyics objects
Started by vonncc123 Feb 13 2019 10:55 PM

14 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

vonncc123

[GLOBAL: userInfoPane.html]
vonncc123
  • Contributor

  • 236 posts
  • Corona SDK

Good day, I'm trying to move a group of physics objects, but it seems that the physics body, and the object itself is not the same? How can I move a group with multiplte physics body in it



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 723 posts
  • Corona SDK

A bit more information would be helpful.

Once you give a physics body to a display object, then that object is pretty much owned by the physics engine. You can still move display objects and display groups around, but this may result in weird and jittery motion as the physics engine is trying to control the objects also.

I'm not sure about your particular case, but you should be able to just move the group and everything will work just fine. If you need to move a group of objects and not an entire display group, I'd just loop through the objects and move them that way. Note, however, that when I say move, I mean "object.x = value" kind of moving. Using transitions with dynamic physics objects is a no go.



[TOPIC: post.html]
#3

vonncc123

[GLOBAL: userInfoPane.html]
vonncc123
  • Contributor

  • 236 posts
  • Corona SDK

for example objects A,B,C,D ha physics body. they are all inserted on a group named as MyGroup, once I modify the position of the MyGroup group, it seems that A,B,C,D's Physics body are left behind, while their game object position is moved.



[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 723 posts
  • Corona SDK

I'm presuming that you are using hybrid draw mode, which shows that the bodies and the assets don't match up?

If your code is as simple as (or something along these lines):

MyGroup.x = MyGroup.x + 100

Then they really should move along with the group.



[TOPIC: post.html]
#5

vonncc123

[GLOBAL: userInfoPane.html]
vonncc123
  • Contributor

  • 236 posts
  • Corona SDK

I'm using transition.to? WHy doesn't it work?



[TOPIC: post.html]
#6

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 723 posts
  • Corona SDK

https://docs.coronalabs.com/guide/physics/limitations/index.html#transitions



[TOPIC: post.html]
#7

vonncc123

[GLOBAL: userInfoPane.html]
vonncc123
  • Contributor

  • 236 posts
  • Corona SDK

Thanks for the help



[TOPIC: post.html]
#8

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 489 posts
  • Corona SDK

@vonncc123 You can move the display group with physics objects in it but you will need to move all the other display groups with physics objects in them as well.  Don't use transition, use hard coordinates but you can use an enterFrame function to simulate transitions.

Runtime:addEventListener( 'enterFrame', yourFunctionToMoveGroups )

Let us know if you have any hard code you would like us to take a look at.



[TOPIC: post.html]
#9

vonncc123

[GLOBAL: userInfoPane.html]
vonncc123
  • Contributor

  • 236 posts
  • Corona SDK

local rect = display.newRect(_W/2,_H/2,50,50)
physics.addBody(rect)
rect.bodyType = "dynamic"
local rect2 = display.newRect(_W/2,_H/2 + 70,50,50)
physics.addBody(rect2)
rect2.bodyType = "static"

group:insert(rect)

local function moveMe()
	group.x = group.x + 1
end

Here is my code, the group is still not falling, the object is moving to the right, but it still doesn't fall.



[TOPIC: post.html]
#10

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 723 posts
  • Corona SDK

What are you trying to achieve?

Based on your code, all you do is create two objects, one dynamic, one static, and place the dynamic object in the group. Then you just move the group to the right. So, what's wrong?



[TOPIC: post.html]
#11

vonncc123

[GLOBAL: userInfoPane.html]
vonncc123
  • Contributor

  • 236 posts
  • Corona SDK

Yup, exactly what you said, but the moving group's objects are not falling, they're just moving to the right. (Assume that the group has may objects), when I do it 1 by 1, the moving objects are falling, but when I do it as a group, they don't fall



[TOPIC: post.html]
#12

vonncc123

[GLOBAL: userInfoPane.html]
vonncc123
  • Contributor

  • 236 posts
  • Corona SDK

What I'm trying to achieve is that, the Group will fall as one.



[TOPIC: post.html]
#13

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,568 posts
  • Corona SDK

My suggestion don't move the group.  

 

I know it would be nice, but this simply won't work the way you want it to with with physics bodies.

 

You need move the objects individually and keep all your groups aligned, otherwise your physics bodies will misbehave.



[TOPIC: post.html]
#14

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 723 posts
  • Corona SDK

To @roaminggamer's point, as far as I've understood, you can move a group with physics objects in it, but you need to be careful about it. The physics simulations will run correctly since all objects in the said group will be offset by the same amount. However, having multiple groups with physics objects in them will likely run into issues.

Taking your sample code and adding the necessary bits there, i.e.
 

local physics = require("physics")
physics.setDrawMode( "hybrid" )
physics.start()

local group = display.newGroup()

local rect = display.newRect( 130, 100, 50, 50 )
physics.addBody( rect )
group:insert( rect )
local rect2 = display.newRect( rect.x+60, 100, 50, 50 )
physics.addBody( rect2, "static" )
group:insert( rect2 )


local function moveMe()
	group.x = group.x + 1
end

Runtime:addEventListener( 'enterFrame', moveMe )

does result in the dynamic object(s) falling, as they should, and every display object on the screen moves right as per moveMe function.

If you want for the group to fall, then you'd have to move the group down (if I am understanding you correctly). If you are having problems with not all objects falling, then that's due to one of them being a static object, which does not react to gravity.



[TOPIC: post.html]
#15

vonncc123

[GLOBAL: userInfoPane.html]
vonncc123
  • Contributor

  • 236 posts
  • Corona SDK

That's very unfortunate, Thanks for the answers though, had to do it manually. Had to manually move the x and y axis of everything, but the scaling will be as a group




[topic_controls]
[/topic_controls]