Skip to content

Commit

Permalink
iSGL3D source added to Google Code.
Browse files Browse the repository at this point in the history
git-svn-id: http://isgl3d.googlecode.com/svn/trunk@6 92988a87-13a2-556d-73fc-66728a54d5e9
  • Loading branch information
stuartcaunt committed Feb 6, 2011
1 parent 0a9883e commit dfec381
Show file tree
Hide file tree
Showing 10 changed files with 1,383 additions and 0 deletions.
66 changes: 66 additions & 0 deletions isgl3dbullet/Isgl3dMotionState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* iSGL3D: http://isgl3d.com
*
* Copyright (c) 2010-2011 Stuart Caunt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#ifndef ISGL3DMOTIONSTATE_H_
#define ISGL3DMOTIONSTATE_H_

#include <LinearMath/btTransform.h>
#include <LinearMath/btMotionState.h>

@class Isgl3dNode;

/*
* The Isgl3dMotionState inherits from btMotionState and is passed in the constructor of a btRigidBody.
* The Isgl3dMotionState constructor takes an Isgl3dNode: this node corresponds to the rendered equivalent
* to the btRigidBody. This provides the bridging between the btRigidBody and the Isgl3dNode, allowing the
* transformation from one to be passed to the other. During the simulation step of Bullet, the btMotionState
* allows direct access to the transformation of the physics object to update any graphical peers.
*/
class Isgl3dMotionState : public btMotionState {

public :
Isgl3dMotionState(Isgl3dNode * sceneNode);
virtual ~Isgl3dMotionState();

/**
* Gets the transformation of the the Isgl3dNode and applies it to the btRigidBody.
* Called internally by the Bullet physics engine.
*/
virtual void getWorldTransform(btTransform& centerOfMassWorldTrans ) const;

/**
* Gets the transformation of the the btRigidBody and applies it to the Isgl3dNode.
* Called internally by the Bullet physics engine.
*/
virtual void setWorldTransform(const btTransform& centerOfMassWorldTrans);


private :
Isgl3dNode * _node;

};


#endif /*ISGL3DMOTIONSTATE_H_*/
46 changes: 46 additions & 0 deletions isgl3dbullet/Isgl3dMotionState.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* iSGL3D: http://isgl3d.com
*
* Copyright (c) 2010-2011 Stuart Caunt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#import "Isgl3dMotionState.h"
#import "Isgl3dNode.h"

Isgl3dMotionState::Isgl3dMotionState(Isgl3dNode * node) :
_node(node) {
}

Isgl3dMotionState::~Isgl3dMotionState() {
}

void Isgl3dMotionState::getWorldTransform(btTransform& centerOfMassWorldTrans) const {
float transformation[16];
[_node getTransformationAsOpenGLMatrix:transformation];
centerOfMassWorldTrans.setFromOpenGLMatrix(transformation);
}

void Isgl3dMotionState::setWorldTransform(const btTransform& centerOfMassWorldTrans) {
float transformation[16];
centerOfMassWorldTrans.getOpenGLMatrix(transformation);
[_node setTransformationFromOpenGLMatrix:transformation];
}
72 changes: 72 additions & 0 deletions isgl3dbullet/Isgl3dPhysicsObject3D.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* iSGL3D: http://isgl3d.com
*
* Copyright (c) 2010-2011 Stuart Caunt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#import <Foundation/Foundation.h>

@class Isgl3dNode;
@class Isgl3dVector3D;
class btRigidBody;

/**
* The Isgl3dPhysicsObject3D contains both an Isgl3dNode and a btRigidBody providing a strong link between both
* the physicaly object and its rendered peer.
*
* Note that when the Isgl3dPhysicsObject3D is deleted, the associated btRigidBody, the btCollisionShape and
* the btMotionState are also deleted. During construction, The Isgl3dNode is retained and when the
* Isgl3dPhysicsObject3D is deleted the Isgl3dNode is released.
*/
@interface Isgl3dPhysicsObject3D : NSObject {

@private
Isgl3dNode * _node;
btRigidBody * _rigidBody;

}

/**
* Returns the associated Isgl3dNode.
*/
@property (readonly) Isgl3dNode * node;

/**
* Returns the associated btRigidBody.
*/
@property (readonly) btRigidBody * rigidBody;

/**
* Initialises the Isgl3dPhysicsObject3D with an Isgl3dNode and a btRigidBody.
* @param node The Isgl3dNode.
* @param rigidBody The btRigidBody.
*/
- (id) initWithNode:(Isgl3dNode *)node andRigidBody:(btRigidBody *)rigidBody;

/**
* Applies a force, defined as a vector, to the btRigidBody at a given vector position.
* @param force The force to be applied.
* @param position The position at which the force is applied.
*/
- (void) applyForce:(Isgl3dVector3D *)force withPosition:(Isgl3dVector3D *)position;

