Physics Body Types/Reference point bug!

Okay, I have been working with corona for about 3 weeks now, and thinks its great! but it has some problems that I have a HARD time getting through. One of these seems to be Physics Bodies. And I just wanted to start a thread where these are THOROUGHLY explained. Because they are driving me crazy, and I think are not very well explained or documented by Ansca (Not complaining!)

So there are three types of physics bodies, we all know that:

Static: Cannot be moved and dont respond to gravity (walls and floors etc)

Kinematic: Can be moved, but dont obey gravity (great for sensors!)

Dynamic: Normal physics bodies, respond to touch and gravity

Then there is the collisions that can happen between them

Static ONLY collides with dynamic

Kinematic ONLY collides with dynamic

Dynamic collides with Dynamic/Static/Kinematic

This is where box2d seems really stupid, because for some reason, they think its easier for EVERYONE to use dynamic objects when clearly gravity is a big factor in designing games. I for one want to use kinematic objects more often, And I know everyone says “Just turn the gravity to 0 and use dynamic”, but I am one to say that it doesn’t always work like that, and almost always complicates things a lot later on.

Then there is the isSensor property, which still doesn’t make a lot of sense to me. From my understanding, its makes that body NOT interact with any other physics object, but still registers collision events, and obeys the same properties of the original body(dynamic/static/kinematic). For example:

A dynamic sensor will still fall from gravity, but will not collide with any static or kinematic objects *Physically*(it will still register collision events)

Or a Kinematic sensor will register collision events, but will not obey gravity, or affect any other normal physics body physically.

So here is where my problem is: I have a couple of dynamic bodies on my stage that I can drag around and throw just fine. They collide with walls and register collision events fine ONLY when they are the ones initiating those events. Then I have a “laser” in the middle of my screen that can rotate all the way around. It is an image that gets shown by changing the “isVisible” property. I have made the image itself a kinematic-sensor. So physics isnt affecting this “laser” in any way. I am rotating it with code.

I have all three bodies attached to a collision function:
[lua]local function laserCollision( self, event )
if(event.phase == “began” ) then
if(event.other.name) then
print(self.name…" with “…event.other.name…” Began")
end
elseif ( event.phase == “ended” ) then
if(event.other.name) then
print(self.name…" with “…event.other.name…” Ended")
end
end
return true;
end[/lua]
And here is what happens:

  1. When I drag the two dynamic bodies around myself, they register collisions fine! Even with the lazer.
    (ie. Square with Laser Began)
  2. When I throw the two dynamic bodies around, they still register fine
  3. When I rotate the laser around myself, it almost never registers any collision events with EITHER of the dynamic bodies. And when it does register, it is NOT how I expect. It happens when the lazer body isn’t even touching or anywhere near the dynamic body.
    (ie. Laser with Square Began)

So all around weird behavior from the laser collisions, and I think this might be because I am manually setting the .rotation property of the image that the body is attached to. But I cant think of a way around it!

-I have tried making the laser static (same behavior)
-I have tried making it dynamic (then it obeys gravity and falls off the screen)
-and YES, I have tried making the gravity 0, but then the menu I am creating acts nothing like it should (I need the dynamic objects to obey gravity)
-I cant make the laser NOT a sensor, because then it reacts weird with the object it is attached to( long story)

So I apologize for the long post, I just wanted to be really thorough, because I know its hard to help someone if you dont know what they need. If anyone has any questions about my problem or additions to the behavior of physics bodies, PLEASE let me know, or reply, because the more info the better. Thank you in advance!
[import]uid: 67379 topic_id: 11713 reply_id: 311713[/import]

Have you tried using “physics.setDrawMode()” to see what’s actually going on with the physics bodies? Is the console showing anything? Are you rotating the laser itself or the object it’s attached to? What type of object is the laser attached to and how is it attached to it (are you using joints or defining as a complex physicse body)? [import]uid: 27965 topic_id: 11713 reply_id: 42666[/import]

Thank you for the response! Let me try to answer all of those:

  1. Yes, I have it set to hybrid and everything looks normal, the bodies arent abnormally large or miss-colored or anything

  2. No, the console is just printing those collision statements mentioned earlier

  3. I am just setting lazer.rotation = whatever I need

4 The lazer is attached to the head of a rag doll. Not by a joint, but I simply made the reference point of the lazer to the left-center, and then do:
[lua]lazer.x = head.x; lazer.y = head.y;[/lua]

I cant really use a joint , because the only one that makes sense is a pivot joint, which doesn’t work for me because for some reason, whenever you create a joint in physics, it doesn’t attach to the current reference point of the laser, but just defaults to the center of the image itself. So if I tried:
[lua]laserJoint = physics.newJoint(“pivot”, head, laser, head.x, head,y)[/lua]

Then the joint acts really weird and whenever I move the lazer, the pivot point of the joint doesn’t stay at the center of the head, but moves up and down and messes up all of my other physics objects on the screen. [import]uid: 67379 topic_id: 11713 reply_id: 42695[/import]

Okay, I don’t really have any way to verify this for sure but I remember reading a while ago on the forums about how using a reference point other than center can cause physics oddities. Doing a quick forum search I found these posts:
http://developer.anscamobile.com/forum/2011/03/20/wrong-physics-collisions-when-body-rotated-and-reference-point-not-centered
http://developer.anscamobile.com/forum/2011/03/06/reference-point-breaks-physics
Unfortunately since you’re having problems I’m assuming it hasn’t been fixed in Corona yet and there doesn’t seem to be a fix or workaround that I can find. [import]uid: 27965 topic_id: 11713 reply_id: 42799[/import]

Ahh, case 4272. Reference point throwing off physics. Unfortunently it still seems to be open… And not resolved. @Ansca/@Carlos any update on the situation? The ticket was opened back in March according to the first of the above posts, but no update on the thread. Heading to bump it right now.

Anyways thank you so much for your help, you found the problem at least, and that comes as great news for me, as it means i am not going crazy :slight_smile: Props to @calebr! [import]uid: 67379 topic_id: 11713 reply_id: 42806[/import]

Sorry, I was a bit tired when I posted about the problem and didn’t offer a solution (well it’s really more of a hack). One workaround that I can think of would be to make a complex body out of the lazer and making one of them a sensor so that it won’t respond to anything. So if your lazer is a rectangle shape then just define two rectangle shapes as a complex physics body with one being “static” or “kinematic” and one being a sensor. Then you can have the reference point be the center of the physics body (which means you shouldn’t have any physics oddities) but your rotations should still work correctly. Not the most elegant solution but it should work. [import]uid: 27965 topic_id: 11713 reply_id: 42869[/import]

I solved this problem of rotation of a pinball flipper around one end of the image by using a transparent graphic so as to position the flipper to one side, putting the pivot at the center of the graphic. You can see the image file and working code at http://martinapps.com/files/pinballphysics.zip [import]uid: 23636 topic_id: 11713 reply_id: 49237[/import]