physics bodies

Is it possible to assign lots of physics bodies one “material”, such that if a ball were to hit some spikes in a game, you could have a specific function for when the ball hits “spike material”, instead of having to do separate functions for each set of spikes? [import]uid: 116264 topic_id: 21085 reply_id: 321085[/import]

You could give the objects a type, eg;

ball.material = “rubber”
spikes.material = “metal”

Then have a listener that fires on any collision (see the sample code in CoronaSDK > SampleCode > Physics folder on your computer) and use an if statement, then if they lined up destroy the ball or whatnot.

Peach :slight_smile: [import]uid: 52491 topic_id: 21085 reply_id: 83355[/import]

Ok I will try that thanks. How do I assign properties to “rubber” and “Spikes” such as bounce, friction etc. Also, would the “if” part of the function be something like:

“if other.material == “spikes” then…” [import]uid: 116264 topic_id: 21085 reply_id: 83359[/import]

You don’t assign the materials in the physics section, it’s just something you’re defining.

So when you create the ball, do the image, x and y, etc, then do;
ball.material = “rubber”

You are correct about the if statement;
[lua]if event.other.material == “rubber” then
– do stuff
end[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 21085 reply_id: 83486[/import]

Ok thanks for you help [import]uid: 116264 topic_id: 21085 reply_id: 84027[/import]

You could assign your density, friction and bounce via variables:

local physicsProperties = {}
physicsProperties[“spike”] = {density=2.0, friction=0.8, bounce=0.1}
physicsProperties[“rubber”] = {density=0.9, friction=1.5, bounce=1.0}
then following Peach’s example:

ball.material = “rubber”
physics:addBody(ball,physicsProperties[ball.material])

Seems like more work than just doing:

rubberProp = {density=0.9, friction=1.5, bounce=1.0}
ball.material = “rubber”
physics:addBody(ball,rubberProp)

though.
[import]uid: 19626 topic_id: 21085 reply_id: 84038[/import]