Physics Body question

Hi all,

This is my first post in the Corona forums. I am building a game that uses a triangle that is being drawn using the newLine display object. I am calculating the x,y points using angles that I feed into a function. The angles will change over a period of time changing the size of the triangle. Ultimately this element will be used as a physics body sensor.

My question is this.

Can i change the physics body shape over time to match the changes in the triangle I am drawing? I have successfully drawn the physics body shape using the same x,y points? Does the Corona / Cocos2d physics engine allow this? Please help!

If anyone needs any additional information please let me know.

Thanks

Scott
[import]uid: 46725 topic_id: 26936 reply_id: 326936[/import]

Hi Scott,
Welcome to Corona and to the forums! This is your best source of help in most matters, and there are many of us here to assist you.

Yes, you can change the physics body “gradually”, but not by changing the actual angles of the *same* body. Instead, you will need to delete the current body and immediately re-apply a new body based on the updated angles. This method shouldn’t present any problems, but note that adding physics bodies can cause a tiny performance hit on older devices (iPhone 3G perhaps). On newer devices, you shouldn’t notice any issues, especially if you optimize your re-calculating function to the maximum.

Best of luck,
Brent Sorrentino
Ignis Design
[import]uid: 9747 topic_id: 26936 reply_id: 109319[/import]

Brent

So would I need two different physics bodies. 1 for the current and then one for the next frame?

S [import]uid: 46725 topic_id: 26936 reply_id: 110039[/import]

Hi Scott,

Essentially, you’d be deleting the current body and instantly replacing it with a new one. All of this would happen in the same game cycle, so you shouldn’t even notice it.

As I mentioned though, it’s mildly processor-taxing to be doing this delete/add constantly. Does the triangle need to be constantly “sensing” other objects as the user draws/re-draws it? Or do you just need to apply a sensor when the user is finished drawing?

There are some useful non-physics collision detection routines out there, which might be more efficient for the cycles when you’re changing the shapes around. Then, in the final state, you could apply an actual physics body to it for use as the sensor.

Brent Sorrentino
[import]uid: 9747 topic_id: 26936 reply_id: 110070[/import]

Hi Brent.
Something that I might not be mking clear. When i change the angle it is not a hard change, I am chanigning it gradually by 1 degree increments over a period of 3 seconds. During that change, the object has to detect a collision and act as a sensor.

So i want to change the physics body on each frame. Is that doable?

Scott [import]uid: 46725 topic_id: 26936 reply_id: 110162[/import]

OK, in this case…

  1. First, try deleting the current body, then calculating the “new” coordinates, then apply a new body. Do this repeatedly over the course of the 3 seconds, either during a Runtime listener (that you toggle on to start and then off at the end of 3 seconds)… or do similar during a slightly “slower” repeating timer, perhaps every 100 milliseconds instead of the Runtime listener’s 16 or 33 millisecond interval.

http://developer.anscamobile.com/reference/index/physicsremovebody
http://developer.anscamobile.com/reference/index/physicsaddbody

  1. If the performance of the above method isn’t acceptable on lower-end devices, or if the physics engine chokes for some odd reason (it shouldn’t), then you could try using non-physics collision routines. One example comes from Rob Miracle; I haven’t tested this personally but I’m sure it works very nicely. :slight_smile:

http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

EDIT: I just noticed that Rob’s example only handles rectangular and circular collisions, not triangular. But I swear there was a post recently about handling polygonal shapes too… can’t seem to find it, but I’ll look a bit further.

Best of luck!
Brent Sorrentino
[import]uid: 9747 topic_id: 26936 reply_id: 110172[/import]