Player collision (with other player)???

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
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Player collision (with other player)???

Post by iPoisonxL »

I'm making a basic game to amuse my friends. I haven't called it anything yet, but it's basically a square chasing another square. The red square is AI, and the white square is player-controlled. I've got the collision all figured out on that, and it works perfectly.

I decided to make a 2 player version of this game. So I just copied the files over to another folder and transformed it into a 2 player game. Everything works fine, except the collision. I have the exact same collision as the singleplayer version, but for some reason, the collision doesn't always work. It's glitchy, and sometimes it only counts if they touched the player's right side, or sometimes it's the left side. There's no determining what happens.

Anyway, here's my death checking code. I call it in the main.lua, in love.update().

Code: Select all

death.check=function()
    for _,enemy in ipairs(enemy) do
        for _,player in ipairs(player) do
            if ((enemy.x+enemy.w>player.x and enemy.x+enemy.w<(player.x+player.w)) and
            (enemy.y+enemy.h>player.y and enemy.y+enemy.h<(player.y+player.h))) then
                death.state=true
            end
        end
    end
end
player.love
(2.79 KiB) Downloaded 85 times
NOTE: Control the player with arrow keys, and control the enemy with WASD.

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Player collision (with other player)???

Post by micha »

I believe it should be like this (untested)

Code: Select all

death.check=function()
    for _,enemy in ipairs(enemy) do
        for _,player in ipairs(player) do
            if enemy.x+enemy.w>player.x and enemy.x<player.x+player.w and
            enemy.y+enemy.h>player.y and enemy.y<player.y+player.h then
                death.state=true
            end
        end
    end
end
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Player collision (with other player)???

Post by iPoisonxL »

micha wrote:I believe it should be like this (untested)

Code: Select all

death.check=function()
    for _,enemy in ipairs(enemy) do
        for _,player in ipairs(player) do
            if enemy.x+enemy.w>player.x and enemy.x<player.x+player.w and
            enemy.y+enemy.h>player.y and enemy.y<player.y+player.h then
                death.state=true
            end
        end
    end
end
Holy crap, yes thank you so much. Can you explain me what I was doing wrong? :o

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Player collision (with other player)???

Post by micha »

Compare my code and yours in detail (but ignore the bracket I removed, they do not change anything). It also helps if you draw (on paper) two boxes and try to formulate, under which conditions they collide. If you have problems figuring this out, ask again and I try to explain in more detail.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Player collision (with other player)???

Post by davisdude »

I know that this is supposed to be a simple game, but have you investigated using for loops? They could help you out in harder codes later on...

shoot.lua

Code: Select all

player = {}
num_of_players = 0

function player:draw()
    for i = 0, #player do
        love.graphics.rectangle( "fill", player[i].x, player[i].y, player[i].w, player[i].h )
    end
end

function player:update(dt)
    for i = 1, #player do
        if i == 1 then
            if love.keyboard.isDown('w') then
                player[i].y = player[i].y - 200 * dt
            end
            --rest of code using wasd controls
         elseif i == 2 then
            if love.keyboard.isDown('up') then
                player[i].y = player[i].y - 200 * dt
            end
            --rest of code using arrow keys
            --need others if more than two players
         end
    end
    player:collide(dt)
end

function player:load( x, y, w, h )
    num_of_players= num_of_players + 1
    player[length] = {}
    player[length].x = x
    player[length].y = y
    player[length].w = w
    player[length].h = h
end

function player:collide(dt)
    if player[1].x + player[1].w > player[2].x and
    player[1].y + player[1].h > player[2].y and
    player[1].x < player[2].x + player[2].w and
    player[1].y < player[2].y + player[2].h then
    --do damage or whatever
    end
end
main.lua

Code: Select all

function love.load()
    player:load( whatever_x, whatever_y, whatever_w, whatever_h )
    player:load( other_x, other_y, other_w, other_h )
end

function love.draw()
    player:draw()
end

function love.update(dt)
    player:update(dt)
end
Note that this code was made on the fly (at school), so I haven't tested it yet.
If you have any questions, just ask! :awesome:
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Player collision (with other player)???

Post by iPoisonxL »

davisdude wrote:I know that this is supposed to be a simple game, but have you investigated using for loops? They could help you out in harder codes later on...

shoot.lua

Code: Select all

player = {}
num_of_players = 0

function player:draw()
    for i = 0, #player do
        love.graphics.rectangle( "fill", player[i].x, player[i].y, player[i].w, player[i].h )
    end
end

function player:update(dt)
    for i = 1, #player do
        if i == 1 then
            if love.keyboard.isDown('w') then
                player[i].y = player[i].y - 200 * dt
            end
            --rest of code using wasd controls
         elseif i == 2 then
            if love.keyboard.isDown('up') then
                player[i].y = player[i].y - 200 * dt
            end
            --rest of code using arrow keys
            --need others if more than two players
         end
    end
    player:collide(dt)
end

function player:load( x, y, w, h )
    num_of_players= num_of_players + 1
    player[length] = {}
    player[length].x = x
    player[length].y = y
    player[length].w = w
    player[length].h = h
end

function player:collide(dt)
    if player[1].x + player[1].w > player[2].x and
    player[1].y + player[1].h > player[2].y and
    player[1].x < player[2].x + player[2].w and
    player[1].y < player[2].y + player[2].h then
    --do damage or whatever
    end
end
main.lua

Code: Select all

function love.load()
    player:load( whatever_x, whatever_y, whatever_w, whatever_h )
    player:load( other_x, other_y, other_w, other_h )
end

function love.draw()
    player:draw()
end

function love.update(dt)
    player:update(dt)
end
Note that this code was made on the fly (at school), so I haven't tested it yet.
If you have any questions, just ask! :awesome:
wait a second, can you explain to me what you did there? I'm not very advanced in lua, and I've forgotten quite a lot of things. I've been doing a lot of java recently.

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Player collision (with other player)???

Post by davisdude »

2 questions:
1) Wow, did that actually work?
2) I used a for loop, by making a table/array/whateveryouwanttocallit for each player. This stores each player's individual x and y positions and should update it when they move.
By the way, #[insert name here] gives the length of the table.
In the player:load it adds a new item to the table every time it's called. It starts with a length of zero, and adds one each time it's called. It then assigns aspects for each one.
Let me know if you need anything else! :awesome:
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
iPoisonxL
Party member
Posts: 227
Joined: Wed Feb 06, 2013 3:53 am
Location: Australia
Contact:

Re: Player collision (with other player)???

Post by iPoisonxL »

davisdude wrote:2 questions:
1) Wow, did that actually work?
2) I used a for loop, by making a table/array/whateveryouwanttocallit for each player. This stores each player's individual x and y positions and should update it when they move.
By the way, #[insert name here] gives the length of the table.
In the player:load it adds a new item to the table every time it's called. It starts with a length of zero, and adds one each time it's called. It then assigns aspects for each one.
Let me know if you need anything else! :awesome:
second "question" isn't a question. haha.
but I have no idea if it worked, I didn't try it. I'm planning on keeping code that only I can write/understand. For loops in lua still confuse me, even if they shouldn't. It's like that unexplored place in a playground where you reaaaally want to go but you can't.

Code: Select all

      L
    L Ö
    Ö V
L Ö V E
Ö B E
V E
E Y
Website
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Player collision (with other player)???

Post by davisdude »

This website has probably everything you'll never want to know about lua: http://www.lua.org/manual/5.1/manual.html
It's pretty huge, so press ctrl and f then search for for. They're pretty much the only data type that lua has, so I could suggest getting nice and smiley with them for future reference. :awesome:
Happy Coding! :awesome:
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests