Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Can't keep objects whithin screen
Started by alec.ribeiro Dec 05 2018 08:17 AM

4 replies to this topic
objects screen objects whithin screen inside the screen proper collision collision problems with collision detec collision detection

Best Answer roaminggamer , 05 December 2018 - 11:31 AM

Hi.  Manually adjusting the x and y position of an object will conflict with the physics system and give you un-specified results. 

 

That is, you can force the 'ship' off the edge of the screen by directly modifying the x position and no physics 'blocker' will stop it.

 

So, instead do this:

 

1. Pre-calculate the min and max X positions corresponding to the left-most and right-most position you want the ship to move to .

 

2. In your movement code, check if the ship is too far left or too far right and if so, move it back inside the min,max bounds.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

alec.ribeiro

[GLOBAL: userInfoPane.html]
alec.ribeiro
  • Observer

  • 2 posts
  • Corona SDK

So I've been trying to make a game similar to asteroids following Corona's tutorial and then modifying, adding features etc. For some reason I can't keep my ship 100% inside the screen.I've tried creating invisible walls and adding bouncing to them, and I've tried adding them to a collision function(the ship seems to detect collision but it doesn't do anything). Here's my code:(outside of any composer's scenes)

local function onCollision(event)
 
    if event.phase == 'began' then
 
        local obj1 = event.object1
        local obj2 = event.object2
        
        if obj1.myName == 'laser' and obj2.myName == 'asteroid' or
             obj1.myName == 'asteroid' and obj2.myName == 'laser'
        then
            -- Remove both the laser and asteroid
            display.remove(obj1)
            display.remove(obj2)
 
            for i = #asteroidsTable, 1, -1 do
                if asteroidsTable[i] == obj1 or asteroidsTable[i] == obj2 then
                    table.remove(asteroidsTable, i)
                    break
                end
            end
 
            -- Increase score
            score = score + 100
            scoreText.text = 'Score: ' .. score
 
        elseif obj1.myName == 'ship' and obj2.myName == 'asteroid' or
                 obj1.myName == 'asteroid' and obj2.myName == 'ship'
        then
          audio.play(hitSound, {channel = 2})-- Ship gets hit
          displayRandom_explosions()
            if died == false then
                died = true
 
                -- Update lives
                lives = lives - 1
                livesText.text = 'Lives: ' .. lives
 
                if lives == 0 then
                    display.remove(ship)
                    timer.performWithDelay(2000, endGame)
                else
                    ship.alpha = 0
                    timer.performWithDelay(1000, restoreShip)
                end
            end
        elseif obj1.myName == 'ship' and obj2.myName == 'leftWall' or obj1.myName == 'leftWall' and obj2.myName == 'ship' then
          ship.x = 50
        end
    end
end

and:(in composer's scene:create(event))

  local leftWall = display.newRect(backGroup, 20, display.contentHeight-100, 10, 10)--invisible wall 1
  local rightWall = display.newRect(backGroup, display.contentWidth-20, display.contentHeight-100, 10, 10)--invisible wall 2
  physics.addBody(leftWall, 'static', {bounce = 0.1})
  physics.addBody(rightWall, 'static', {bounce = 0.1})
  leftWall.myName = 'leftWall'
  rightWall.myName = 'rightWall'
  
  local background = display.newImageRect(backGroup, 'background.png', 800, 1400)
  background.x = display.contentCenterX
  background.y = display.contentCenterY

  ship = display.newImageRect(mainGroup, objectSheet, 4, 98, 79)
  ship.x = display.contentCenterX
  ship.y = display.contentHeight - 100
  physics.addBody(ship, {radius=30, isSensor=true})
  ship.myName = 'ship'

THANK YOU SO MUCH FOR YOUR TIME  :D  :D  ;)  ;)



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,380 posts
  • Corona SDK

  Best Answer

Hi.  Manually adjusting the x and y position of an object will conflict with the physics system and give you un-specified results. 

 

That is, you can force the 'ship' off the edge of the screen by directly modifying the x position and no physics 'blocker' will stop it.

 

So, instead do this:

 

1. Pre-calculate the min and max X positions corresponding to the left-most and right-most position you want the ship to move to .

 

2. In your movement code, check if the ship is too far left or too far right and if so, move it back inside the min,max bounds.



[TOPIC: post.html]
#3

alec.ribeiro

[GLOBAL: userInfoPane.html]
alec.ribeiro
  • Observer

  • 2 posts
  • Corona SDK

Perfect! Thank you! I added a test to see if the ship is inside the screen to both sides inside the movement of the ship('ended').



[TOPIC: post.html]
#4

thanhman1305

[GLOBAL: userInfoPane.html]
thanhman1305
  • Observer

  • 1 posts
  • Corona SDK

you can't go off camera; Keeping a rigidbody within the screen bounds?



[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,380 posts
  • Corona SDK

you can't go off camera; Keeping a rigidbody within the screen bounds?

 

Not sure I understand your question.

 

You can move a body off screen even if it is 'blocked'.  Why? How?  Because manually updating a position bypasses the physics system.

 

If you're moving a body with forces all is well, but again manually setting <x,y> will cause unexpected behaviors.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: objects, screen, objects whithin screen, inside the screen, proper collision, collision, problems with collision detec, collision detection