Forum

Box2d beginner questions, gravity variations


#1

Hi,
I’m trying to create what I thought would be a simple box2d world, where some objects begin static, others fall down and bounce off the static ones as they fall, then eventually the static ones fall off the bottom of the screen too.

This is my first real attempt to use box2d for anything, i’ve done a bit of reading and playing around and I think I can see a few ways of doing it, but none of them seem ideal.

Is it better to keep the world’s gravity off, and use updatebody to make half the objects fall to start with, either with position or velocity? Then change the position or velocity of the (initially) static objects later to make them fall too?
Or is it better to have the world’s gravity turned on but set the (initially) static bodies mass to 0 so they are unaffected by it, then later use updatebody’s position pin to make them fall off the bottom of the screen?

any hints and tips gratefully received, thank you!


#2

… no text …


#3

um, did you accidentally post an empty reply or am i somehow not seeing what you replied with Joreg?


#4

my bad, was a wrong click.


#5

I think I’d start with gravity and have mass 0, then setbody velocity to the static boxes, you might have to update the static bodies postions to keep them in place when they get knocked by falling bodies though.
Or create falling bodies at the locations of the static boxes and at the same time kill the static bodies.


#6

Thanks Catweasel, killing the static bodies and creating new ones in their place turned out to be far the easiest method.

However, it has made another aspect more tricky.
The static (and then later dynamic ones that replace them) bodies are textured with a ‘1’ while the constantly falling bodies are textured with a ‘0’
So, updating the spread of textures when i start killing bodies and creating new ones in their place, which also need to keep the same texture is taxing my 4V knowledge and spread understanding to the max!
I’m pretty sure it’ll involve a select node, and maybe a reorder? But beyond that I’m struggling a bit. Any suggestions on how best to approach this problem?

thanks.


#7

realised insertslice would be my saviour here, but there isn’t one for textures!
What can I use instead?

edit:
think i answered my own question, insertslice going into the select(node) looks like it will work with some fiddling…


#8

I think you add a custm field to objects, so you could sift that maybe and use that?


#9

Thanks! that’s a much better way of doing it. :)