How can I detect 2 objects in physics

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:

You must have listener functikn listening to collision of on obkect with another

http://docs.coronalabs.com/api/event/collision/index.html

Hi piotrz55

I did not really understand this –

You must have listener functikn listening to collision of on obkect with another


and Saerothir

I have been there many times, and still I don’t get it.

That page is for people that already knows how to use it

and they just need to refresh the code. it’s not for me

I read that, I don’t understand it. I copy the crate1 code

I get errors.

would it be to much to ask one of you guys, just to type a very simple code

for collision.

I mean, if it’s simple, if it is too complicated, I guess I would have to learn a lot more for this

Did you take a look at the ‘event.phase’ bit of documentation?
http://docs.coronalabs.com/api/event/collision/phase.html
The example code is very self-explanitory. read every line and you’ll easily be able to see what is happening. :slight_smile:

The logic for basic collision handling is very simple -
Write a collision function > add an event listener (or depending on the scenario, a Runtime listener ) to the object you want said function to apply to.

Hi guys

I came out with a way of doing it.

I don’t know if it’s the “correct” way or the “best” way

but I can detect when the ball gets to a certain place

like this

local function sound ()     if ball.y \> 500 then         audio.play(win)     end end Runtime:addEventListener("enterFrame", sound )

The problem is that once the ball reaches 500

the audio plays, and plays and plays and keeps playing forever

because of the Runtime

How do I solve that.

I just want the function to execute once.

I guess you may find it so easy to do…

once you already know.

but I’m just going crazy…

Honestly I do need someone to be so kind to actually tell me how

not just to send me to another link.

Please, anyone out there, I’m begging for help.


I try this function -- from corona labs local function onLocalCollision( self, event )         if ( event.phase == "began" ) then           elseif ( event.phase == "ended" ) then                 audio.play(win)         end end

And as soon as the ball touches “anything” it plays the audio

I want to be able to tell the “ball”

– play the audio, ONLY if you touch this “item” or object


I also made the ball like this

ball = display.newImage ("ball.png")     group:insert ( ball )     ball.x = math.random(100, 945)     ball.y = -200     ball:scale (.2, .2)     ball.isVisible = false     ball.myName = "ball"     ball.collision = onLocalCollision     ball:addEventListener( "collision", ball )

and I though I made the second “crate” another object

only the one, I need to be detected by the ball

local rect14 = display.newRect(0, 0, 5, 1680)     group:insert ( rect14 )     rect14.x = 1025; rect14.y = 400     rect14:setFillColor(255)     rect14.myName = "rect"     physics.addBody( rect14, "static", { density = 1.0, friction = 0.9, bounce = 0.8 } )     rect14.collision = onLocalCollision     rect14:addEventListener( "collision", rect14 )

but the problem is the ball is “detecting” every single object

any help will be greatly appreciate it.

That’s my point, I didn’t “already know how to do it”… nobody did, that’s why the docs are there so you can learn how to do it.

(I’ve learned quite a bit over the past 20ish months, however I’m not very good at “teaching” others So hopefully the following will make sense)
Basically when it comes to collisions there are three phases

  • Began

  • Moved

  • Ended

This is very helpful for deciding when you want something to happen during a collision either with a specific object or any object.

Now, you say “play the audio, ONLY if you touch this “item” or object”

Okay so let’s think about this…

We have two objects - ‘ball’ and ‘rect14’ - and we want something to happen when the ball touches/collides with ‘rect14’.
Now in your own code you give both objects a .myName value, I’m assuming you know why and how it can be used.
Since we’ll want to check for collisions on our ‘ball’ we should add a few things to it -

ball.collision = onballCollision -- this is a reference to what will be the name of our collision function. ball:addEventListener( "collision", ball ) -- this adds a collision listener to the ball object

So we’ve got our objects, each with their own unique identifier and a listener for collisions.
Now we need a collision function.
 

This is what you have so far -
First thing is to change the name of the function to match the name we created earlier

​-- I assume you know what 'self' and 'event' in the parenthesis is for local function onballCollision( self, event )      if ( event.phase == "began" ) then        elseif ( event.phase == "ended" ) then              audio.play(win)     end end

