1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 
16 
17 #include "btCollisionObject.h"
18 #include "LinearMath/btSerializer.h"
19 
btCollisionObject()20 btCollisionObject::btCollisionObject()
21 	:	m_anisotropicFriction(1.f,1.f,1.f),
22 	m_hasAnisotropicFriction(false),
23 	m_contactProcessingThreshold(BT_LARGE_FLOAT),
24 		m_broadphaseHandle(0),
25 		m_collisionShape(0),
26 		m_rootCollisionShape(0),
27 		m_collisionFlags(btCollisionObject::CF_STATIC_OBJECT),
28 		m_islandTag1(-1),
29 		m_companionId(-1),
30 		m_activationState1(1),
31 		m_deactivationTime(btScalar(0.)),
32 		m_friction(btScalar(0.5)),
33 		m_restitution(btScalar(0.)),
34 		m_internalType(CO_COLLISION_OBJECT),
35 		m_userObjectPointer(0),
36 		m_hitFraction(btScalar(1.)),
37 		m_ccdSweptSphereRadius(btScalar(0.)),
38 		m_ccdMotionThreshold(btScalar(0.)),
39 		m_checkCollideWith(false)
40 {
41 	m_worldTransform.setIdentity();
42 }
43 
~btCollisionObject()44 btCollisionObject::~btCollisionObject()
45 {
46 }
47 
setActivationState(int newState)48 void btCollisionObject::setActivationState(int newState)
49 {
50 	if ( (m_activationState1 != DISABLE_DEACTIVATION) && (m_activationState1 != DISABLE_SIMULATION))
51 		m_activationState1 = newState;
52 }
53 
forceActivationState(int newState)54 void btCollisionObject::forceActivationState(int newState)
55 {
56 	m_activationState1 = newState;
57 }
58 
activate(bool forceActivation)59 void btCollisionObject::activate(bool forceActivation)
60 {
61 	if (forceActivation || !(m_collisionFlags & (CF_STATIC_OBJECT|CF_KINEMATIC_OBJECT)))
62 	{
63 		setActivationState(ACTIVE_TAG);
64 		m_deactivationTime = btScalar(0.);
65 	}
66 }
67 
serialize(void * dataBuffer,btSerializer * serializer) const68 const char* btCollisionObject::serialize(void* dataBuffer, btSerializer* serializer) const
69 {
70 
71 	btCollisionObjectData* dataOut = (btCollisionObjectData*)dataBuffer;
72 
73 	m_worldTransform.serialize(dataOut->m_worldTransform);
74 	m_interpolationWorldTransform.serialize(dataOut->m_interpolationWorldTransform);
75 	m_interpolationLinearVelocity.serialize(dataOut->m_interpolationLinearVelocity);
76 	m_interpolationAngularVelocity.serialize(dataOut->m_interpolationAngularVelocity);
77 	m_anisotropicFriction.serialize(dataOut->m_anisotropicFriction);
78 	dataOut->m_hasAnisotropicFriction = m_hasAnisotropicFriction;
79 	dataOut->m_contactProcessingThreshold = m_contactProcessingThreshold;
80 	dataOut->m_broadphaseHandle = 0;
81 	dataOut->m_collisionShape = serializer->getUniquePointer(m_collisionShape);
82 	dataOut->m_rootCollisionShape = 0;//@todo
83 	dataOut->m_collisionFlags = m_collisionFlags;
84 	dataOut->m_islandTag1 = m_islandTag1;
85 	dataOut->m_companionId = m_companionId;
86 	dataOut->m_activationState1 = m_activationState1;
87 	dataOut->m_activationState1 = m_activationState1;
88 	dataOut->m_deactivationTime = m_deactivationTime;
89 	dataOut->m_friction = m_friction;
90 	dataOut->m_restitution = m_restitution;
91 	dataOut->m_internalType = m_internalType;
92 
93 	char* name = (char*) serializer->findNameForPointer(this);
94 	dataOut->m_name = (char*)serializer->getUniquePointer(name);
95 	if (dataOut->m_name)
96 	{
97 		serializer->serializeName(name);
98 	}
99 	dataOut->m_hitFraction = m_hitFraction;
100 	dataOut->m_ccdSweptSphereRadius = m_ccdSweptSphereRadius;
101 	dataOut->m_ccdMotionThreshold = m_ccdMotionThreshold;
102 	dataOut->m_ccdMotionThreshold = m_ccdMotionThreshold;
103 	dataOut->m_checkCollideWith = m_checkCollideWith;
104 
105 	return btCollisionObjectDataName;
106 }
107 
108 
serializeSingleObject(class btSerializer * serializer) const109 void btCollisionObject::serializeSingleObject(class btSerializer* serializer) const
110 {
111 	int len = calculateSerializeBufferSize();
112 	btChunk* chunk = serializer->allocate(len,1);
113 	const char* structType = serialize(chunk->m_oldPtr, serializer);
114 	serializer->finalizeChunk(chunk,structType,BT_COLLISIONOBJECT_CODE,(void*)this);
115 }
116