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

CreatePolygonFromVertices needs to return body #15

Open
Vykri opened this issue Jan 15, 2025 · 6 comments
Open

CreatePolygonFromVertices needs to return body #15

Vykri opened this issue Jan 15, 2025 · 6 comments

Comments

@Vykri
Copy link

Vykri commented Jan 15, 2025

The CreatePolygonFromVertices function creates the body object, but does not return it.

. There should be a return body; line at the end of that function.

The downstream issue that this is causing is that calling CreatePhysicsEditorShape({worldId, key, url}).then(body => ...) results in body being undefined.

@Vykri
Copy link
Author

Vykri commented Jan 15, 2025

FYI, by just adding return body; CreatePhysicsEditorShape will return an object with {bodyId, shapeId, object} where shapeId is the last shape created in the loop and object is shape's vertices, normals, etc. This doesn't really make much sense since the body is comprised of multiple shapes, so just returning the last one doesn't seem super useful. It should probably just return {bodyId} or {bodyId, shapeIds, objects}.

@mreinstein
Copy link
Contributor

Maybe the CreatePhysicsEditorShape function should just return the bodyId ? I think all the shapes and polygons attached to said body can be queried from that. I'm just unsure how this function is relied upon in userland.

@mreinstein
Copy link
Contributor

if that makes sense to do, it might be good to rename it, something like CreatePhysicsEditorBody which would likely reference one or more shapes?

@Vykri
Copy link
Author

Vykri commented Jan 15, 2025

Honestly, CreatePhysicsEditorShape should probably be split into 2 parts. The comment says "It is recommended to prepare data with this before the game loop starts; It is async and quite slow.", but since it reads the file and also creates the body, it doesn't make sense to call before the game loop as the created body will be useless. I recommend Creating LoadPhysicsEditorShapeData and CreatePhysicsEditorBodyFromData functions where you call LoadPhysicsEditorShapeData during preload and CreatePhysicsEditorBodyFromData to actually create the bodies from that data.

@Vykri
Copy link
Author

Vykri commented Jan 15, 2025

Regardless of what is done to CreatePhysicsEditorShape, CreatePolygonFromVertices should still be fixed to return body at the end of the function.

mreinstein added a commit to mreinstein/phaser-box2d that referenced this issue Jan 18, 2025
@Vykri
Copy link
Author

Vykri commented Feb 13, 2025

Another issue I found with CreatePolygonFromVertices is that the call to CreatePolygon (Which is only called for the first shape when body doesn't exist yet) doesn't pass all of the necessary data along. Specifically, shapeDef should be included as this caused a bug where I called CreatePolygonFromVertices with shapeDef.isSensor = true, but the first shape of the body didn't include this and therefore had collision. The rest of the shapes that were created with the b2CreatePolygonShape calls were correctly marked as sensors, which meant that one small part of my sprite was not a sensor and had collision.

I was able to fix this by adding shapeDef to the param object being sent to CreatePolygon.

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

2 participants