General Noob Collision Coding Help

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
xxcjn420xx
Prole
Posts: 8
Joined: Thu May 29, 2014 5:23 pm

General Noob Collision Coding Help

Post by xxcjn420xx »

To begin, my keyboard is far less than par. The G, H, ', and backspace keys do not work.

Now that that's out of the way, onto the issue.

My familiarity with Lua, and programming in general, are both still very early in development, though I'm doing the best that I can. I can't seem to figure out how to use return values such as true or false when they are given by a function, such as the CheckCollision functions within the code provided. Any help would be greatly appreciated. Thank you for your time.

Code: Select all

function love.load()
    f = love.graphics --Shortcut to love.graphics(my 'g' key is broken and does not work)
    px = -15 --The player's x-position
    py = 32 --The player's y-position
    pw = 32 --Player Width
    ph = 32 --Player height
    x1 = 32
    x2 = 468
    x3 = 468
    x4 = 403
    x5 = 105
    x6 = 105
    x7 = 568
    y1 = 345
    y2 = 240
    y3 = 0
    y4 = 35
    y5 = 0
    y6 = 447
    y7 = 410
    w1 = 105
    w2 = 37
    w3 = 37
    w4 = 65
    w5 = 37
    w6 = 105
    w7 = 37
    h1 = 37
    h2 = 105
    h3 = 105
    h4 = 37
    h5 = 105
    h6 = 37
    h7 = 105
    xCoord = 5
    yCoord = 5
    y = 0
    x = 0
end

function love.update(dt)
    x = math.floor(px)
    y = math.floor(py)
    if (px >= -16) and (px <= 32) then  --Makes the player come into the screen from the left border
        px = px + 50*dt
    end
    if px == 32 then --Stops the player at a specific location
        px = 32
    end
    if love.keyboard.isDown("down") then
        py = py + 100*dt
        if py >= 536 then
            py = 536
        end
    end
    if love.keyboard.isDown("up") then
        py = py - 100*dt
        if py <= 32 then
            py = 32
        end
    end
    if love.keyboard.isDown("right") then
        px = px + 100*dt
        if (px >= 736) and (py >= 64) then
            px = 736
        elseif (px >= 736) and (py == 64) then
            px = px
        end
    end
    if love.keyboard.isDown("left") then
        px = px - 100*dt
        if px <= 32 then
            px = 32
        end
    end      
    if CheckCollision1 == true then
        px = x1+w1
        x1 = px+pw
        py = y1+h1
        y1 = py+ph
    end
    if CheckCollision2 == true then
        px = x2+w2
        x2 = px+pw
        py = y2+h2
        y2 = py+ph
    end
    if CheckCollision3 == true then
        px = x3+w3
        x3 = px+pw
        py = y3+h3
        y3 = py+ph
    end
    if CheckCollision4 == true then
        px = x4+w4
        x4 = px+pw
        py = y4+h4
        y4 = py+ph
    end
    if CheckCollision5 == true then
        px = x5+w5
        x5 = px+pw
        py = y5+h5
        y5 = py+ph
    end
    if CheckCollision6 == true then
        px = x6+w6
        x6 = px+pw
        py = y6+h6
        y6 = py+ph
    end
    if CheckCollision7 == true then
        px = x7+w7
        x7 = px+pw
        py = y17h7
        y7 = py+ph
    end
end

function love.draw()
    f.setBackgroundColor(150,150,150,255)
    f.setColor(0,0,0,255)
    f.rectangle("fill",x1,y1,w1,h1)
    f.setColor(0,0,0,255)
    f.rectangle("fill",x2,y2,w2,h2)
    f.setColor(0,0,0,255)
    f.rectangle("fill",x3,y3,w3,h3)
    f.setColor(0,0,0,255)
    f.rectangle("fill",x4,y4,w4,h4)
    f.setColor(0,0,0,255)
    f.rectangle("fill",x5,y5,w5,h5)
    f.setColor(0,0,0,255)
    f.rectangle("fill",x6,y6,w6,h6)
    f.setColor(0,0,0,255)
    f.rectangle("fill",x7,y7,w7,h7)
    f.setColor(0,255,64,255)
    f.rectangle("fill",px,py,pw,ph)
    f.setColor( 255, 0, 255, 255 )
    local isTrue = ""
    f.print( "Player coordinates: ("..x..","..y..")", xCoord, yCoord )
end

function CheckCollision1(px,py,pw,ph, x1,y1,w1,h1)
    return 
        px < x1+w1 and
        x1 < px+pw and
        py < y1+h1 and
        y1 < py+ph
