is it possible to use grid with newWorld and love.body

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
kingslovelua
Citizen
Posts: 71
Joined: Mon Sep 26, 2011 7:16 pm

is it possible to use grid with newWorld and love.body

Post by kingslovelua »

Hello I'm playing around with Lua/Love2D and I want to know if it's possible to Use a grid and love.body together. I wanted to make and test out grids and I also want to make a world = love.physics.newWorld to set the gravity 0, 0 and use love.body:applyForce to give it a space like feeling.


So right now I have player

Code: Select all

    
     player = {}
      	player.gridx = 64
 	player.gridy = 64
	player.acty = 200
	player.speed = 32
 


Because I'm using grid but I would also like add

Code: Select all

	world = love.physics.newWorld(0, 0, true)

       In   love.load()
And then in player class


Code: Select all

	
   player = {}
      player.body = love.physics.newBody(world, 200, 550, "dynamic")
      player.body:setMass(100) -- make it pretty light
      player.shape = love.physics.newRectangleShape(0, 0, 30, 15)
      player.fixture = love.physics.newFixture(player.body, player.shape, 2) 
      player.fixture:setRestitution(0.4)    -- make it bouncy


and then use

Code: Select all

     if love.keyboard.isDown("right") then
          player.body:applyForce(10, 0.0)
          print("moving right")
     elseif love.keyboard.isDown("left") then
          player.body:applyForce(-10, 0.0)
          print("moving left")
     end
    if love.keyboard.isDown("up") then
       player.body:applyForce(0, -500)
    elseif love.keyboard.isDown("down") then
        player.body:applyForce(0, 100)
    end
Instead of

Code: Select all

      function love.keypressed(key)

      if key == "up" then
        if testMap(0, -1) then
            player.gridy = player.gridy - 32
        end
     elseif key == "down" then
        if testMap(0, 1) then
            player.gridy = player.gridy + 32
         end
       elseif key == "left" then
        if testMap(-1, 0) then
            player.gridx = player.gridx - 32
        end
      elseif key == "right" then
        if testMap(1, 0) then
            player.gridx = player.gridx + 32
        end
      end
     end
enter code here
Attachments
Lovegrid2d.love
(2.86 KiB) Downloaded 111 times
Last edited by kingslovelua on Sun Oct 13, 2013 1:40 pm, edited 1 time in total.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: is it possible to use grid with newWorld and love.body

Post by Roland_Yonaba »

The forum etiquette, in point 9, advises using the right

Code: Select all

 tags when posting.
Not those <code> and </code>.
Please fix that.  :awesome:
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: is it possible to use grid with newWorld and love.body

Post by Robin »

kingslovelua wrote:Hello I'm playing around with Lua/Love2D and I want to know if it's possible to Use a grid and love.body together.
You can, but it's really awkward and depending on what exactly you want, there's going to be a better way.
Help us help you: attach a .love.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: is it possible to use grid with newWorld and love.body

Post by davisdude »

Hi! Welcome to LÖVE! :)

To make it fixed to certain points, just use math.floor and some numbers.
For example:

Code: Select all

function bindToGrid( x, y, number )
    return ( math.floor( x / number ) * number ), ( math.floor( y /  number ) * number )
end
Note that math.floor would round down (lower it). If you wanted to go higher, use math.ceil.
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
kingslovelua
Citizen
Posts: 71
Joined: Mon Sep 26, 2011 7:16 pm

Re: is it possible to use grid with newWorld and love.body

Post by kingslovelua »

Robin wrote:
kingslovelua wrote:Hello I'm playing around with Lua/Love2D and I want to know if it's possible to Use a grid and love.body together.
You can, but it's really awkward and depending on what exactly you want, there's going to be a better way.

yeah I figured it would seem weird to most, but I just wanted the ease of altering a map/ stage/ grid by just adding 111100011010 but still have the ability to add and use love.body features and adding love.physics.newWorld(0, 0, true) for a space like feeling when moving around.

also

Code: Select all

if love.keyboard.isDown("right") then
       player.body:applyForce(10, 0.0)
	   print("moving right")
    elseif love.keyboard.isDown("left") then
			player.body:applyForce(-10, 0.0)
			print("moving left")
		end
		if love.keyboard.isDown("up") then
			player.body:applyForce(0, -500)
		elseif love.keyboard.isDown("down") then
			player.body:applyForce(0, 100)
    end
seems to be a smoother then

Code: Select all

function love.keypressed(key)

    if key == "up" then
        if testMap(0, -1) then
            player.gridy = player.gridy - 32
        end
    elseif key == "down" then
        if testMap(0, 1) then
            player.gridy = player.gridy + 32
        end
    elseif key == "left" then
        if testMap(-1, 0) then
            player.gridx = player.gridx - 32
        end
    elseif key == "right" then
        if testMap(1, 0) then
            player.gridx = player.gridx + 32
        end
    end
end
I put my old version of my game in the attachment this version doesn't use a map just basic old school making solid walls technique. which works great just that I just learn how to make a grid and want to see if I can integrate grids and love.body.

davisdude wrote:Hi! Welcome to LÖVE! :)

To make it fixed to certain points, just use math.floor and some numbers.
For example:

Code: Select all

function bindToGrid( x, y, number )
    return ( math.floor( x / number ) * number ), ( math.floor( y /  number ) * number )
end
Note that math.floor would round down (lower it). If you wanted to go higher, use math.ceil.
I'm new to making maps/grids so I 'm not sure where I would put this code to see if it will work for me. I learned about grids here https://love2d.org/wiki/Tutorial:Gridlocked_Player
Attachments
prototype shoot 0.0.0.0.1.1.love
(5.86 KiB) Downloaded 111 times
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: is it possible to use grid with newWorld and love.body

Post by davisdude »

Code: Select all

function love.keypressed(key)

    if key == "up" then
        if testMap(0, -1) then
            player.gridy = player.gridy - 32
        end
    etc.
Right after this part. Just do

Code: Select all

if key == "up" then
        if testMap(0, -1) then
            player.newy= player.gridy - 32
        end
etc.
Then at the end of the if-elseif-else bit put

Code: Select all

player.gridx, player.gridy = bindToGrid( player.newx, player.newy, 32 )
It doesn't have to be 32, it can be anything that you want to bind it to the grid with.
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: No registered users and 80 guests