@end
64 changes: 64 additions & 0 deletions isgl3dbullet/Isgl3dPhysicsObject3D.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* iSGL3D: http://isgl3d.com
*
* Copyright (c) 2010-2011 Stuart Caunt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#import "Isgl3dPhysicsObject3D.h"
#import "Isgl3dNode.h"
#import "Isgl3dVector3D.h"

#include "btBulletDynamicsCommon.h"

@implementation Isgl3dPhysicsObject3D

@synthesize node = _node;
@synthesize rigidBody = _rigidBody;

- (id) initWithNode:(Isgl3dNode *)node andRigidBody:(btRigidBody *)rigidBody {
if (self = [super init]) {
_node = [node retain];
_rigidBody = rigidBody;

}

return self;
}

- (void) dealloc {
[_node release];

delete _rigidBody->getMotionState();
delete _rigidBody->getCollisionShape();
delete _rigidBody;

[super dealloc];
}

- (void) applyForce:(Isgl3dVector3D *)force withPosition:(Isgl3dVector3D *)position {
btVector3 bodyForce(force.x, force.y, force.z);
btVector3 bodyPosition(position.x, position.y, position.z);

_rigidBody->applyForce(bodyForce, bodyPosition);
}

@end
94 changes: 94 additions & 0 deletions isgl3dbullet/Isgl3dPhysicsWorld.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* iSGL3D: http://isgl3d.com
*
* Copyright (c) 2010-2011 Stuart Caunt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#import "Isgl3dNode.h"

@class Isgl3dPhysicsObject3D;
class btRigidBody;
class btDiscreteDynamicsWorld;
class btCollisionShape;

/**
* The Isgl3dPhysicsWorld provides a wrapper to the btDiscreteDynamicsWorld and contains all the Isgl3dPhysicsObject3D objects.
* It inherits from Isgl3dNode so is added directly to the scene. At every frame it updates automatically the
* btDiscreteDynamicsWorld (the physics simulation is updated) and hence the transformations of the physics
* objects (btRigidBody) are updated.
*/
@interface Isgl3dPhysicsWorld : Isgl3dNode {

@private
btDiscreteDynamicsWorld * _discreteDynamicsWorld;

NSDate * _lastStepTime;
NSMutableArray * _physicsObjects;

}

/**
* Initialises the Isgl3dPhysicsWorld;
*/
- (id) init;

/**
* Sets the btDiscreteDynamicsWorld in which the Bullet Physics simulation takes place. The btDiscreteDynamicsWorld is
* automatically stepped at every rendered frame.
*/
- (void) setDiscreteDynamicsWorld:(btDiscreteDynamicsWorld *)discreteDynamicsWorld;

/**
* Adds a new Isgl3dPhysicsObject3D containing both a btRigidBody and an Isgl3dNode. Note, this DOES NOT add the
* node contained in the physics object to the scene: this needs to be done independently. The btRigidBody is
* added to the btDiscreteDynamicsWorld.
* @param physicsObject The Isgl3dPhysicsObject3D containing both btRigidBody and Isgl3dNode.
*/
- (void) addPhysicsObject:(Isgl3dPhysicsObject3D *)physicsObject;

/**
* Removes an Isgl3dPhysicsObject3D from the physics world. Note, this DOES remove the associated Isgl3dNode
* from its parent node in the scene. The btRigidBody is removed from the btDiscreteDynamicsWorld.
* @param physicsObject The Isgl3dPhysicsObject3D to remove.
*/
- (void) removePhysicsObject:(Isgl3dPhysicsObject3D *)physicsObject;

/**
* Sets the size and direction of gravity in the physics simulation.
* @param x The x component of the gravity vectory.
* @param y The y component of the gravity vectory.
* @param z The z component of the gravity vectory.
*/
- (void) setGravity:(float)x y:(float)y z:(float)z;

/**
* Utility method to create an Isgl3dPhysicsObject3D from an Isgl3dNode and a btCollisionShape. A btRigidBody is created for
* the shape with an Isgl3dMotionShape containing the node. Both btRigidBody and Isgl3dNode are returned in the Isgl3dPhysicsObject3D.
* @param node The Isgl3dNode to be manipulated as a result of the physics simulation.
* @param shape The shape of the object.
* @param mass The mass of the object.
* @param restitution The restitution of the object.
* @return (autorelease) The created Isgl3dPhysicsObject3D containing the btRigidBody and the Isgl3dNode.
*/
- (Isgl3dPhysicsObject3D *) createPhysicsObject:(Isgl3dNode *)node shape:(btCollisionShape *)shape mass:(float)mass restitution:(float)restitution;

@end
Loading

0 comments on commit dfec381

Please sign in to comment.