end

function CheckCollision2(px,py,pw,ph, x2,y2,w2,h2)
    return
        px < x2+w2 and
        x2 < px+pw and
        py < y2+h2 and
        y2 < py+ph
end

function CheckCollision3(px,py,pw,ph, x3,y3,w3,h3)
    return
        px < x3+w3 and
        x3 < px+pw and
        py < y3+h3 and
        y3 < py+ph
end

function CheckCollision4(px,py,pw,ph, x4,y4,w4,h4)
    return
        px < x4+w4 and
        x4 < px+pw and
        py < y4+h4 and
        y4 < py+ph
end

function CheckCollision5(px,py,pw,ph, x5,y5,w5,h5)
    return
        px < x5+w5 and
        x5 < px+pw and
        py < y5+h5 and
        y5 < py+ph
end

function CheckCollision6(px,py,pw,ph, x6,y6,w6,h6)
    return
        px < x6+w6 and
        x6 < px+pw and
        py < y6+h6 and
        y6 < py+ph
end

function CheckCollision7(px,py,pw,ph, x7,y7,w7,h7)
    return
        px < x7+w7 and
        x7 < px+pw and
        py < y7+h7 and
        y7 < py+ph
end

function love.focus(bool)
    if bool == false then --Quits the program if the mouse clicks outside of the screen
        love.event.quit()
    end
end    
Attachments
test.love
(1.45 KiB) Downloaded 121 times
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: General Noob Collision Coding Help

Post by micha »

Hi,

first let me give you some general advice for your code:
I highly suggest using tables for representing multiple rectangles. At the moment, you have a separate variable for each rectangle. That means that you have to copy and paste the code for collision checking for each rectangle. So instead of having all these variables:

Code: Select all

x1 = 32
    x2 = 468
    x3 = 468
    x4 = 403
    x5 = 105
    x6 = 105
    x7 = 568
better use a table:

Code: Select all

x = {32,468,468,403,105,105,568}
If the meaning of this is not clear to you, have a look at some Lua tutorials about tables.

Another small comment. This code:

Code: Select all

    if px == 32 then --Stops the player at a specific location
        px = 32
    end
Has no effect. It probably does not do, what you initially intended.

Now for your question about collision. Collision usually consists of two parts: detect collisions and resolve collisions. You got the first part right, from what I can tell. Now, how to resolve collision? This is what you have so far:

Code: Select all

px = x1+w1
x1 = px+pw
py = y1+h1
y1 = py+ph
What it does is set the player to the right end of the rectangle and then set the rectangle to the right end of the player. Then the same for the vertical axis. To improve the code, first answer this question: When the player collides with a box, should then the box move or should only the player move or both? The easiest would be to only move the player. In that case you would have to find out, if the player was previously moving left or right/up or down and then move the player back to where he came from until the collision is resolved.

I know this explanation is a bit vague. At least I hope it explains, why your code does not work as expected. Please ask, if anything is unclear.
xxcjn420xx
Prole
Posts: 8
Joined: Thu May 29, 2014 5:23 pm

Re: General Noob Collision Coding Help

Post by xxcjn420xx »

I'm very confused, I apologize for the inconvenience.

I still can't seem to figure out how to use return values such as true or false when they are given by a function, such as the CheckCollision functions.

To rephrase this, how could I tell it to do things if the CheckCollision function returns true?

Hopefully this makes sense. :death:


Edit: Also, I already declared x and y as 0 for the code that displays the player coordinates at the top left of the screen, so how could I declare x and y tables without them being declared twice? Unless it already wouldn't be declared twice, which I don't think is the case.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: General Noob Collision Coding Help

Post by micha »

Sorry for not answering your original question in the other post. Here is the answer:
If you use the name of a function without parenthesis "()", then the function is not called. So doing this:

Code: Select all

if CheckCollision7 == true then

checks if the content of variable CheckCollision7 is equal to the value true. Since the content is a function, it is not equal to true. To actually call the function you have to add the parantheses and pass parameters:

Code: Select all

if CheckCollision(px,py,pw,ph, x2,y2,w2,h2) then
end
You can leave away the "== true" because if something is true, then these two things do the same:

Code: Select all

if a then
end
if a == true then
end
xxcjn420xx
Prole
Posts: 8
Joined: Thu May 29, 2014 5:23 pm

Re: General Noob Collision Coding Help

Post by xxcjn420xx »

Thank you very much, that was easily understandable and I feel slightly less intelligent for not figuring that out.

Thank you again. :)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests