Exemple #1
0
func TestCollideBoxBox(t *testing.T) {
	a, b, cons := NewBody(NewBox(0.5, 0.5, 0.5)), NewBody(NewBox(1, 1, 1)), newManifold()
	if _, _, cs := collideBoxBox(a, b, cons); len(cs) == 0 || cs[0].depth != -1.58 ||
		dumpV3(cs[0].point) != "{-1.0 0.5 0.5}" || dumpV3(cs[0].normal) != "{-1.0 -0.0 -0.0}" {
		depth, point, norm := cs[0].depth, dumpV3(cs[0].point), dumpV3(cs[0].normal)
		t.Errorf("Boxes should collide since one is inside the other %f %s %s", depth, point, norm)
	}

	// just inside of contact range.
	a.World().Loc.SetS(0, 0, 1.49)
	if _, _, cs := collideBoxBox(a, b, cons); len(cs) != 4 || !lin.Aeq(cs[0].depth, -0.09) ||
		dumpV3(cs[0].point) != "{0.5 0.5 1.0}" || dumpV3(cs[0].normal) != "{0.0 0.0 1.0}" {
		depth, point, norm := cs[0].depth, dumpV3(cs[0].point), dumpV3(cs[0].normal)
		t.Errorf("Boxes should collide %f %s %s", depth, point, norm)
	}
	a.World().Loc.SetS(0, 1.49, 0)
	if _, _, cs := collideBoxBox(a, b, cons); len(cs) != 4 || !lin.Aeq(cs[0].depth, -0.09) ||
		dumpV3(cs[0].point) != "{0.5 1.0 0.5}" || dumpV3(cs[0].normal) != "{0.0 1.0 0.0}" {
		depth, point, norm := cs[0].depth, dumpV3(cs[0].point), dumpV3(cs[0].normal)
		t.Errorf("Boxes should collide %f %s %s", depth, point, norm)
	}
	a.World().Loc.SetS(1.49, 0, 0)
	if _, _, cs := collideBoxBox(a, b, cons); len(cs) != 4 || !lin.Aeq(cs[0].depth, -0.09) ||
		dumpV3(cs[0].point) != "{1.0 0.5 0.5}" || dumpV3(cs[0].normal) != "{1.0 0.0 0.0}" {
		depth, point, norm := cs[0].depth, dumpV3(cs[0].point), dumpV3(cs[0].normal)
		t.Errorf("Boxes should collide %f %s %s", depth, point, norm)
	}

	// just outside of contact range.
	a.World().Loc.SetS(0, 0, 1.6)
	if _, _, cs := collideBoxBox(a, b, cons); len(cs) != 0 {
		t.Errorf("Boxes should not collide")
	}
}
Exemple #2
0
func TestSphereProperties(t *testing.T) {
	b := newBody(NewSphere(1)).SetMaterial(0.5, 0.8).(*body)
	if b.IsMovable() != true || !lin.Aeq(b.imass, 2) {
		t.Errorf("Expecting movable body with mass %f", b.imass)
	}
	if dumpV3(b.iit) != "{5.0 5.0 5.0}" {
		t.Errorf("Expecting initial inverse inertia %s", dumpV3(b.iit))
	}
}
Exemple #3
0
// Tests that the narrowphase collision lookup finds the algorithm that flips
// the box-sphere to be sphere-box.
func TestCollideBoxSphere(t *testing.T) {
	box, sphere, c, cons := newBody(NewBox(1, 1, 1)), newBody(NewSphere(1)), newCollider(), newManifold()
	sphere.World().Loc.SetS(0, 2, 0)
	algorithm := c.algorithms[box.shape.Type()][sphere.shape.Type()]
	i, j, cs := algorithm(box, sphere, cons)
	ii, jj := i.(*body), j.(*body)
	if ii.shape.Type() != SphereShape || jj.shape.Type() != BoxShape {
		t.Error("Should have flipped the objects into Sphere, Box")
	}
	if !lin.Aeq(cs[0].depth, -margin) || dumpV3(cs[0].point) != "{0.0 1.0 0.0}" || dumpV3(cs[0].normal) != "{0.0 1.0 0.0}" {
		t.Errorf("Contact info should be the same %f %s %s", cs[0].depth, dumpV3(cs[0].point), dumpV3(cs[0].normal))
	}
}
Exemple #4
0
func TestCollideBoxBox1(t *testing.T) {
	slab := newBody(NewBox(50, 50, 50)).setMaterial(0, 0)
	slab.World().Loc.SetS(0, -50, 0)
	box := newBody(NewBox(1, 1, 1)).setMaterial(1, 0)
	box.World().Loc.SetS(-5.000000, 1.388000, -3.000000)
	box.World().Rot.SetS(0.182574, 0.365148, 0.547723, 0.730297)
	wantPoint, wantDepth := lin.NewV3S(-5.2, -0.1, -4.0), -0.108
	_, _, cs := collideBoxBox(slab, box, newManifold())
	if !lin.Aeq(cs[0].depth, wantDepth) || dumpV3(cs[0].point) != dumpV3(wantPoint) {
		t.Errorf("Got point %s wanted %s. Got depth %f wanted %f",
			dumpV3(cs[0].point), dumpV3(wantPoint), cs[0].depth, wantDepth)
	}
}
Exemple #5
0
func TestCollideSphereBox(t *testing.T) {
	a, b, cons := NewBody(NewSphere(1)), NewBody(NewBox(1, 1, 1)), newManifold()
	if _, _, cs := collideSphereBox(a, b, cons); cs[0].depth != -2.04 ||
		dumpV3(cs[0].point) != "{1.0 0.0 0.0}" || dumpV3(cs[0].normal) != "{1.0 0.0 0.0}" {
		t.Errorf("Sphere touching box at point A %f %s %s", cs[0].depth, dumpV3(cs[0].point), dumpV3(cs[0].normal))
	}
	a.World().Loc.SetS(0, 2, 0)
	if _, _, cs := collideSphereBox(a, b, cons); !lin.Aeq(cs[0].depth, -margin) ||
		dumpV3(cs[0].point) != "{0.0 1.0 0.0}" || dumpV3(cs[0].normal) != "{0.0 1.0 0.0}" {
		t.Errorf("Sphere touching box at point %f %s %s", cs[0].depth, dumpV3(cs[0].point), dumpV3(cs[0].normal))
	}
	a.World().Loc.SetS(0, 0, 2.15)
	if _, _, cs := collideSphereBox(a, b, cons); len(cs) != 0 {
		t.Errorf("Sphere not touching box %f %s %s", cs[0].depth, dumpV3(cs[0].point), dumpV3(cs[0].normal))
	}

	// close enough to be considered in contact.
	a.World().Loc.SetS(0, 0, 2.1)
	if _, _, cs := collideSphereBox(a, b, cons); !lin.Aeq(cs[0].depth, 0.06) ||
		dumpV3(cs[0].point) != "{0.0 0.0 1.0}" || dumpV3(cs[0].normal) != "{0.0 0.0 1.0}" {
		t.Errorf("Sphere close to touching box %f %s %s", cs[0].depth, dumpV3(cs[0].point), dumpV3(cs[0].normal))
	}
}
Exemple #6
0
func TestSphereVolume(t *testing.T) {
	sp := Shape(NewSphere(1.25))
	if !lin.Aeq(sp.Volume(), 6.13592315) {
		t.Errorf("Expected sphere mass 6.13592315, got %2.8f", sp.Volume())
	}
}