Fizzlib, OO Physics (Contacts Fixed!)

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
mikembley
Citizen
Posts: 72
Joined: Wed Dec 17, 2008 5:30 pm
Location: Blackburn, UK
Contact:

Fizzlib, OO Physics (Contacts Fixed!)

Post by mikembley »

Ok, So im in the progress of making a library, with the focus of making physics module a bit easier to use, or harder depending on which way you would prefer to use it.

But first off, i would like to say that my programming skill is somewhat shitty ( making stuff work, but doesnt look neat ) so my code is ugly looking in places, and is probably inefficient to say the least, which is of course why i am posting now, to get some sound and expert advice

Features:
  • -Object Oriented-ness (e.g - b = World:newBody(...) -> b:newRectangleShape(...)
    -Drawing Shapes/Joints for quick prototyping
    -Collision handling (Of which im stuck on, But have a concept for whomever can help me implement it)
Ive uploaded a demo of my project, but unfortunately it will crash, due to the fact that i dont understand why contact:getPosition() seems to crash LÖVE.
And this is mainly why i am posting, as a cry for help.

I am trying to use world:setCallbacks() to add/update/remove the bodies/shapes collision data to themselves. Like i envision in this code:
Which i have now implemented with 3 methods

Code: Select all

local b = fizz.newBody(...)
b:newRectangleShape(...)
b:setMassFromShapes()

local b2 = fizz.newBody(...)
b2:newRectangleShape(...)

if b:isColliding() then
     local otherBody = b:getCollidingBody()               --Returns the body colliding with b
     local myCollidingShape = b:getCollidingShape()              --Returns the shape on b that collided
     local otherBodyCollidingShape = otherBody:getCollidingShape()           --Returns the otherBody's shape that collided
end
It sort of works, but its temperamental, i would guess this issue is possibly the same as this: (http://love2d.org/wiki/Remove_Workaround)
The offending code starts at line 576 of fizzlib.lua


My erratic bug seems to be an issue with a vector lib that i borrowed, some sort of math bug, i havent located it yet, but the area that causes it is the car drawing code.

Also constructive criticism is welcome as well, such as structure and layout of code.

The controls for the little demo:

Left/Right Arrow keys = Rotate Car
Up/Down Arrow Keys = Accelerate/Reverse Car
A / D = Move little character left and right
1 = Turn on crashing contact bug
2 = Toggle Drawing Car and Character images

Sidenote: Ignore the dodgy references in other files ;) i took the LÖVE naming convention a bit too far

Updated:
fizzlib_fixed2.love
Added Contacts
(626.15 KiB) Downloaded 247 times
Older Version:
fizzlib.love
iHazBrokenz
(625.97 KiB) Downloaded 645 times
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Fizzlib, OO Physics (Contacts Fixed!)

Post by ivan »

I actually had to write a Box2D wrapper in Lua for my latest game so I'm somewhat familiar with this.
Although my game is not using Love I took a look at your code and have some suggestions.
A body may collide with several other bodies in the same frame so keep that in mind.
Also, you are creating a new table for each new collision just to overwrite the old collision table which is not very efficient.
"bodyAtPoint(x, y)" there could be several bodies occupying the same point if they have sensor shapes, for example.
Wouldn't it be nice to be able to create and lookup bodies by ID/name?
How about a world:findbody ( id ) function?
A vector class (like b2Vec2) sounds like a neat idea but it's a burden on the Lua GC in my experience.
Lastly, don't forget that when destroying a body, you also have to destroy and clear all associated joints.
User avatar
arquivista
No longer with us
Posts: 266
Joined: Tue Jul 06, 2010 8:39 am
Location: Insert Geolocation tag here
Contact:

Re: Fizzlib, OO Physics (Contacts Fixed!)

Post by arquivista »

Ouch, Fizzlib fixed 2 once it reach the ground it crashes (on a mac osx). Seems either Love 0.62 or 0.7 have same fate :S
--------------------------------------------------------
To Do: Insert Signature Here
--------------------------------------------------------
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Fizzlib, OO Physics (Contacts Fixed!)

Post by zac352 »

mikembley wrote:Ok, So im in the progress of making a library, with the focus of making physics module a bit easier to use, or harder depending on which way you would prefer to use it.

But first off, i would like to say that my programming skill is somewhat shitty ( making stuff work, but doesnt look neat ) so my code is ugly looking in places, and is probably inefficient to say the least, which is of course why i am posting now, to get some sound and expert advice

Features:
  • -Object Oriented-ness (e.g - b = World:newBody(...) -> b:newRectangleShape(...)
    -Drawing Shapes/Joints for quick prototyping
    -Collision handling (Of which im stuck on, But have a concept for whomever can help me implement it)
Ive uploaded a demo of my project, but unfortunately it will crash, due to the fact that i dont understand why contact:getPosition() seems to crash LÖVE.
And this is mainly why i am posting, as a cry for help.

I am trying to use world:setCallbacks() to add/update/remove the bodies/shapes collision data to themselves. Like i envision in this code:
Which i have now implemented with 3 methods

Code: Select all

local b = fizz.newBody(...)
b:newRectangleShape(...)
b:setMassFromShapes()

local b2 = fizz.newBody(...)
b2:newRectangleShape(...)

if b:isColliding() then
     local otherBody = b:getCollidingBody()               --Returns the body colliding with b
     local myCollidingShape = b:getCollidingShape()              --Returns the shape on b that collided
     local otherBodyCollidingShape = otherBody:getCollidingShape()           --Returns the otherBody's shape that collided
end
It sort of works, but its temperamental, i would guess this issue is possibly the same as this: (http://love2d.org/wiki/Remove_Workaround)
The offending code starts at line 576 of fizzlib.lua


My erratic bug seems to be an issue with a vector lib that i borrowed, some sort of math bug, i havent located it yet, but the area that causes it is the car drawing code.

Also constructive criticism is welcome as well, such as structure and layout of code.

The controls for the little demo:

Left/Right Arrow keys = Rotate Car
Up/Down Arrow Keys = Accelerate/Reverse Car
A / D = Move little character left and right
1 = Turn on crashing contact bug
2 = Toggle Drawing Car and Character images

Sidenote: Ignore the dodgy references in other files ;) i took the LÖVE naming convention a bit too far

Updated:
fizzlib_fixed2.love
Older Version:
fizzlib.love
Unless you slice out the love.physics part and add either a vector class or my vector file, I'm not interested. I'll be making my own physics library soon;. :P
Hello, I am not dead.
User avatar
mikembley
Citizen
Posts: 72
Joined: Wed Dec 17, 2008 5:30 pm
Location: Blackburn, UK
Contact:

Re: Fizzlib, OO Physics (Contacts Fixed!)

Post by mikembley »

ivan wrote: A body may collide with several other bodies in the same frame so keep that in mind.
Thank you for the reminder, I was intending to check for multiple bodies.
ivan wrote: Also, you are creating a new table for each new collision just to overwrite the old collision table which is not very efficient.
And this, may be a possible reason why it was crashing in the first place, although i am unsure. What would be the best way? reuse the old table and remove the expired contacts?

Heres how i have it so far:

The world, when created does setCallbacks(fizz.addCollision, fizz.addCollision, fizz.removeCollision, nil)

The fizz.addCollision function accesses the data table set by the shape creation (which is body=theparentbody, and the shape=thecreatedshape)
and changes "theparentbody"'s collision to a table containing all of the colliding bodies/shapes

The fizz.removeCollison function acesses the same data table, but just sets "theparentbody"'s collision table to an empty table { }

Unfortunately i am at the point where i am crashing with no error again, I dont really know what to blame or where to go from here.
The crash only happens, when i use the contacts functions.

So i think the best thing to do is cut back a bit and remove some possible causes and have a simple testbed for now.

Unless, of course, anyone can shed some light on this
ivan wrote: "bodyAtPoint(x, y)" there could be several bodies occupying the same point if they have sensor shapes, for example.
That function was purely just for testing, but i suppose you are right, I will eventually it make it more useful.
ivan wrote: Wouldn't it be nice to be able to create and lookup bodies by ID/name?
How about a world:findbody ( id ) function?

I agree, Although i am not really interested in this part yet
ivan wrote: A vector class (like b2Vec2) sounds like a neat idea but it's a burden on the Lua GC in my experience.
I was only using a vector class to draw a suspension onto my Car.
ivan wrote: Lastly, don't forget that when destroying a body, you also have to destroy and clear all associated joints.
I will try to remember this when i get onto finishing the joints part
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Fizzlib, OO Physics (Contacts Fixed!)

Post by ivan »

mikembley wrote:And this, may be a possible reason why it was crashing in the first place, although i am unsure. What would be the best way? reuse the old table and remove the expired contacts?
You might find useful what I did in my own project:
Each body has a 'contacts' table which buffers its number of contact points.
The the indexes of this table are IDs (or names) of the other bodies which it is collding with.
The value is simply the number of contact points.
Everytime a contact point is added between two bodies all you have to do is increment that number, like so:
body1.contacts[body2.id] = body1.contacts[body2.id] + 1
When a contact point is removed, decrement the number.
The only problem with this method is that it doesn't store the actual position of contact.
User avatar
mikembley
Citizen
Posts: 72
Joined: Wed Dec 17, 2008 5:30 pm
Location: Blackburn, UK
Contact:

Re: Fizzlib, OO Physics (Contacts Fixed!)

Post by mikembley »

ivan wrote:The only problem with this method is that it doesn't store the actual position of contact.
This is an essential part of the contact imo! Anyway, After staring and blindlessly modifying things, I've come to the conclusion it was the Persist callback that i was abusing.

Every time a contact would be generated by the Persist callback, i was actually adding it to my collisions.

The way i do it now, is to use the Persist callback just to update the collisions contacts that were added to my bodies from the Add callback.
And then once the Remove callback comes, i obviously delete the contact.

So far i see it works, I just need to test a few things with it, and then hopefully i can continue with other bits and pieces..

Edit: I also think Contacts are not fulfilled when the body is sleeping (Re-edit: Unconfirmed, Can't reproduce, I suck at bug hunting..)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot] and 0 guests