Results 1 to 4 of 4

Thread: Collision Help

  
  1. #1
    Persian 117 is offline -Hacks Member
    Join Date
    Jan 2007
    Location
    Bowels of hell
    Posts
    272
    Rep Power
    13

    Default

    Could someone help me add collision detection? I'm totally confused :S

    Code:
    red = Color.new(225,0,0) 
    green = Color.new(0,225,0) 
    blue = Color.new(0,0,225) 
    white = Color.new(225,225,225)
    black = Color.new(0,0,0)
    
    Music.playFile("song.xm", true)
    
    player = Image.load("cursor.png")
    Player = { x = 120, y = 0, width = 28, height = 32 }
    
    star = Image.load("starfield.jpg")
    
    blockImage = Image.load("asteroid.png")
    block = { x = 452, y = 120, width = 65, height = 35 }
    
    function movePlayer()
    	pad = Controls.read()
    	if pad:right() then
    	Player.y = Player.y + 5
    	end
    	if pad:left() then
    	Player.y = Player.y - 5
    	end
    	if pad:down() then
    	Player.x = Player.x + 5
    	end
    	if pad:up() then
    	Player.x = Player.x - 5
    	end
    	if pad:up() and Player.x < 0 then
    	Player.x = 0
    	end
    	if pad:left() and Player.y < 0 then
    	Player.y = 0
    	end
    	width = 480 - player:width()
    	height = 272 - player:height()
    	if pad:right() and Player.y > width then
    	Player.y = width
    	end
    	if pad:down() and Player.x > height then
    	Player.x = height
    	end
    end
    
    function foes()
    block.x = block.x - 2
    
    if block.x < -65 then
    block.x = 452
    block.y = math.random(35,237)
    end
    end
    
    while true do 
    	if Controls.read():start() then break end
    	screen:clear()
    	movePlayer()
    	foes()
    	screen:blit(0,0,star)
    	screen:blit(block.x,block.y,blockImage)
    	screen:blit(Player.y,Player.x,player)
    	screen.waitVblankStart()
    	screen.flip()
    end


  2. #2
    Persian 117 is offline -Hacks Member
    Join Date
    Jan 2007
    Location
    Bowels of hell
    Posts
    272
    Rep Power
    13

    Default

    Am I close?

    Code:
    red = Color.new(225,0,0) 
    green = Color.new(0,225,0) 
    blue = Color.new(0,0,225) 
    white = Color.new(225,225,225)
    black = Color.new(0,0,0)
    
    Music.playFile("song.xm", true)
    
    player = Image.load("cursor.png")
    Player = { x = 120, y = 0, w = 28, h = 32 }
    
    star = Image.load("starfield.jpg")
    
    blockImage = Image.load("asteroid.png")
    block = { x = 452, y = 120, w = 65, h = 35 }
    
    function collision(ob1,ob2)
    	if (ob1.x + ob1.w > ob2.x) and (ob1.x < ob2.x + ob2.w) and (ob1.y + ob1.h > ob2.y) and (ob1.y < ob2.y + ob2.h) then
    		return true
    	else
    		return false
    	end
    end
    
    function movePlayer()
    	pad = Controls.read()
    	if pad:right() then
    	Player.y = Player.y + 5
    	end
    	if pad:left() then
    	Player.y = Player.y - 5
    	end
    	if pad:down() then
    	Player.x = Player.x + 5
    	end
    	if pad:up() then
    	Player.x = Player.x - 5
    	end
    	if pad:up() and Player.x < 0 then
    	Player.x = 0
    	end
    	if pad:left() and Player.y < 0 then
    	Player.y = 0
    	end
    	width = 480 - player:width()
    	height = 272 - player:height()
    	if pad:right() and Player.y > width then
    	Player.y = width
    	end
    	if pad:down() and Player.x > height then
    	Player.x = height
    	end
    end
    
    function foes()
    block.x = block.x - 2
    
    if block.x < -65 then
    block.x = 452
    block.y = math.random(35,237)
    end
    
    if collision(block,Player) then
    screen:print(65,80,"Collision!", white)
    end
    
    end
    
    while true do 
    	if Controls.read():start() then break end
    	screen:clear()
    	movePlayer()
    	foes()
    	screen:blit(0,0,star)
    	screen:blit(block.x,block.y,blockImage)
    	screen:blit(Player.y,Player.x,player)
    	screen.waitVblankStart()
    	screen.flip()
    end

  3. #3
    ST@TIC is offline -Hacks Newbie
    Join Date
    Nov 2006
    Location
    South Africa
    Posts
    39
    Rep Power
    13

    Default

    YA u are, i think
    (ob1.y + ob1.h > ob2.y)
    must be
    (ob1.y - ob1.h > ob2.y)

    \"Death be not proud, some have called thee mighty and great, thou art not so\"

  4. #4
    Persian 117 is offline -Hacks Member
    Join Date
    Jan 2007
    Location
    Bowels of hell
    Posts
    272
    Rep Power
    13

    Default

    it still doesn't work for me :(

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •