Question about collisions.

I am trying to  make it so that when a figure in my game touches the ground it instantly goes to my restart.lua file.

I have looked at some tutorials on collision events and I saw that it said without post and pre collision listeners the event wont happen . How do I make it so that nothing happens on pre or post collision . Also should the collision event where it go’s to restart.lua be post collision or am I alright .
Thank you for your help . :slight_smile:

@true,

You can use a standard collision listener (table or function style).  For example this function listener would do the job.

-- May contain typos -- local circle = display.newCircle( 10,10,10) -- my pseudo-code example for your player circle.isPlayer = true local ground = display.newRect( ... ) -- my pseudo-code example for your ground ground.isGround = true -- add physics bodies, etc. local function onCollision( event ) if( event.phase == "began" and event.target.isPlayer and event.other.isGround ) then timer.performWithDelay( 30, function() composer.openScene( "restart.lua" ) end ) end return true end circle:addEventListener( circle, onCollision )

While there are better ways to do this, this will work.

So when im putting different listeners for post and pre put

circle:addEventListener( circle, preCollision ) -- or I can I put circle:addEventListener( circle, postCollision ) -- just change the prefix right ?

As you can see in the code I asked if I want to change it to pre and post collision just change the prefix .

Also how do I make it so that nothing happens on pre and post collision ?

Thank you.

Hmmm… You asked how to have an object touch another object and change scenes.  Then you mixed in some questions about pre and post collision, neither of which I think is needed for this.

Sorry, but I rarely use those listeners and really don’t think you need to for this.  I think the basic answer I gave should work.

Are you asking a question about collision response?  You used the phrase ‘so that nothing happens pre and post collision’.  I didn’t get that as it was a little vague.

Let me clarify some details about collisions…

Basic Concepts

  • Collision Detection - This is the code associated with detecting that a collision has occurred and making a decision(s) about what to do.
  • Collision Response - This is the bouncing, and sliding, pushing, and all the physics stuff you see happening.

Specific Collision Detection Events

  • preCollision - Fires just before a collision will occur.  It is used to modify future collision responses or to take actions prior to said responses.
  • collision - This is your traditional collision detection logic and encompasses the exact beginning and end of the collision.
  • postCollision - Fires just after the collision has ended.  Like preCollision, it is used to allow collision response changes and to handle actions that should not occur till the collision is fully completed.

Modifying Collision Responses

This is a broad topic, but it boils down to things like:

  • modifying physics related object body fields like: isBodyActive, gravityScale, etc.
  • Removing and adding bodies
  • Modifying body quantities like velocity via method calls to setLinearVelocity(), and other physics methods.
  • Pausing, stopping, and restarting physics 

You are not allowed to do any of the above things while the ‘collision’ event is being processed, thus you often see people using timer.performWithDelay( … ) to make changes.

If you read this, and follow up by reading the physics guides (at top of this page under ‘See Also’ http://docs.coronalabs.com/daily/api/library/physics/index.html), all of what I said will become much clearer.

What im saying is that in your response you only give me a listener function for “onCollision” i have seen that without pre and post collision listeners the event wont fire and in my case that event is going to restart. Lua .and i want to make it so that before the collision and after the collision no event takes place . The only time i want something to happen is onCollision and that something is going to restart. Lua instantly. Is what im trying to say clarified?If so please answer.
Thank you.

as I said, you dont need to use pre or post collision I said that you only need collision. the fact that it isn’t working for you indicates ao problem in your code not in the corona events.  I understand you want to restart and only when the collision happens.  There is probably an issue with your code not related to the collision portion. you should write a small test bench that only drops and object onto another object and then triggers a scene change.  If you use a variation of my code it will work.  Please just forget about pre and post collision for now and focus on why you’re not getting a collisoin event.  Find this problem and you’ll solve the issue.

Note: I don’t normally write such terrible and hard to read answers, but I wanted to make a point.  You’re posts are always so hard to read.  It is frustrating and confusing.

Please:

  • format,
  • capitalize
  • use punctuation marks 
  • correct spelling errors
  • use paragraphs to break up topics
  • and try a bullet list every once in a while for lists
  • Oh, after posting, read your post then re-edit to fix mistakes.

Again.  The solution I gave is the right one for the issue you seemed to describe.  i.e. Object A hits object B and this results in a call to composer in order to change the scene.  No need for pre- or post- collision code.  A basic collision() handler will do the trick.

** FIXED TYPO **

Note: If you want to prevent further calls to collision you can do this:

local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isPlayer and other.isGround ) then timer.performWithDelay( 30, function() composer.openScene( "restart.lua" ) end ) self:removeEventListener( "collision") --- REMOVE EVENT LISTENER NOW; STOP FUTURE CALLS TO ME end return true end circle.collision = onCollision circle:addEventListener( "collision" )

@true,

By the way.  I’m not trying to be mean to you, or to shame you when I ask that you work on formatting your posts better.

I am doing it because:

  • I enjoy helping answer questions, but not when I have to struggle through a hard to read post. 
  • I want you to get the answer you need, and I know that when one spends the time to ask a question well, the odds of that go up dramatically.
    • That is, lazy questions get lazy answers, or just as bad no answers.
    • There is also a hidden benefit to spending significant time on your question.  For me, doing so often triggers a thought that leads to a solution. i.e. I self-solve by spending the time and effort to format my questions to be clear enough for others to understand them.