You want this function to apply only when the ‘ball’ collides with ‘rect14’
So think about it… how can we get the name of each object? Well we gave each object their own identifier, so let’s use that!..
Oh, but where do we put it? Well if we want something to happen on the ended phase then it would make sense to add something to that ‘if statement’… but again, how?

Back to the docs!

local function onCollision( event ) if ( event.phase == "began" ) then print( "began: " .. event.object1.myName .. " & " .. event.object2.myName ) elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.myName .. " & " .. event.object2.myName ) end end

Well this is printing something in the terminal when the event begins and ends…
Back to the docs again (read event.object1 and event.object2 )
Oh so it’s printing the name of the first and second object involved in the collision!.. maybe I can reference what object I want to be the first and second, in addition to whatever event.phase I’m checking for.
(Notice they are using .myName for identifying each object… maybe you could do the same )

Now I don’t want to just type the code you need so you can capo&paste it, no, I gave you some information so you can understand how it works so later on when you want to do more complex collision detection you’ll understand some of the basics of what’s going on.

-Saer

Thanks Saer

I changed the code. I added the .myName

print( "began: " .. event.ball.myName .. " & " .. event.rect14.myName )

it gives me error…

I know that you want to help me learn.

thank you for that.

But if I see the code…

I copy and paste in my app.

and it works…

then I know that — that code works

then I can study and read, line by line

and think about it a lot

until it make sense to me brain.

the little bit of code I know, that’s how I learned it.

but if I just try to figure that out

honestly it is very difficult…

I might get it— after several months

but if you help me

and write the code that works

I will learn faster.

I know that you have no obligation to help me

you don’t even know me.

But in the name of my family, and me

I would really be very honored if you share your knowledge with me.

I’m using it to make nice apps for kids, and teach a little bit to them.

hoping that they will be better in the future, by learning something positive.

Thanks for understanding

and I hope you do help me solve this problem.

Could you post the error?

Also, you do know that line is nothing more than a print statement right?

Well it’s one thing to have some code that you can past in and figure out “okay if I use this line here it will do this” etc… etc… but it’s a completely different thing to understand why  something works.

For example, this
I would have never been able to pick apart&adapt certain portions of horacebury’s code to my own project if I didn’t first understand how&why certain things work.

Another example:
print( "began: " … event.object1.myName … " & " … event.object2.myName )
Yes, you might be able to recognize “this print the names of both objects in the terminal” but do you understand how it does that? And how&why that syntax does what it does?

My whole point is that it takes time to learn all this.
Unfortunately I think Corona’s motto “Anyone can code” gives some people a false sense of what is really involved in learning to program a mobile application.
Too many people (generally with no prior experience) approach mobile app development with this unrealistic mindset where they think they can have a fully functional, content rich, bug free application within a couple weeks. Then upon realizing that there is quite a bit more to it, they give up. – not directed at you, just some thoughts I’ve had for a while… that I decided to vent. :wink:

-Saer

Edit: I think you’re getting this line confused: 

print( "began: " .. event.object1.myName .. " & " .. event.object2.myName )

 object1 does not mean you should put the name of your object there, it means (as the docs state)
“Reference to the first object in the collision.”
Since you passed ‘self’ then whatever object you have calling the function will automatically be designated as ‘self’. If you have ‘ball’ calling your collision function, ball will = self and the other objects will = event.other

So you could do it this way ‘event.self.myName’ and 'event.other.myName’
What that will do is print the target object, in this case ‘ball’ is calling the function so it gets to be the target/self, and it will also print the second object - event.other (event - the collision. .other - the other object. event.other.myName - finds the other object’s .myName and prints it)
 

I think you are right Saer.

I give up.

I guess I’m not ready to detect collision at this point.

I will be doing something else. until I learn.

And yes! you are right – not everyone can learn how to program an app.

Thank you very much for all your help.

I tough it was going to be easier (the collision detection thing)

but I guess it’s only for a few privileged ones.

Thanks

I’m not saying you should give up at all.
All the information you need is in the docs, and what I posted should be very helpful.

Basically, if you’re trying to check collisions for a specific object on the ‘ended’ phase then just specify which object you’re checking for.
You already have ‘ball’ as ‘self’ so now the only thing you need to check is if ‘rect14’ is the other object the ball is colliding with, and if so then do something.

