How can I detect 2 objects in physics

Thank you piotrz55, for explaining how “.” attributes work… much better at this than I am. :wink:

As far as collisions go, If you don’t want to create tables for your objects (not advisable) then just specify a unique name/class/type/identifier for your objects.

playerBall.Myname = “userBall”
opponentBall.myName = “badBall”

playerBall.class = “userBall”
opponentBall.class = “badBall”

playerBall.obj_Type = “userBall”
opponentBall.obj_Type = “badBall”

then in a collision function, when checking the phase (began, moved, ended) you can just check the (as piotrz55 says - “.nameOfProperty”) of the other object to see if it is indeed the object you’re checking for, and then code what you want to happen.
In your case you have a .myName attribute assigned to both the ball and rect14.
So in your collision function just check if the other object’s .myName “=” what you have specified for rect14.

-Saer

Now on collision.

First we must include physics and make it start

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

Then we need some variables

local hasSoundPlayed = false local mySound = audio.loadSound( "BOINK.wav" )

my sound is BOINK.wav, you must write name of your sound.

Then let’s create 2 objects and floor

local myBall = display.newCircle( 150, 50, 20 ) myBall:setFillColor( 255, 0, 0 ) local myCrate = display.newRect( 50, 150, 50, 50 ) local myFloor = display.newRect( 0, 400, 300, 50 ) myFloor:setFillColor( 0, 255, 0 )

If you run simulator then there wil be 3 objects.

Remember as we talked about properties? For objects created with display.*  we can also as for tables add properties so

myCrate.name = "crate" myBall.name = "ball"

Now we must make our objects physical

physics.addBody( myBall, "dynamic", { density = 0.1, friction = 0.9, bounce = 0.5, radius = 20 } ) physics.addBody( myCrate, "dynamic", { density = 100.0, friction = 0.9, bounce = 0.0 } ) physics.addBody( myFloor, "static", { density = 1.0, friction = 0.9, bounce = 0.0 } )

This will make myBall and myCrate react to gravity (“dynamic”) and make myFloor being still in place (“static”).

Now we want to detect collision with myFloor. 

Do you remember how I wrote that for each event there is table with different properties and that “collision” had

event.other

? We will use it. event.other is the object with myFloor is colliding. As I also said there is 

event.phase

Which will tell us phase of collision. It can be “began” or “ended”. Why event.phase ? Because corona will call our function twice so we must somehow tell what happened - is collision begining or ending.

So if we have event.other as object with which myFloor collided then we can check if it has .name property and is one of our names (“ball”, “crate”).

So taking all into one we have

local physics = require( "physics" ) physics.start() local hasSoundPlayed = false local mySound = audio.loadSound( "BOINK.wav" ) local myBall = display.newCircle( 150, 50, 20 ) myBall:setFillColor( 255, 0, 0 ) local myCrate = display.newRect( 50, 150, 50, 50 ) local myFloor = display.newRect( 0, 400, 300, 50 ) myFloor:setFillColor( 0, 255, 0 ) myCrate.name = "crate" myBall.name = "ball" physics.addBody( myBall, "dynamic", { density = 0.1, friction = 0.9, bounce = 0.5, radius = 20 } ) physics.addBody( myCrate, "dynamic", { density = 100.0, friction = 0.9, bounce = 0.0 } ) physics.addBody( myFloor, "static", { density = 1.0, friction = 0.9, bounce = 0.0 } ) local function onCollisionListener( self, event ) print(event.other.name, event.phase) -- we will exexute it for both phases "began" and "ended" but you can give here other "if" statement to differ them. if event.other.name == "ball" then if hasSoundPlayed == false then audio.play( mySound ) hasSoundPlayed = true end end end myFloor.collision = onCollisionListener myFloor:addEventListener("collision", myFloor)

Hi piotrz55.

Thank you very much. For as long as I live I will remember and will be grateful to you

that you were the one to teach me how to do the collision event.

it works!! great.


Now I don’t really think I would have been able to make it work on my own.

you add the variable – local hasSoundPlayed = false

you use it here – if hasSoundPlayed == false then

and here – hasSoundPlayed = true

before I had

– Runtime:addEventListener(“enterFrame”, sound )

and now it’s

– myFloor:addEventListener(“collision”, myFloor).

So I saw a few changes but big changes.

now I will try to actually use that in my real app

that will be the challange.

And also I did not see at all the

if phase == “began” or “ended”

I will try to make that work, now that I see more or less the logic.

I’m not going to say that I get it 100% but this will save my life

Thank you one more time.

your friend

Victor

I don’t think I would have think of that.

the other thing is the change in – Runtime and addEvent

@Victor: I would recommend to read one or two books about Corona SDK (and Lua).

Dr. Brian Burton for example has written a few books about Corona SDK´s basics and explains how everything works. http://www.burtonsmediagroup.com/books/mobile-app-development-with-corona-getting-started/

I highly recommend it to you :slight_smile: When I started almost 2/3 years ago I had no experience in LUA or any other programming language. 

Max / CineTek

Thnak you CineTek.

I already bought 1 of those books. I learned a lot from it.

but when I got to a certain point, it was too difficult for me

I did not understand it at all.

that was 4 months ago.


Now I can probably read it again and I might understand more thing.


also I bought the videos from Jay White, Great!


I bought 2 more books, and a lot of reading on line. and youtube.

I do like to study.


I have been working with lua for only 7 months.

before I didn’t know anything. 0, nil


but I really like it a lot, been able to create something that doesn’t exist

and then have people all over the world playing your game

it’s amazing! A wonderful feeling.


A little note to piotrz55…

I put your code and adapted for my app…

it’s better than magic!, better than I tough!

now my whole view of games has changed

I think I can create much, much more things, it’s amazing.

you Sir. You did something really good today.

Thank you

No “sir”, I’m lot younger :stuck_out_tongue:

You do not need to understand everything from the start :slight_smile:

You have more experience with Corona today! Read Brian´s book again, from the start - to make sure not to miss anything that is necessary for later chapters. 

Thank you. I will do that.


and for you piotrz55

I said “Sir” like they say in America’s Got Talent

out of admiration and respect even if you are 20 years old or 14.

Because I’m really happy with the code you did for me

I’m using it a lot and I will get a lot of that piece of information.

thanks one more time.

Good to be helpful :D 

Hope development is going good :stuck_out_tongue: