Physics Objects (or today's dumb question)

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
Lord Uber Dowzen
Prole
Posts: 18
Joined: Wed Sep 29, 2010 3:02 am

Physics Objects (or today's dumb question)

Post by Lord Uber Dowzen »

Hello once again.

So today I'm still working on getting the physics system worked out. I'm building the basics of my code off the two physics tutorials in the wiki (the basic Physics tutorial and the Collision Callbacks tutorial) and noticed that each of these treats the objects in the program in different ways. The former has two separate tables for shapes and bodies whereas the latter seems to have a separate table for each object (assuming I've interpreted the code right). Personally the second method seems easier to manage (in my mind at least). So my questions are:

1. Have I completely misinterpreted this code and the way I've described those methods has nothing to do with what is going on?
2. If no, which of the methods is better?
3. What system do you use to keep track of shapes and bodies.

Thanks :awesome:
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Physics Objects (or today's dumb question)

Post by TechnoCat »

I don't think either implementations are very good for most games.

However, the method from the collision callback tutorial way might be good if you know you are going to have a certain (hopefully small if you choose this method) number of objects ahead of time.

Otherwise I would throw all the objects with corresponding functions attached in a generic table with table.insert and then use ipairs to iterate over them all in the table.

I'm sure other people on the forums have much better and more descriptive ways to handle objects though.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Physics Objects (or today's dumb question)

Post by kikito »

I like this question because I spent a lot of time thinking about it a while ago.

I've got a global table of 'actors'.

Each actor has one body (I could change this, but I still have to see a situation that goes well with that). I actually use the Actor himself to build the shapes of the Body (they have add*Shape methods). I always set the Data of every shape to the Actor of its body (this is done on the add*Shape method of the actors)

Actors can have a method called 'touches', and 'stopsTouching'. As parameter, they accept "the other objects they touch", as well as the contact object.

The collision callbacks just makes sure that these methods are called, if they exist. Something like this:

Code: Select all

  local function invokeMethod(methodName, actor1, actor2, contact)
    local method = actor1[methodName]
    if type(method)=='function' then method(actor1, actor2, contact) end
  end

  love.setCallbacks(
    -- Invoke 'touches' on both actors, when the actors start touching each other
    function(actor1,actor2,contact)
      invokeMethod('touches', actor1, actor2, contact)
      invokeMethod('touches', actor2, actor1, contact)
    end, --the actors start touching

    nil, --the actors continue touching. This is processor intensive so I normally leave it blank

    -- Invoke 'stopsTouching' on both actors when they stop touching
    function(actor1,actor2,contact)
      invokeMethod('stopsTouching', actor1, actor2, contact)
      invokeMethod('stopsTouching', actor2, actor1, contact)
    end
  )
The way an Actor handles collision depends completely up to the actor itself. A ship impacted by a powerup will increase its firepower. A missile impacting pretty much anything will generate an explosion, etc.
When I write def I mean function.
Lord Uber Dowzen
Prole
Posts: 18
Joined: Wed Sep 29, 2010 3:02 am

Re: Physics Objects (or today's dumb question)

Post by Lord Uber Dowzen »

Thank you very much, that's awesome!
Post Reply

Who is online

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