Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Overlapping physics objects driving me mad :)

edited April 2015 in Questions Posts: 277

Hello all,

I'm having an issue with overlapping physics objects.

In the following video, you can see that when I create regular shaped physics objects close each other, their positions adjust to leave room to one another.

This works great until I have an elongated physics object. Then, creating an object close to the elongated one result in a crazy overlapping.

Here's a little screen capture of the messy situation: https://dl.dropboxusercontent.com/u/73950/IMG_1747.jpg

I need to avoid that. I was thinking of testing for overlap (with body.testOverlap( otherBody )) every time I create a new object and force-change the y coordinate of one object so it moves higher up and the two objects are separated. But wouldn't this end up taking al lot of resources? Also, it would most certainly screw with the goal of the game, which is to balance structures out.

Any idea on where this "bug" could come from, and how I could deal with it?

Thanks!!!

Comments

  • dave1707dave1707 Mod
    Posts: 7,521

    @Rodolphe Isn't the object of your game to drop the shapes onto the pile and not create them in the pile. If you drop them, you won't have the problem.

  • Posts: 277

    indeed, but -strategically- some shapes are best placed between others, already existing, objects. in order to consolidate the structure. I really need to keep this feature...

  • dave1707dave1707 Mod
    Posts: 7,521

    That makes sense. What if you only allow the long object to be dropped.

  • Posts: 277

    The same problem occurs when the long object already exists and another object is created right besides.

  • dave1707dave1707 Mod
    Posts: 7,521

    OK. Let me try some things and see what I come up with.

  • Posts: 277

    Thanks! :)

  • Jmv38Jmv38 Mod
    edited April 2015 Posts: 3,295

    maybe you could do this: start with a small object then make it grow bigger (replace it with a slightly bigger one) until you get the right shape? then maybe the room needed will be generated in the process, pushing other objects, and you get what you want.

  • Posts: 277

    hmmm, that's a lead! thanks!

  • dave1707dave1707 Mod
    Posts: 7,521

    One problem with that is if the (long) object goes between two objects. Instead of landing on them, it's will push them apart as it grows to it original size.

  • Posts: 277

    Yes indeed... I'm starting to think that testing for overlap every time I create a new object and, if True, force-changing the y coordinate of one object so it moves up or do, is the most sensible solution. hmm :(

  • dave1707dave1707 Mod
    Posts: 7,521

    @Rodolphe I created a test with a pile of objects. When I create a long, skinny objects randomly in the pile, it pushes everything out of its way. As I continue to create the skinny object in the pile, everything gets pushed to the side or pushed up. I have no overlapping objects.

  • edited April 2015 Posts: 277

    Thanks for the test @dave1707!

    I noticed it mainly happens with an object I create like this (the one on the picture and video). Could I be doing this wrong?

    local r1 = 920.0*math.random(98,102)/100.0
    local e = 2520.0*math.random(98,102)/100.0
    local a = math.random(0.0,math.pi*100.0)/100
    
    points = {
    vec2(0,e*(math.random(90,100)/100)):rotate(2*math.pi/1.03):rotate(a),
    vec2(0,e*(math.random(90,100)/100)):rotate(2*math.pi/.97):rotate(a),
    vec2(0,r1*(math.random(95,105)/100)):rotate(2*math.pi/6):rotate(a),
    vec2(0,e*(math.random(90,100)/100)):rotate(2*math.pi/3.27):rotate(a),
    vec2(0,e*(math.random(90,100)/100)):rotate(2*math.pi/2.77):rotate(a),
    vec2(0,r1*(math.random(95,105)/100)):rotate(math.pi):rotate(a),
    vec2(0,e*(math.random(90,100)/100)):rotate(2*math.pi/1.56):rotate(a),
    vec2(0,e*(math.random(90,100)/100)):rotate(2*math.pi/1.44):rotate(a),
    vec2(0,r1*(math.random(95,105)/100)):rotate(2*math.pi/1.2):rotate(a)
    }
    

    I don't know why, but I feel the problem might actually be related to these tripod shapes, and not the long blocks...

  • dave1707dave1707 Mod
    Posts: 7,521

    @Rodolphe I haven't tried to use that code to create an object yet, but if the points aren't in a clockwise or counter clockwise order, then they won't create the object you think they will. I'll use the code above and see the order of the points. That probably is the problem because no matter how many objects I created in my test, I didn't have any overlaps.

  • dave1707dave1707 Mod
    Posts: 7,521

    @Rodolphe The points you create with the code above are created in a counter clockwise order. I took your code and created objects with them. I created a pile of 40 of them and then started to create them within the pile. Depending on where the objects were, they DID overlap and they wouldn't seperate. It appears that objects created with more than 4 point ( ? ) can overlap and they get stuck trying to seperate.

  • Posts: 2,042

    @dave1707, could you try setting the objects as bullets in your test? A Google search I did turned that up as a potential fix.

  • Posts: 277

    Thanks @dave1707, this is useful! do all the other objects of your test have 3 points?

    I'll try to set them as bullets too, and see what happens, thanks @Jakattak. What's the downside of this by the way?

  • dave1707dave1707 Mod
    Posts: 7,521

    @Rodolphe In my original test, all the objects had 4 points. I'll try setting bullets in the second test and see what happens. It won't be for awhile because I have to go somewhere first.

  • Posts: 277

    I just tried bullet, actually, and it doesn't solve my problem... I also tried physics.iterations(100,100) (instead of the default 10,8), but it still happens.

  • edited April 2015 Posts: 277

    Oops, I forgot to set physics.continuous to True before using bullet. when I set bullet to only tripods shapes, it seems to behave better (the shapes still overlap, but are pushed away from each other a bit faster than before). I'm testing further...

    By the way, could the concave shape of the tripods be the reason for this mess?

  • Posts: 2,020

    No, just that box2D has no way to simulate impossible situation like two physics bodies occupying the exact same space, so the results are unpredictable.

    How about an "explosion" (using linearImpulse from the world point of where the new body will appear, acting on all the currently existing bodies), to displace all the other bodies a few frames before the new body appears in the hole opened up. In my experience, getting the force of the linearImpulse right could be tricky, as it would depend on the total mass of bodies pressing down on the spot you're trying to open up. If there's a lot of mass, the bodies won't move at all, whereas if the combined pressure is low the bodies could go flying (perhaps have a counter tracking the total mass of all bodies as they are created, and use that to gauge the strength of explosion needed)

  • Posts: 277

    Hi @yojimbo2000 , indeed, linearImpulse is tricky to use in my case.

    But this made me think of something: is there a quick and easy way to know, one frame before building it, if my newly-made body will be set on top of another?

    If so, I could just adjust the coordinates I will set the new bodies at, with something like createNewBody(touch.x,touch.y+math.random(-10,10)).

  • Posts: 2,020

    I guess first create a dummy body set to sensor (so it doesn't disturb the physics), then do a check for overlaps.

  • dave1707dave1707 Mod
    Posts: 7,521

    Bullet set to true in my test didn't work either. Bullet is for fast moving objects to prevent tunneling, not for overlapping objects. When some of my objects overlap, they start to spin slowly because they're trying to seperate from each other and also reacting to the objects around them. Also, the objects don't have to overlap exactly. If one of them is created over the arm of another, they still stay overlapped. I think that all the points are reacting to each other and depending on their position, just get pushed back and forth and can't get away.

  • edited April 2015 Posts: 342

    Though it may be difficult, i had the idea of making the object grow a different way; make it completely flat then expand it along just one axis to its full size. This way it doesnt slide the objects horizontally, just pushes them up. And even easier, use the physics "edge" type for the bottom so te flat object doesnt clip

  • edited May 2015 Posts: 277

    Alright, after trying many other, simpler, solutions, I'm now convinced I need to try something along the line of @Monkeyman32123 solution.

    I'm thinking of resizing the bodies from tiny to normal size in, say, 5 to 10 frames. Is there a mathematic way to reconstruct a physics body, or its corresponding vector shape:

    points = {vec2(x1,y1),vec2(x2,y2),vec2(x3,y3),vec2(x4,y4),vec2(x5,y5),vec2(x6,y6)}
    

    according to a scale factor? (am I even making sense?)

  • Posts: 2,020

    Something like this?

    local points = myBody.points
    local scale = 0.1
    for i,v in ipairs(points) do
        v = v * scale
    end
    -- myBody.points = points --don't think this works,
    -- so I guess you have to destroy and reinitialise the body? 
    myBody:destroy()
    myBody=physics.body(POLYGON, unpack(points))
    
Sign In or Register to comment.