Class for creating love.physic objects

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
Ducktor Cid
Posts: 18
Joined: Wed Oct 05, 2016 8:08 pm

Class for creating love.physic objects

Post by Ducktor Cid » Mon Oct 17, 2016 7:11 pm

I'm trying to make a game based on both Haunt the House and the Casualty series of flash games.

I tried it in AS3 with Flashpunk but came to a halt when there were no working physics engine that worked with it. (Box2FP which bridged the gap between flashpunk and box2d didn't really work and I had trouble with it and Nape barely had any documentation for AS3)

I've ultimately decided to go straight to love2D since I hadn't gotten really far with the AS3 version and that flash is mostly dead. (I've even had people saying that won't help me test a flash game because I'm using flash)

The main premise of the game is to possess objects around a house and kill as many people. People who witness deaths will make a run for it to the exit, your points are based on how many people you kill (a perfect score being no one escaping and all killed)

I've created a basic player class and a Vector2 class (Vector2 mostly being so I can just put stuff like the players size, velocity and position into one variable) and I want to create a class for the physics objects you can possess and move around.

I've got a very basic framework of it currently:

object = {}
object.__index = object


I have really no idea what I'm supposed to return.

User avatar
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA

Re: Class for creating love.physic objects

Post by Beelz » Mon Oct 17, 2016 11:09 pm

From the looks, you would be returning an instance. Basically you'd hold a table of objects in your main.lua(or in a gamestate) and whenever a new object is created it would call on that constructor which would return a table to be inserted into your object pool. Then you can iterate over that pool for whatever reason. If you are using love.physics(box2d) don't forget to declare your world and update it as well.

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website

User avatar
Party member
Posts: 1394
Joined: Fri Mar 07, 2008 1:39 pm

Re: Class for creating love.physic objects

Post by ivan » Tue Oct 18, 2016 5:15 am

Hello there,
I would like to start by saying that Box2D already returns (userdata) objects.
Personally I don't recommend wrapping Box2D objects.
If you want to roll out your own "wrapper" class then you should at least hide the box2d stuff from the rest of your game:

Code: Select all

local object = {}
local object_mt = { __index = object }
function object.newRect(x,y,width,height)
  local instance = {}
  setmetatable(instance, object_mt)
  instance.body = -- create a body at x,y
  instance.fixture = -- create a fixture of width/height
  return instance
function object.getPosition()
  return self.body:GetPosition()
return object
Note that you have to call "myObject = object.newRect(0,0,32,64)" with a single dot (.) in the case above.
When accessing instances "myObect:getPosition()" you have to use the colon (:) operator.

If you want to have inheritance, I recommend using the following mini-library:

Code: Select all

local oo = {}
local meta = {}
local rmeta = {}
-- Creates a new class
function oo.class()
  local c = {}
  local mt = { __index = c }
  meta[c] = mt
  rmeta[mt] = c
  return c
-- Creates a subclass
function oo.subclass(p)
  assert(meta[p], "undefined parent class")
  local c = oo.class()
  return setmetatable(c, meta[p])
-- Creates a new instance
function oo.instance(c)
  assert(meta[c], "undefined class")
  return setmetatable({}, meta[c])
-- Gets the class of an instance
function oo.getclass(i)
  local mt = getmetatable(i)
  return rmeta[mt]
return oo
Example usage:

Code: Select all

-- Definitions/Inheritance:
local oo = require 'oo'
local Fruit = oo.class()
Fruit.sweetness_threshold = 5 -- sort of like "static"
function Fruit:initialize(sweetness)
  self.sweetness = sweetness
function Fruit:isSweet()
  return self.sweetness > Fruit.sweetness_threshold

local Lemon = oo.subclass(Fruit) -- subclassing
function Lemon:initialize()
  Fruit.initialize(self, 1) -- manually invoking the superclass' initializer

--  Instances:
local lemon = oo.instance(Lemon)
print(lemon:isSweet()) -- false

Post Reply

Who is online

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