(again I’m not that great at explaining things, but hopefully this will help)
If  event.other.myName  gets the .myName of the other object involved in the collision, then ask yourself "what is the other object I’m checking for? ‘rect14’ okay, now how would I reference that object? Well, I know I have a >> ** _.myName ** <<  _assigned to my object, and if  event.other.myName  is checking the .myName of the other object then I guess I should pass in the .myName of my object so it can check whether or not the other object’s name is equal to my object’s name!  :smiley:

It’s quite simple if you break it down step by step and ask yourself “What should happen here?”

Anyone can code yes, said person just better be prepared for what is involved.
Like when trying to learn an instrument (I play jazz piano) it takes a while, and same goes for trying to learn programming.
“I guess it’s only for a few privileged ones.” Who’s “privileged”? Me? haha I’m just your average eighteen year old guy who, almost two years ago, decided that he was going to make a game."
I’m getting very close to finishing my first game. I could have released it a while ago, but I want it to be a polished/complete game that people will look at and say, hopefully, “wow, that’s a very well designed&built game.”

A job well done takes time my friend. :slight_smile:

-Saer

 

Don’t give up. I Will write for you something in the while.

Gonna be longer

First, I would like to talk about few basic matters.

In Lua (language you write in Corona) there is something like tables. Tables are special objects which has properties. But from the begining.

You create table like this

local myObject = {}

This created table which I called “myObject”. As you see, to create table you just type “= {}”.

I called it myObject because I want you to think about tables as objects.

So if tables are like objects then it must have some properties.

Let’s assume that myObject is a cat :slight_smile: If it’s cat then it has color, age, color of eyes, number of teeth and so on :slight_smile:

In lua to access property of table you use “.nameOfProperty”. So if we want to know color of our cat we would write

print( myObject.color )

and for age

print( myObject.age )

But hey, we have “nil” twice. Why? If myObject is a cat then it must have color and age. However our cat is empty (we used ‘{}’ to create empty table). If we want it to have color and age we must give him this properties. To do so we write

myObject.color = "brown" myObject.age = 5

And now it will have color and age.

This is full code:

local myObject = {} print( myObject.age ) print( myObject.color ) myObject.color = "brown" myObject.age = 5 print( myObject.age ) print( myObject.color )

Event listeners

You have encountered this.

Event listeners are functions which listen to specific events and perform some action if it happens.

Such events can be touch, tap or collision.

Why event’s listeners? Instead of checking each time if there was touch, tap or collision yourself, you just say Corona to check it for you - so if for exemple there will be touch, Corona will execute some sort of action you sepecified.

Events listeners can be for object or Runtime. What’s the difference? Let’s take touch for example. If you addEventListener for touch for object, then just this object will react on touch. If you create it for Runtime then all touches on screen will be detected.

Do you remember 

local function onLocalCollision( self, event ) -- some code here end

This is function which you said corona to execute when “collision” event is detected. It has 2 parameters. The important one is “event”. This is table. As I wrote earlier table have properties. And each type like “touch”, “tap”, “collision” have this table with different parameters.

For example touch, tap and collision have event table with

event.phase

but only “collision” has

event.other

For what each table have you must see docs.

Some comments on your posts

local function sound ()
    if ball.y > 500 then
        audio.play(win)
    end
end
Runtime:addEventListener(“enterFrame”, sound )

 

The problem is that once the ball reaches 500

the audio plays, and plays and plays and keeps playing forever

because of the Runtime

 

How do I solve that.

 

I just want the function to execute once.

You use Runtime listener for enterFrame event, it means that all the time sound() will be executed. So if your ball.y is 501 then sound() will be called. If ball.y = 502 too. It will happen all the time if ball.y > 500 (and probably 30 times per second).

I just want the function to execute once.

So think, how would you do it? If you want something to happen once then… you must remember it happened once! So if it will happen once again then you do not do it again.

For example

local hasSoundPlayed = false local function sound () if ball.y \> 500 then if hasSoundPlayed == false then audio.play(win) hasSoundPlayed = true end end end Runtime:addEventListener("enterFrame", sound )