Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Collision Detection on event "ended"
Started by veracode Jan 15 2020 11:05 AM

1 reply to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 39 posts
  • Corona SDK

i am attempting to detect a collision based on the end of a touch and hold.

i use guide to detect the collision and plan to eventually add more guides to say too far, too close , and PERFECT!

 

but on the release of touch on the guide it doesn't do anything until i hit the reset button, only then does it say "TOO FAR"  the reset button is only temporary for game testing but seems to be the only way this plays out correctly. 

 

the physics body is made large on purpose to get this portion of the game working before refining it to work as intended. 

 

i attempted to add checkCollision to function movestick in the ended section but just get an error. 

 

 

 

here is my code, Thanks in advance

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

--seed number generator
math.randomseed( os.time( ))

local physics = require("physics")
physics.start()
physics.setGravity( 0, 0 )

physics.setDrawMode( "hybrid" )


local centerX = display.contentCenterX
local centerY = display.contentCenterY
local screenTop = display.screenOriginY
local screenLeft = display.screenOriginX
local bottomMarg = display.contentHeight - display.screenOriginY
local rightMarg = display.contentWidth - display.screenOriginX

local testText

local backGround = display.newImageRect( "Images/Background.png", display.contentWidth, display.contentHeight)
backGround.x = display.contentCenterX
backGround.y = display.contentCenterY

local stick = display.newImageRect("Images/stick.png" , 23 , 1083 )
stick.x = rightMarg - 85
stick.y = bottomMarg + 445
physics.addBody( stick, "dynamic", { isSensor=true } )
stick.myName = "stick"



local resetButton = display.newCircle( screenTop + 35, screenLeft + 35 , 30 )
physics.addBody( resetButton, "static" , {radius = 30 , isSensor = true})
resetButton.alpha = 0.5

local stickobject
stickobject = display.newImageRect( "Images/stickobject.png", 30 , 30 )
stickobject.x = rightMarg - 75
stickobject.y = bottomMarg -80
physics.addBody( stickobject, {radius = 15 , isSensor = true} )
stickobject.myName = "stickobject"

local someObject
someObject = display.newImageRect("Images/Object1.png", 150 , 500)
someObject.x = centerX + screenLeft - 60
someObject.y = bottomMarg - 315

local guide = display.newImageRect( "Images/Line.png", 1067 , 10)
guide.x = centerX
guide.y = screenTop + 50
guide.alpha = 1.0 --change to 0.0 to get invisiable collision line.
physics.addBody( guide, "dynamic", {isSensor = true} )
guide.myName = "Guide"


local ground = display.newImageRect( "Images/Ground.png", 900.6 , 151.1 )
ground.x = centerX
ground.y = bottomMarg - 40




--local function checkHeight(event)
--  if (event.phase == "ended") then
--end

local function movestick(event)
  if ( event.phase == "began" ) then
         -- Code executed when the button is touched
             stick:setLinearVelocity(0,- 100)  -- "event.target" is the touched object
     elseif ( event.phase == "ended" ) then
         -- Code executed when the touch lifts off the object
         stick:setLinearVelocity(0,0)

  end
  --if ( event.phase == "moved" ) then
  --  stick:setLinearVelocity(0,0)
     return true  -- Prevents tap/touch propagation to underlying objects

end


local function checkCollision(event) -- check where the stick stopped
  if (event.phase == "ended" ) then

    local obj1 = event.object1
    local obj2 = event.object2

    if ( (obj1.myname == "Guide" and obj2.myName == "stick") or (obj1.myName == "stick" and obj2.myName == "Guide"))
    then
      testText = display.newText( "TOO FAR", display.contentCenterX, display.contentCenterY, native.systemFont, 80)
      testText:setFillColor(0,0,0)

      stick:setLinearVelocity(0,0)
    end
  end
end


local function resetGame(event)
  if ( event.phase == "began" ) then
    stick.x = rightMarg - 85
    stick.y = bottomMarg + 445
    display.remove( testText )
  end
end


Runtime:addEventListener("collision" , checkCollision)
stickobject:addEventListener("touch", movestick)
resetButton:addEventListener("touch", resetGame)



[TOPIC: post.html]
#2

veracode

[GLOBAL: userInfoPane.html]
veracode
  • Enthusiast

  • 39 posts
  • Corona SDK

I adjusted the physics body to be the size i wanted and ran a test i then noticed once the physics body for

 

stick passes the physics body for guide then the message TOO FAR pops up. this is okay since i can work

 

with this but it wont resolve my issue for the next steps which would be added where the stick is supposed

 

to land once you remove your touch from the screen which is initially what i wanted. 




[topic_controls]
[/topic_controls]