question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Breaking solid objects in half

See original GitHub issue

Description

This isn’t actually an issue, I’m just looking for guidance.

Suppose I have a rectangle (representing a brick) and a circle (representing a slow-ish bullet) and if the bullet hits the brick with enough speed I want to turn the brick from one object into two or three objects along fracture lines that seem intuitive.

Do you think it would work for me to modify the ContactManager.Collide() method in such a way that it does two passes… The first pass could replace the bottom part of the function that looks like

// Here we destroy contacts that cease to overlap in the broad-phase.
if (overlap == false)
{
    Contact cNuke = c;
    c = cNuke.GetNext();
    Destroy(cNuke);
    continue;
}

// The contact persists.
c.Update(m_contactListener);
c = c.GetNext();

with something like

if (overlap == true)
{
    ...break brick into pieces if conditions are met...
}

and then the second pass would be exactly the code that’s in the ContactManager.Collide() method normally?

Or would this not work? Maybe I would need to rewind positions after the first pass? Possibly by capturing the world state at the beginning of the first pass and resetting it to that (except for the broken brick) before doing the second pass?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
thomasvtcommented, Apr 1, 2022

Hello,

Splitting a brick into pieces is game logic, and not the job of the physics system (box2d). The job of the physics system is to simulate interaction of physical bodies that the game logic defined earlier. The Physics engine should not be used to decide when to change or create new bodies based on game-specific rules like the one you are describing.

Therefore I suggest you do not change any internal code of box2d, but to use box2d’s ContactListener (many tutorials online. eg. https://www.iforce2d.net/b2dtut/collision-callbacks (should be trivial to translate to C# equivalent).

Your ContactListener implementation will receive collision notifications from box2d. You can then check if one body is a bullet and the other body is a brick. Splitting the brick will work best by deleting the brick entirely and creating several new brickfragments as separate game objects (each with their own RigidBody).

By the way: This kind of question is more a working-with-box2d question, or even general gamedev question. You will reach a lot more people by asking things like this through eg. StackOverflow. The original box2d is C++, so many people will respond from that C++ perspective, but the box2d concepts translate perfectly to C# with Box2d-netstandard. So, you should be able to get help from them too.

0reactions
MadCowChickencommented, Apr 1, 2022

If I create a copy of the bullet with IsSensor = true and fix it to the original, is there a way to guarantee that the sensor collisions will happen before the bullet collisions? And also allow my dynamic fracturing code to be run before the bullet collisions, rather than running my dynamic fracturing code at the end of the collision process?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Split a Solid Into Two Bodies
Use the Split Body command on the Modify panel in the Model workspace. You can use a workplane, surface, sketch, or face to...
Read more >
Blender 3 Tutorial: How To Slice, Cut Out, Separate ... - YouTube
How To Slice, Cut Out, Separate And Fill A Solid Shape From A Solid Object. Blender is a free and open-source 3D computer...
Read more >
What happens when we cut an object in half?
If a solid object breaks, you mostly break the bonds between molecules making up the object. This happens because you exceed the elasticity...
Read more >
Cut, divide, and trim objects in Illustrator
Select the object to use as a cutter, and position it so that it overlaps the objects to cut. Choose Object > Path...
Read more >
Modeling Complex 3D Shapes with the Solid Tools
With SketchUp's Solid tools, you can create new shapes by combining or cutting one shape with another, making it easy to model an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found