So, best of luck to you on your project.  I’ll do my best to answer future questions.  I hope this one is resolved or getting close to being resolved.

thank you for your help

@true,

You can use a standard collision listener (table or function style).  For example this function listener would do the job.

-- May contain typos -- local circle = display.newCircle( 10,10,10) -- my pseudo-code example for your player circle.isPlayer = true local ground = display.newRect( ... ) -- my pseudo-code example for your ground ground.isGround = true -- add physics bodies, etc. local function onCollision( event ) if( event.phase == "began" and event.target.isPlayer and event.other.isGround ) then timer.performWithDelay( 30, function() composer.openScene( "restart.lua" ) end ) end return true end circle:addEventListener( circle, onCollision )

While there are better ways to do this, this will work.

So when im putting different listeners for post and pre put

circle:addEventListener( circle, preCollision ) -- or I can I put circle:addEventListener( circle, postCollision ) -- just change the prefix right ?

As you can see in the code I asked if I want to change it to pre and post collision just change the prefix .

Also how do I make it so that nothing happens on pre and post collision ?

Thank you.

Hmmm… You asked how to have an object touch another object and change scenes.  Then you mixed in some questions about pre and post collision, neither of which I think is needed for this.

Sorry, but I rarely use those listeners and really don’t think you need to for this.  I think the basic answer I gave should work.

Are you asking a question about collision response?  You used the phrase ‘so that nothing happens pre and post collision’.  I didn’t get that as it was a little vague.

Let me clarify some details about collisions…

Basic Concepts

  • Collision Detection - This is the code associated with detecting that a collision has occurred and making a decision(s) about what to do.
  • Collision Response - This is the bouncing, and sliding, pushing, and all the physics stuff you see happening.

Specific Collision Detection Events

  • preCollision - Fires just before a collision will occur.  It is used to modify future collision responses or to take actions prior to said responses.
  • collision - This is your traditional collision detection logic and encompasses the exact beginning and end of the collision.
  • postCollision - Fires just after the collision has ended.  Like preCollision, it is used to allow collision response changes and to handle actions that should not occur till the collision is fully completed.

Modifying Collision Responses

This is a broad topic, but it boils down to things like:

  • modifying physics related object body fields like: isBodyActive, gravityScale, etc.
  • Removing and adding bodies
  • Modifying body quantities like velocity via method calls to setLinearVelocity(), and other physics methods.
  • Pausing, stopping, and restarting physics 

You are not allowed to do any of the above things while the ‘collision’ event is being processed, thus you often see people using timer.performWithDelay( … ) to make changes.

If you read this, and follow up by reading the physics guides (at top of this page under ‘See Also’ http://docs.coronalabs.com/daily/api/library/physics/index.html), all of what I said will become much clearer.

What im saying is that in your response you only give me a listener function for “onCollision” i have seen that without pre and post collision listeners the event wont fire and in my case that event is going to restart. Lua .and i want to make it so that before the collision and after the collision no event takes place . The only time i want something to happen is onCollision and that something is going to restart. Lua instantly. Is what im trying to say clarified?If so please answer.
Thank you.

as I said, you dont need to use pre or post collision I said that you only need collision. the fact that it isn’t working for you indicates ao problem in your code not in the corona events.  I understand you want to restart and only when the collision happens.  There is probably an issue with your code not related to the collision portion. you should write a small test bench that only drops and object onto another object and then triggers a scene change.  If you use a variation of my code it will work.  Please just forget about pre and post collision for now and focus on why you’re not getting a collisoin event.  Find this problem and you’ll solve the issue.

Note: I don’t normally write such terrible and hard to read answers, but I wanted to make a point.  You’re posts are always so hard to read.  It is frustrating and confusing.

Please:

  • format,
  • capitalize
  • use punctuation marks 
  • correct spelling errors
  • use paragraphs to break up topics
  • and try a bullet list every once in a while for lists
  • Oh, after posting, read your post then re-edit to fix mistakes.

Again.  The solution I gave is the right one for the issue you seemed to describe.  i.e. Object A hits object B and this results in a call to composer in order to change the scene.  No need for pre- or post- collision code.  A basic collision() handler will do the trick.

** FIXED TYPO **

Note: If you want to prevent further calls to collision you can do this:

local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isPlayer and other.isGround ) then timer.performWithDelay( 30, function() composer.openScene( "restart.lua" ) end ) self:removeEventListener( "collision") --- REMOVE EVENT LISTENER NOW; STOP FUTURE CALLS TO ME end return true end circle.collision = onCollision circle:addEventListener( "collision" )

@true,

By the way.  I’m not trying to be mean to you, or to shame you when I ask that you work on formatting your posts better.

I am doing it because:

  • I enjoy helping answer questions, but not when I have to struggle through a hard to read post. 
  • I want you to get the answer you need, and I know that when one spends the time to ask a question well, the odds of that go up dramatically.
    • That is, lazy questions get lazy answers, or just as bad no answers.
    • There is also a hidden benefit to spending significant time on your question.  For me, doing so often triggers a thought that leads to a solution. i.e. I self-solve by spending the time and effort to format my questions to be clear enough for others to understand them.

So, best of luck to you on your project.  I’ll do my best to answer future questions.  I hope this one is resolved or getting close to being resolved.

thank you for your help