I want to detect contact with two objects with BulletX in C#
But everytime i try,i fail
In C++,there is a demo called CollisionInterfaceDemo.so i convert it to C#,and it works well
but in this demo,it doesn't contains more complex transform on Shapes.
if i use a boxShape with boxHalfExtents(0.5,0.5,0.5) to initiate it,and set worldTransform to scale and translate, it result in wrong detetion, why?
Could you help me to find errors in the following codes?
public class CollisionTest
{
public static CollisionWorld collisionWorld;
private static bool flag=false;
private static System.Windows.Media.Media3D.Matrix3D matrix1;
private static System.Windows.Media.Media3D.Matrix3D matrix2;
static CollisionTest()
{
CollisionDispatcher dispatcher = new CollisionDispatcher();
Vector3 worldAabbMin = new Vector3(-10000, -10000, -10000);
Vector3 worldAabbMax = new Vector3(10000, 10000, 10000);
AxisSweep3 broadphase = new AxisSweep3(worldAabbMin, worldAabbMax, 100);
collisionWorld = new CollisionWorld(dispatcher, broadphase);
matrix1 = System.Windows.Media.Media3D.Matrix3D.Identity;
matrix2 = System.Windows.Media.Media3D.Matrix3D.Identity;
matrix1.Scale(new System.Windows.Media.Media3D.Vector3D(100, 100, 100));
matrix2.Scale(new System.Windows.Media.Media3D.Vector3D(100, 100, 100));
}
public static bool IsContact(Object3D o1, Object3D o2)
{
bool isContact = false;
IList<CollisionObject> ObjectList = new List<CollisionObject>();
// ObjectList.Add(CreateCollisionObject(o1));
// ObjectList.Add(CreateCollisionObject(o2));
ObjectList.Add(CreateTestObject());
ObjectList.Add(CreateTestObject());
//System.Windows.Media.Media3D.Matrix3D matrix1 = o1.Shapes[0].Transform;
//System.Windows.Media.Media3D.Matrix3D matrix2 = o2.Shapes[0].Transform;
matrix1.Translate(new System.Windows.Media.Media3D.Vector3D(0,0,1));
ObjectList[0].WorldTransform = new Matrix((float)matrix1.M11, (float)matrix1.M12, (float)matrix1.M13, (float)matrix1.M14,
(float)matrix1.M21, (float)matrix1.M22, (float)matrix1.M23, (float)matrix1.M24,
(float)matrix1.M31, (float)matrix1.M32, (float)matrix1.M33, (float)matrix1.M34,
(float)matrix1.OffsetX, (float)matrix1.OffsetY, (float)matrix1.OffsetZ, (float)matrix1.M44);
ObjectList[1].WorldTransform = new Matrix((float)matrix2.M11, (float)matrix2.M12, (float)matrix1.M13, (float)matrix1.M14,
(float)matrix2.M21, (float)matrix2.M22, (float)matrix2.M23, (float)matrix2.M24,
(float)matrix2.M31, (float)matrix2.M32, (float)matrix2.M33, (float)matrix2.M34,
(float)matrix2.OffsetX, (float)matrix2.OffsetY, (float)matrix2.OffsetZ, (float)matrix2.M44);
collisionWorld.AddCollisionObject(ObjectList[0]);
collisionWorld.AddCollisionObject(ObjectList[1]);
if (collisionWorld != null)
collisionWorld.PerformDiscreteCollisionDetection();
///one way to draw all the contact points is iterating over contact manifolds / points:
int i;
int numManifolds = collisionWorld.Dispatcher.ManifoldCount;
//the following codes is to get Contact Points detail
for (i = 0; i < numManifolds; i++)
{
PersistentManifold contactManifold = collisionWorld.Dispatcher.GetManifoldByIndex(i);
CollisionObject obA = contactManifold.BodyA as CollisionObject;
CollisionObject obB = contactManifold.BodyB as CollisionObject;
contactManifold.RefreshContactPoints(obA.WorldTransform, obB.WorldTransform);
int numContacts = contactManifold.ContactsCount;
for (int j = 0; j < numContacts; j++)
{
ManifoldPoint pt = contactManifold.GetContactPoint(j);
Vector3 ptA = pt.PositionWorldOnA;
Vector3 ptB = pt.PositionWorldOnB;
Console.WriteLine("Contact! X:::{0}:::{1}", ptA.X - ptB.X, ObjectList[0].WorldTransform.M42);
isContact = true;
}
//you can un-comment out this line, and then all points are removed
contactManifold.ClearManifold();
}
collisionWorld.RemoveCollisionObject(ObjectList[0]);
collisionWorld.RemoveCollisionObject(ObjectList[1]);
ObjectList.Clear();
// if (!isContact) Console.WriteLine("false");
return isContact;
}
private static CollisionObject CreateTestObject() {
Vector3 boxHalfExtents = new Vector3( 0.5f,
0.5f,
0.5f
);
BoxShape boxShape = new BoxShape(boxHalfExtents);
CollisionObject collisionBoject = new CollisionObject();
collisionBoject.CollisionShape = boxShape;
return collisionBoject;
}
In the debug Mode:
I watch when
boxShaps boxHalfExtents is (0.5,0.5,0.5)
collisionObject[0].WorldTransForm is(100,0,0,0,0,100,0,0,0,0,100,0,0,0,10,1)
collisionObject[1].WorldTransForm is (100,0,0,0,0,100,0,0,0,0,100,0,0,0,0,1)
and it cannot reach the line: Console.WriteLine("Contact! X:::{0}:::{1}", ptA.X - ptB.X, ObjectList[0].WorldTransform.M42);
In my logic,collisionObject[0]'s center(0,0,10),large(100,100,100)
collisionObject[1]'s center(0,0,0),large(100,100,100)
why doesn't they contact?
Hello everyone! Could anyone help me out of this problem?
-
- Posts: 18
- Joined: Thu Nov 15, 2007 2:01 pm
- Location: Sichuan province, China
-
- Site Admin
- Posts: 4221
- Joined: Sun Jun 26, 2005 6:43 pm
- Location: California, USA
Re: Hello everyone! Could anyone help me out of this problem?
You can only use method 1 (provide local scaling) or method 2 ('bake' the scaling into the halfextents). You cannot apply the scaling into the world transform. Bullet and BulletX doesn't allow this.ocean2oo6 wrote:1:HalfExtent =0.5, LocalScaling=100
2:HalfExtent =50, LocalScaling=1
3:HalfExtent =0.5 or any suitable, WorldMatrix to scale 100 or any suitable
if I have an Object transfrom from Identity Box, How to initial it? HalfExtent,LocalScaling,WorldMatrix?
And, It would be much easier to code if i can use Method 3th. Because i can apply it to any transformed object.
But, I failed each time i put scale in WorldMatrix(not in HalfExtent or LocalScaling).
why?
What do you mean exactly by 'an object that transform from a box' ?I want to detect contact whth any object that transfroms from an box,how can i do?
thx for reading my question,really.
Thanks,
Erwin
-
- Posts: 18
- Joined: Thu Nov 15, 2007 2:01 pm
- Location: Sichuan province, China
Re: Hello everyone! Could anyone help me out of this problem?
Thank u for your quick answer
I mean that in the case i want to detect two object transfrom from an BoxShape, such as Camera frustum.
i know it's TransfromMatrix from an Identity BoxShape
then,how can i detect contact like this object?
I mean that in the case i want to detect two object transfrom from an BoxShape, such as Camera frustum.
i know it's TransfromMatrix from an Identity BoxShape
then,how can i detect contact like this object?
-
- Site Admin
- Posts: 4221
- Joined: Sun Jun 26, 2005 6:43 pm
- Location: California, USA
Re: Hello everyone! Could anyone help me out of this problem?
The world transform for a Bullet rigid body can only contain a translation or rotation, so you cannot deform a box into a frustum.
You can create a ConvexHullShape instead, and add all the corner points of your frustum.
Hope this helps,
Erwin
You can create a ConvexHullShape instead, and add all the corner points of your frustum.
Hope this helps,
Erwin
-
- Posts: 18
- Joined: Thu Nov 15, 2007 2:01 pm
- Location: Sichuan province, China
Re: Hello everyone! Could anyone help me out of this problem?
yep, thx a lot. i understand it now!