Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Physics do not spawn unless I create extra unused b2Vec2 #79

Open
scott-cornwell opened this issue Oct 7, 2024 · 1 comment
Open

Physics do not spawn unless I create extra unused b2Vec2 #79

scott-cornwell opened this issue Oct 7, 2024 · 1 comment

Comments

@scott-cornwell
Copy link

I noticed this completely by accident, but I fear this library is not stable enough to use because of this. I ended up with an extra b2Vec2 that I wasn't using, so I deleted it, but it caused my physics spawn to stop working. Made some other changes and now I had to add two unused b2Vec2 instances to get it to work. Makes absolutely no sense. All I can think is it's some kind of odd race condition?

// this "zero" vec2 does nothing and is not referenced anywhere but if I comment it out nothing spawns
const zero = new b2Vec2(0, 0) 
const pos = new b2Vec2(options.x, options.y)
let shape

const fixture = new b2FixtureDef()
fixture.density = 1.0
fixture.friction = 0.3
shape = new b2CircleShape()
shape.m_radius = options.radius

const bodyDef = new b2BodyDef()
bodyDef.set_type(b2_dynamicBody)

bodyDef.set_position(pos)

const body = this.world.CreateBody(bodyDef)
body.CreateFixture(fixture)
body.SetTransform(pos, 0)
body.SetAwake(true)
body.SetEnabled(true)
@scott-cornwell
Copy link
Author

Still don't fully understand what's going on, but it seems destroying the pos b2Vec2 I was using after each spawn makes it work, without adding unused b2Vec2 objects. Very strange! I get that the objects are pooled in this version, which is great but I don't understand how that could even cause this behavior as it didn't stop working after the first spawn, it just didn't work at all. I also was not reusing b2Vec2s for each spawn, I was creating new ones so not sure how that would affect it but it does.

Box2D.destroy(pos)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant