Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How can i detect collision ? — Gideros Forum

How can i detect collision ?

edited February 2014 in General questions
Hi,

I am trying to make space fighting game :) So i made player space ship and i made enemy ships and player ship can throw laser... I have two main function. startPlane() is create the player ship and add events when player touch the screen, ship throw laser. startEnemies() is create the enemies and start to enemy movement.

When player touch the screen, event start the fire() function. And fire function create the fire object and start the moveFire function. moveFire function is add -y for move the fire. Now, how can i detect when laser hit the enemy ?

Comments

  • Hi,
    I'd say to look for TNTcollision Engine to check points/shapes collisions without physics (it's a "plugin" for gideros).

    If you like to engage with physics/gravity and forces you can try box2d (already available in gideros).

  • HarrisonHarrison Member
    edited February 2014
    this looks like what you are looking for
    i didnt make this. It's @WauloK 's
    http://bluebilby.com/2013/05/08/gideros-mobile-tutorial-creating-your-first-game/
    check out :hitTestPoint
    _________________________________________________________________________________
    local function goTouch(gameOverImage, event)
    -- See if the Game Over object was touched
    if gameOverImage:hitTestPoint(event.touch.x, event.touch.y) then
    gameoverImg:removeEventListener(Event.TOUCHES_END, goTouch)
    -- Clean up our objects
    stage:removeChild(gameoverImg)
    gameoverImg=nil
    local i
    for i = 1,MAXNUMBEROFENEMIES do
    stage:removeChild(enemyShape[i])
    enemyShape[i]=nil
    end
    stage:removeChild(player)
    player=nil
    stage:removeChild(hiScoreText)
    hiScoreText=nil
    stage:removeChild(scoreText)
    scoreText=nil
    -- Restart the game
    startGame()
    end
    end
    “ The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ” - Tom Cargill
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    1) @pie mentioned TNT collision engine
    2) @Harrison mentioned hitTestPoint
    3) Using box2d physics engine
    3) using bounding box check:
    function Sprite:collidesWith(sprite2)
    	local x,y,w,h = self:getBounds(stage)
    	local x2,y2,w2,h2 = sprite2:getBounds(stage)
     
    	return not ((y+h < y2) or (y > y2+h2) or (x > x2+w2) or (x+w < x2))
    end
     
    --usage like
    if fire:collidesWith(enemy) then
        --enemy is hit
    end
  • Thanks for the answers :)
  • It would be cool to have a user defined bounding box, one that would be smaller than the actual sprite. Currently I'm using @ar2rsawseen's mentioned 4) Sprite:collidesWith. My hacky workaround is another bounding box sprite which is invisible =| .
  • What about rotated sprites?
Sign In or Register to comment.