Jump to content

Body class function questions


photo

Recommended Posts

Hi Dongju,

An object can have only one body. When a contact with the body emerges a contact callback function is called. But the object's body might have no information on some of its contacts with other physical bodies, as these contacts can be handled by other bodies. So you can't tell which one, the first or the second body participating in a contact with a certain number is yours until you check it. It really depends on which body handles the contact (the same story with shapes).

So a contact can be handled by any of the bodies that participate in it. To which body it will be assigned is completely random. If the contact is assigned to and handled by another body, the number of contacts for the current body is not incremented.

The contact callback function is to be used to detect all contacts in which the body participates (see the example below).

Please be aware that physics-based callbacks are executed in parallel with the main tread, so you should not modify nodes inside these functions, so if you want to reposition, transform, create or delete nodes captured by your callback function, you can store them in the array and then perform all necessary operations in the update().

// list of collisions that were processed by other bodies
Vector<BodyPtr> unregistered_collisions;

// flag indicating if collisions were detected
int collision_detected = 0;

/*...*/

void contact_callback(BodyPtr body, int num)
{
	// collision was detected
	collision_detected = 1;

	// if the collision is processed by other body, adding it to the list
	if (body != this_body)
		unregistered_collisions.append(body);
}

/*...*/

int update()
{
	if (collision_detected)
	{
		// resetting collision flag
		collision_detected = 0;
		for (int i = 0; i< body->getNumContacts(); i++)
		{
			// process all nodes with which collisions were registered
			NodePtr node = body->getContactObject(i)->getNode();

			// changing node's rotation
			if (node)
				node->setRotation(quat(0.0f, 1.0f, 0.0f, 90.0f));
		}
		for (int i = 0; i < unregistered_collisions.size(); i++)
		{
			// process all unregistered collisions
			NodePtr node = unregistered_collisions[i]->getObject()->getNode();

			// deleting a node
			if (node)
				node.deleteLater();
        }
		// clearing the list of unregistered collisions after processing them
		unregistered_collisions.clear();
	}
}

In case of contact with a non-physical object (such object does not have a body) the ContactBody1 shall be NULL. Contact ID, point, normal, etc. should be valid.

Thanks!

  • Like 1
Link to comment
×
×
  • Create New...