📄 oasisphysics.cpp
字号:
/******************************************************************************
* This source file is part of Bad Camel Gaming
* Copyright (C) 2003 Zephie Greyvenstein
* See Readme.html for acknowledgements
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*****************************************************************************/
/******************************************************************************
* FILENAME : oasisPhysics.cpp
* DESCRIPTION : Physics instance
* AUTHOR : Zephie Greyvenstein
*****************************************************************************/
#include "oasisPhysics.h"
#include "oasisPhysical.h"
#include "oasisPhysicsSystem.h"
#include "oasisPhysicsResource.h"
#include "oasisException.h"
#include "oasisConvert.h"
#include "oasisConfiguration.h"
#include "oasisProxies.h"
#include "oasisMass.h"
#include "oasisPhysicsCommon.h"
namespace Oasis {
const real physics::infiniteFriction = dInfinity;
physics::physics( ) {
body = new dBody( physicsSystem::get( ).getODEWorld( )->id( ) );
if ( !body ) {
oasisError( exception::ET_NULL_POINTER,
"Could not create a new ODE body",
"physics::( )" );
}
usedMass = new mass( );
if ( !usedMass ) {
oasisError( exception::ET_NULL_POINTER,
"Could not create a new mass",
"physics::( )" );
}
usedMass->setODEBodyParent( body );
proxies.clear( );
}
physics::~physics( ) {
if ( usedMass ) {
delete usedMass;
usedMass = NULL;
}
if ( body ) {
delete body;
body = NULL;
}
}
void physics::updateProxies( void ) {
for ( proxyList::iterator it = proxies.begin( );
it != proxies.end( ); ++it ) {
( *it )->setPosition( convert::parseVector3( body->getPosition( ) ) );
( *it )->setOrientation( convert::parseQuaternion( body->getQuaternion( ) ) );
}
}
void physics::setDynamicsEnabled( bool newState, bool newReenable ) {
if ( newState ) {
if ( !drivingPhysical ) {
oasisError( exception::ET_INTERNAL_ERROR,
"Cannot enable dynamics unless attach to a physical",
"physics::setDynamicsEnabled" );
}
body->enable( );
disableTimer = 0;
setPosition( drivingPhysical->getPosition( ) );
setOrientation( drivingPhysical->getOrientation( ) );
} else {
body->setLinearVel( 0, 0, 0 );
body->setAngularVel( 0, 0, 0 );
body->disable( );
}
dynamicsEnabled = newState;
reenableDynamics = newReenable;
}
const bool physics::getDynamicsEnabled( void ) const {
return dynamicsEnabled;
}
const bool physics::getDynamicsReenabled( void ) const {
return reenableDynamics;
}
void physics::setCollisionEnabled( bool newState ) {
if ( newState ) {
if ( !drivingPhysical ) {
oasisError( exception::ET_INTERNAL_ERROR,
"Cannot enable collision unless attach to a physical",
"physics::setDynamicsEnabled" );
}
collisionEnabled = true;
} else {
collisionEnabled = false;
}
collisionEnabled = newState;
}
const bool physics::getCollisionEnabled( void ) const {
return collisionEnabled;
}
mass *physics::getMass( void ) const {
return usedMass;
}
boxProxy *physics::addBoxProxy( real width, real height, real depth ) {
boxProxy *newProxy = new boxProxy( physicsSystem::get( ).getODESpace( ),
width, height, depth );
if ( !newProxy ) {
oasisError( exception::ET_NULL_POINTER,
"Could not create box proxy",
"physics::addBoxProxy" );
}
newProxy->setParent( this );
newProxy->setEnabled( true );
proxies.push_back( newProxy );
return newProxy;
}
sphereProxy *physics::addSphereProxy( real radius ) {
sphereProxy *newProxy = new sphereProxy( physicsSystem::get( ).getODESpace( ),
radius );
if ( !newProxy ) {
oasisError( exception::ET_NULL_POINTER,
"Could not create sphere proxy",
"physics::addSphereProxy" );
}
newProxy->setParent( this );
newProxy->setEnabled( true );
proxies.push_back( newProxy );
return newProxy;
}
ccylinderProxy *physics::addCCylinderProxy( real radius, real length ) {
ccylinderProxy *newProxy = new ccylinderProxy( physicsSystem::get( ).getODESpace( ),
radius, length );
if ( !newProxy ) {
oasisError( exception::ET_NULL_POINTER,
"Could not create capped cylinder proxy",
"physics::addCCylinderProxy" );
}
newProxy->setParent( this );
newProxy->setEnabled( true );
proxies.push_back( newProxy );
return newProxy;
}
void physics::removeProxy( uint8 index ) {
if ( index < proxies.size( ) ) {
// Get the correct element
proxyList::iterator it = proxies.begin( );
// Increment
while ( index-- ) {
++it;
}
delete ( *it );
proxies.erase( it );
} else {
oasisError( exception::ET_ITEM_NOT_FOUND,
"Index was out of bounds",
"physics::removeProxy" );
}
}
void physics::removeProxy( proxy *oldProxy ) {
if ( oldProxy != NULL ) {
delete oldProxy;
proxies.remove( oldProxy );
}
}
void physics::removeAllProxies( void ) {
for ( proxyList::iterator it = proxies.begin( );
it != proxies.end( ); ++it ) {
delete ( *it );
}
proxies.clear( );
}
physics::proxyIterator physics::getProxyIterator( void ) {
return proxyIterator( proxies.begin( ), proxies.end( ) );
}
void physics::setSoftness( const real newSoftness ) {
softness = newSoftness;
}
const real physics::getSoftness( void ) const {
return softness;
}
void physics::setFriction( const real newFriction ) {
friction = newFriction;
}
const real physics::getFriction( void ) const {
return friction;
}
void physics::setGravity( bool newState ) {
useGravity = newState;
if ( useGravity ) {
body->setGravityMode( 1 );
} else {
body->setGravityMode( 0 );
}
}
const bool physics::getGravity( void ) const {
return useGravity;
}
void physics::setBounce( const real coEff, const real velThreshold ) {
bounceCoeffRest = coEff;
bounceVelThreshold = velThreshold;
}
const real physics::getBounceCoEff( void ) const {
return bounceCoeffRest;
}
const real physics::getBounceVelThreshold( void ) const {
return bounceVelThreshold;
}
void physics::setLinearVelocity( real x, real y, real z ) {
setLinearVelocity( vector3( x, y, z ) );
}
void physics::setLinearVelocity( const vector3 &velocity ) {
if ( !drivingPhysical ) {
oasisError( exception::ET_INTERNAL_ERROR,
"Cannot set velocity on an unused physics",
"physics::setLinearVelocity" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -