grid based block interaction

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
lewislepton
Prole
Posts: 6
Joined: Sun May 03, 2015 12:12 pm

grid based block interaction

Post by lewislepton »

howdy folks,

just working on a new game, or more so working on just something to get back into LÖVE. but have been playing with grid based creation of maps. but having trouble getting the player to interact with my borders, or more so, my images for walls.

did use this lovely piece of info for doing grid based maps [ viewtopic.php?f=4&t=12639&p=75275&hilit ... els#p75275 ]. have been able to make borders, but only on the edges of the screen. but want the wallBlocks to have those borders
rather than post the love file, since it does spit out errors due to me working on it. ill just post the code that needs looking at, see if anyone can help me on it

thanks

main.lua

Code: Select all

require 'code/playerClass'
-- require 'code/blockCollide'
require 'code/mapClass'

function love.load()
	mapLoad()
	map = level1_map
end

function love.update(dt)
	require('lurker/lurker').update()
	playerUpdate(dt)
	mapUpdate()
end

function love.draw()
	mapDraw()
	playerDraw()

end
mapClass.lua

Code: Select all

block = {}
blockFloor = love.graphics.newImage('asset/blockFloor.png')
blockWall = love.graphics.newImage('asset/blockWall.png')
function mapLoad()
      level1_map = {
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
    }
end

function mapUpdate()
    if player.x < 0 then
        player.x = 0
    end
    if player.y < 0 then
        player.y = 0
    end
    if player.x + player.draw:getWidth() > blockWall then
        player.x = blockWall - player.draw:getWidth()
    end
    if player.y + player.draw:getHeight() > blockWall then
        player.y = blockWall - player.draw:getHeight()
    end
end

function mapDraw()
    for y=1, #map do
        for x=1, #map[y] do
            if map[y][x] == 1 then
                love.graphics.draw(blockWall, x*32, y*32)
            end
            if map[y][x] == 0 then
                love.graphics.draw(blockFloor, x*32, y*32)
            end
        end
    end
end
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: grid based block interaction

Post by ivan »

lewislepton wrote:but having trouble getting the player to interact with my borders, or more so, my images for walls.
Hello.
There are several questions that are relevant to your problem:
-Can the player be positioned between tiles or does he always stand on the center of a tile?
If your gameplay expects the player to always be located on ONE tile then the code becomes trivial. All you have to do is animate the player as he moves from one tile to another.
If you want to unconstrained movement, it gets tricky:

-How do you expect the player to react when he contacts a blocked tile?

If you just want to prevent the player from entering block tiles, it's not too hard:
1.you check for overlap between the player and the tiles
2.you move the player so there's no overlap
For example:

Code: Select all

    -- checks for a collision between rectangles "a" and "b"
    -- returns "nil" if there is no collision
    -- returns the separation vector (sx,sy) if there is a collision
    function checkcol(a, b)
      local x1,y1, w1,h1 = a.x,a.y, a.w/2, a.h/2
      local x2,y2, w2,h2 = b.x,b.y, b.w/2, b.h/2
      -- center points
      local c1x, c1y = x1 + w1, y1 + h1
      local c2x, c2y = x2 + w2, y2 + h2
      -- distance between centers
      local dx, dy = c2x - c1x, c2y - c1y
      local dxa, dya = math.abs(dx), math.abs(dy)
      -- overlap check
      local sw, sh = w1 + w2, h1 + h2
      if dxa >= sw or dya >= sh then return end
      -- separation (positive)
      local sx, sy = sw - dxa, sh - dya
      -- ignore the longer axis
      if sx > 0 and sy > 0 then
        if sx > sy then sx = 0 else sy = 0 end
      end
      -- preserve sign
      if dx < 0 then sx = -sx end
      if dy < 0 then sy = -sy end
      return sx, sy
    end
To use this function you need something like:

Code: Select all

    function separate()
       -- determine potential collisions
       local blocknearplayer = findblocksnear(player)
       -- check potential collisions
       for block in pairs(blocknearplayer) do 
          sx, sy = checkcol(player, block)
          -- solve each collision
          if sx and sy then
             moveplayer(sx, sy)
          end
        end
    end
- How do you expect the tile to behave when it contacts the player?
If you want to have pushable tiles and so on it gets complicated.
Simple example: viewtopic.php?f=5&t=75931
In this case I would suggest an external library like Bump, Hardon or perhaps FizzX.
lewislepton
Prole
Posts: 6
Joined: Sun May 03, 2015 12:12 pm

Re: grid based block interaction

Post by lewislepton »

thanks for getting back.

wel how it is right now is that the player is freely moving around. sort of like a top-down at this moment. but its only if the player hits a wall block that it stops moving, much like how i have found in if it reaches say 0 on the x or y axis that it stops moving. but its putting tht it actual blocks. so really, its to have these walls within a grid based 1 & 0 type method.
movaeble blocks would be something for the future, but just to start off small im just looking for the player to stop once hitting a wall

as for the player script

Code: Select all

player = {}
player.x, player.y = 300.0, 400.0
player.w, player.h = 32,32
player.velX, player.velY = 0.0,0.0
player.speed = 60
player.friction = 9.0
player.gravity = 8.91
player.velocity = 0.0
player.draw = love.graphics.newImage('asset/player.png')
-- player.x = 30
-- player.y = 30
-- player.velX = 0.0
-- player.velY = 0.0
-- player.speed = 60
-- player.friction = 9

function playerUpdate(dt)
	-- player.velocity = player.velocity + player.gravity
	player.x = player.x + player.velX
	player.y = player.y + player.velY

	player.velX = player.velX * (1 - math.min(dt * player.friction, 1))
	player.velY = player.velY * (1 - math.min(dt * player.friction, 1))

	if love.keyboard.isDown('left') then
		player.velX = player.velX - player.speed * dt
	elseif love.keyboard.isDown('right') then
		player.velX = player.velX + player.speed * dt
	end
	if love.keyboard.isDown('up') then
		player.velY = player.velY - player.speed * dt
	elseif love.keyboard.isDown('down') then
		player.velY = player.velY + player.speed * dt
	end
end

function playerDraw()
	love.graphics.draw(player.draw, player.x, player.y)
	-- love.graphics.rectangle('fill', player.x, player.y, player.w, player.h)
end
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: grid based block interaction

Post by ivan »

lewislepton wrote:putting tht it actual blocks. so really, its to have these walls within a grid based 1 & 0 type method.
movaeble blocks would be something for the future, but just to start off small im just looking for the player to stop once hitting a wall
If you're going for action-based gameplay like in the old 2D Zeldas,
I would advise moving directly to a collisions library since
it's tricky (and a lot of work) to get that part working correctly
and the rest of your gameplay will be depended on it.

But in case you want to implement your own, just post your .love file :)
Last edited by ivan on Sun May 03, 2015 3:14 pm, edited 1 time in total.
lewislepton
Prole
Posts: 6
Joined: Sun May 03, 2015 12:12 pm

Re: grid based block interaction

Post by lewislepton »

cool spot. well ihave seen hardoncollider, also there is bounding box. but hardon seems one of the better ways to go

thanks for your help
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests