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: 26937 reply_id: 326937[/import]

Yes, lookup removeBody. Rather than changing the shape you’ll have to remove and re-add the body (and associated collision listeners, etc.) [import]uid: 8271 topic_id: 26937 reply_id: 109329[/import]

So do i need to create the physics body and remove it in the same function or do i make two functions one that creates a physics body and then one to remove it?

This functionality is tied to a timer that runs endlessly so how does one call both functions? Through a callback?

I am a new at all of this any help would be great!

Scott

[import]uid: 46725 topic_id: 26937 reply_id: 109373[/import]

When you need to update the shape of the body call removeBody, work out your new shape, then call addBody. [import]uid: 8271 topic_id: 26937 reply_id: 109374[/import]

Hi,

I have been playing with this for a few days trying to get what you all described to work.

Here is a better explaination.

I have a triangle that i am drawing useing the display newLine routine. The triangle grows over time and gets narrower and wider or wider to narrower by using a table that change the angle a[frame] and b[frame] of the triangle. I draw the triangle using the follow:

xa = math.round((_W/2+r * math.sin(a[frame] * math.pi / 180)))
ya = math.round((_H/2-r * math.cos(a[frame] * math.pi / 180)))

xb = math.round((_W/2+r * math.sin(b[frame] * math.pi / 180)))
yb = math.round((_H/2-r * math.cos(b[frame] * math.pi / 180)))

And plot the coordinates using the following:

triangle = display.newLine( _W/2, _H/2, xa, ya)
triangle:append(xb,yb,_W/2,_H/2)

and you get an triangle.

Now I apply a physics body like this:

triangleShape = {_W/2-320,_H/2-480, xa-320,ya-480, xb-320,yb-480, _W/2-320,_H/2-480}
physics.addBody( triangle, “dynamic”, { friction=0.0, bounce=0,shape=triangleShape } )

When I run my code right now as is, the physics body writes over itself.

My question is how do I remove the physics body and redraw the physics body when the angle changes over time. IE going from wider to narrower over or narrower to wider.

Can someone outline the idea a little further? If you think it would help if you got my code let me know.

Please someone HELP!!

Scott

[import]uid: 46725 topic_id: 26937 reply_id: 110029[/import]

Just as you have there, but when you change the size/shape of the body call removeBody first, then use your code to add another one. The graphics object will stay the same but you will be removing the physics information and creating brand new physics information. If there is any velocity or angular momentum you’ll need to record those in temp variables first and then reapply them, but the API documentation can give you all the physics body functions you’ll need. [import]uid: 8271 topic_id: 26937 reply_id: 110168[/import]