Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / OpenPL_Demo / CApi.cpp
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         Draft high-level generic physics C-API. For low-level access, use the physics SDK native API's.
18         Work in progress, functionality will be added on demand.
19
20         If possible, use the richer Bullet C++ API, by including <src/btBulletDynamicsCommon.h>
21 */
22
23 #include "Bullet-C-Api.h"
24 #include "btBulletDynamicsCommon.h"
25
26 /*
27         Create and Delete a Physics SDK 
28 */
29
30 plPhysicsSdkHandle      plNewBulletSdk()
31 {
32         return 0;
33 }
34
35 void            plDeletePhysicsSdk(plPhysicsSdkHandle   physicsSdk)
36 {
37         
38 }
39
40 /* Dynamics World */
41 plDynamicsWorldHandle plCreateDynamicsWorld(plPhysicsSdkHandle physicsSdk)
42 {
43         return (plDynamicsWorldHandle) new btDiscreteDynamicsWorld;
44 }
45 void           plDeleteDynamicsWorld(plDynamicsWorldHandle world)
46 {
47         btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
48         delete dynamicsWorld;
49 }
50
51 void    plStepSimulation(plDynamicsWorldHandle world,   plReal  timeStep)
52 {
53         btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
54         assert(dynamicsWorld);
55         dynamicsWorld->stepSimulation(timeStep);
56 }
57
58 void plAddRigidBody(plDynamicsWorldHandle world, plRigidBodyHandle object)
59 {
60         btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
61         assert(dynamicsWorld);
62         btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
63         assert(body);
64
65         dynamicsWorld->addRigidBody(body);
66 }
67
68 void plRemoveRigidBody(plDynamicsWorldHandle world, plRigidBodyHandle object)
69 {
70         btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
71         assert(dynamicsWorld);
72         btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
73         assert(body);
74
75         dynamicsWorld->removeRigidBody(body);
76 }
77
78 /* Rigid Body  */
79
80 plRigidBodyHandle plCreateRigidBody(    void* user_data,  float mass, plCollisionShapeHandle cshape )
81 {
82         btTransform trans;
83         trans.setIdentity();
84         btVector3 localInertia;
85         btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
86         assert(shape);
87         shape->calculateLocalInertia(mass,localInertia);
88         btRigidBody* body = new btRigidBody(mass, trans,shape,localInertia);
89         body->m_userObjectPointer = user_data;
90         return (plRigidBodyHandle) body;
91 }
92
93 void plDeleteRigidBody(plRigidBodyHandle cbody)
94 {
95         btRigidBody* body = reinterpret_cast< btRigidBody* >(cbody);
96         assert(body);
97         delete body;
98 }
99
100
101 /* Collision Shape definition */
102
103 plCollisionShapeHandle plNewSphereShape(plReal radius)
104 {
105         return (plCollisionShapeHandle) new btSphereShape(radius);
106         
107 }
108         
109 plCollisionShapeHandle plNewBoxShape(plReal x, plReal y, plReal z)
110 {
111         return (plCollisionShapeHandle) new btBoxShape(btVector3(x,y,z));
112 }
113
114 plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height)
115 {
116         //capsule is convex hull of 2 spheres, so use btMultiSphereShape
117         btVector3 inertiaHalfExtents(radius,height,radius);
118         const int numSpheres = 2;
119         btVector3 positions[numSpheres] = {btVector3(0,height,0),btVector3(0,-height,0)};
120         btScalar radi[numSpheres] = {radius,radius};
121         return (plCollisionShapeHandle) new btMultiSphereShape(inertiaHalfExtents,positions,radi,numSpheres);
122 }
123 plCollisionShapeHandle plNewConeShape(plReal radius, plReal height)
124 {
125         return (plCollisionShapeHandle) new btConeShape(radius,height);
126 }
127
128 plCollisionShapeHandle plNewCylinderShape(plReal radius, plReal height)
129 {
130         return (plCollisionShapeHandle) new btCylinderShape(btVector3(radius,height,radius));
131 }
132
133 void plDeleteShape(plCollisionShapeHandle cshape)
134 {
135         btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
136         assert(shape);
137         delete shape;
138 }
139 void plSetScaling(plCollisionShapeHandle cshape, plVector3 cscaling)
140 {
141         btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
142         assert(shape);
143         btVector3 scaling(cscaling[0],cscaling[1],cscaling[2]);
144         shape->setLocalScaling(scaling);        
145 }
146
147
148
149 void plSetPosition(plRigidBodyHandle object, const plVector3 position)
150 {
151 }
152 void plSetOrientation(plRigidBodyHandle object, const plQuaternion orientation)
153 {
154 }
155
156
157 //plRigidBodyHandle plRayCast(plDynamicsWorldHandle world, const plVector3 rayStart, const plVector3 rayEnd, plVector3 hitpoint, plVector3 normal);
158
159 //      extern  plRigidBodyHandle plObjectCast(plDynamicsWorldHandle world, const plVector3 rayStart, const plVector3 rayEnd, plVector3 hitpoint, plVector3 normal);