box2d icon indicating copy to clipboard operation
box2d copied to clipboard

Collisions problem

Open mcfly722 opened this issue 5 years ago • 2 comments

Hello, guys I'm trying to use your engine in my own game and found that the are no collisions between bodies occurs at all. I created a small test with your code from cpp_compliance_test.go, and I see that there the same situation.

This is a small test - a small dinamic circle body above ground Edge. Standard gravity (0,-10), and you can see that this circle body fall down and pass through the edge to negative Y axis.

Is there something wrong in this code that I copied from your cpp_compliance_test.go, or collisions between dynamic/static are not supported yet?

package main

import (
	"fmt"
	"testing"

	"github.com/ByteArena/box2d"
)

func TestBox2dCollisions(t *testing.T) {

	world := box2d.MakeB2World(box2d.MakeB2Vec2(0, -10))

	// ground body (from cpp_compliance_test.cpp)
	{
		bd := box2d.MakeB2BodyDef()
		ground := world.CreateBody(&bd)
		shape := box2d.MakeB2EdgeShape()
		shape.Set(box2d.MakeB2Vec2(-20.0, 0.0), box2d.MakeB2Vec2(20.0, 0.0))
		ground.CreateFixture(&shape, 0.0)
	}

	// circle character (from cpp_compliance_test.cpp)
	bd := box2d.MakeB2BodyDef()
	bd.Position.Set(0, 10.0)
	bd.Type = box2d.B2BodyType.B2_dynamicBody
	bd.FixedRotation = true
	bd.AllowSleep = false

	body := world.CreateBody(&bd)

	shape := box2d.MakeB2CircleShape()
	shape.M_radius = 0.5

	fd := box2d.MakeB2FixtureDef()
	fd.Shape = &shape
	fd.Density = 200.0
	body.CreateFixtureFromDef(&fd)

	// Prepare for simulation. Typically we use a time step of 1/60 of a
	// second (60Hz) and 10 iterations. This provides a high quality simulation
	// in most game scenarios.
	timeStep := 1.0 / 60.0
	velocityIterations := 8
	positionIterations := 3

	for i := 0; i < 100; i++ {
		// Instruct the world to perform a single step of simulation.
		// It is generally best to keep the time step and iterations fixed.
		//runtime.Breakpoint()
		world.Step(timeStep, velocityIterations, positionIterations)
		fmt.Printf("circle character coordinates: %v,%v\n", body.GetPosition().X, body.GetPosition().Y)
	}
}

Output is:

=== RUN   TestBox2dCollisions
circle character coordinates: 0,9.997222222222222
circle character coordinates: 0,9.991666666666665
circle character coordinates: 0,9.983333333333333
circle character coordinates: 0,9.972222222222221
circle character coordinates: 0,9.958333333333332
........etc
........
circle character coordinates: 0,1.2222222222222219
circle character coordinates: 0,0.9999999999999999
circle character coordinates: 0,0.7750000000000001
circle character coordinates: 0,0.5472222222222226
circle character coordinates: 0,0.3166666666666673
circle character coordinates: 0,0.08333333333333426
circle character coordinates: 0,-0.15277777777777657
circle character coordinates: 0,-0.39166666666666516
circle character coordinates: 0,-0.6333333333333315
circle character coordinates: 0,-0.8777777777777757
circle character coordinates: 0,-1.1249999999999976
circle character coordinates: 0,-1.3749999999999973
circle character coordinates: 0,-1.6277777777777747
circle character coordinates: 0,-1.8833333333333298
circle character coordinates: 0,-2.1416666666666626
circle character coordinates: 0,-2.4027777777777732
circle character coordinates: 0,-2.6666666666666616```

mcfly722 avatar Aug 03 '20 08:08 mcfly722

Solution is set filter mask/category bits in fixtures. For example: https://stackoverflow.com/questions/936935/box2d-collision-groups

fe3dback avatar Sep 02 '20 05:09 fe3dback

The problem is with a missing piece in this library. The function MakeB2FixtureDef does not assign a default Filter in the B2FixtureDef that it is returning. The documentation for Box2D indicates that the default value is: CategoryBits: 0x0001,MaskBits:0xFFFF so the fix is simple, in DynamicsB2Fixture.go file, the function needs to be updated to include the default Filter as well: Filter: B2Filter{CategoryBits: 0x0001,MaskBits:0xFFFF},

E4 avatar Sep 05 '20 02:09 E4