improve collider

This commit is contained in:
yangxiao 2015-06-30 14:16:05 +08:00
parent 1e7cc1b159
commit 2ed5ee123e
6 changed files with 83 additions and 25 deletions

View File

@ -445,10 +445,10 @@ Physics3DCollider::~Physics3DCollider()
CC_SAFE_RELEASE(_physics3DShape);
}
Physics3DCollider* Physics3DCollider::create(Physics3DShape *shape)
Physics3DCollider* Physics3DCollider::create(Physics3DColliderDes *info)
{
auto ret = new (std::nothrow) Physics3DCollider();
if (ret->init(shape))
if (ret->init(info))
{
ret->autorelease();
return ret;
@ -523,20 +523,28 @@ bool Physics3DCollider::IsTrigger() const
return (_btGhostObject->getCollisionFlags() & btCollisionObject::CF_NO_CONTACT_RESPONSE) != 0;
}
void Physics3DCollider::setIsTrigger(bool isTrigger)
void Physics3DCollider::setTrigger(bool isTrigger)
{
_btGhostObject->setCollisionFlags(isTrigger == true ?
_btGhostObject->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE :
_btGhostObject->getCollisionFlags() & ~btCollisionObject::CF_NO_CONTACT_RESPONSE);
}
bool Physics3DCollider::init(Physics3DShape *shape)
bool Physics3DCollider::init(Physics3DColliderDes *info)
{
_physics3DShape = shape;
_physics3DShape = info->shape;
_physics3DShape->retain();
_btGhostObject = new btCollider(this);
_btGhostObject->setCollisionShape(_physics3DShape->getbtShape());
_btGhostObject->setCollisionFlags(_btGhostObject->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE);
setTrigger(info->isTrigger);
setFriction(info->friction);
setRollingFriction(info->rollingFriction);
setRestitution(info->restitution);
setHitFraction(info->hitFraction);
setCcdSweptSphereRadius(info->ccdSweptSphereRadius);
setCcdMotionThreshold(info->ccdMotionThreshold);
_type = Physics3DObject::PhysicsObjType::COLLIDER;
return true;
}

View File

@ -360,6 +360,35 @@ protected:
std::vector<Physics3DConstraint *> _constraintList;
};
/**
* @brief The description of Physics3DCollider.
*/
struct CC_DLL Physics3DColliderDes
{
Physics3DShape* shape;
cocos2d::Mat4 originalTransform;
bool isTrigger; //is it a trigger?
float friction;
float rollingFriction;
float restitution;
float hitFraction;
float ccdSweptSphereRadius;
float ccdMotionThreshold;
Physics3DColliderDes()
: shape(nullptr)
, isTrigger(false)
, friction(0.5f)
, rollingFriction(0.0f)
, restitution(0.0f)
, hitFraction(1.0f)
, ccdSweptSphereRadius(0.0f)
, ccdMotionThreshold(0.0f)
{
}
};
/**
* @brief Inherit from Physics3DObject, the main class for Colliders
*/
@ -367,12 +396,12 @@ class CC_DLL Physics3DCollider : public Physics3DObject
{
public:
static Physics3DCollider* create(Physics3DShape *shape);
static Physics3DCollider* create(Physics3DColliderDes *info);
btGhostObject* getGhostObject() const { return _btGhostObject; }
/** Set trigger. */
void setIsTrigger(bool isTrigger);
void setTrigger(bool isTrigger);
/** Check is a trigger. */
bool IsTrigger() const;
@ -423,7 +452,7 @@ CC_CONSTRUCTOR_ACCESS :
Physics3DCollider();
virtual ~Physics3DCollider();
bool init(Physics3DShape *shape);
bool init(Physics3DColliderDes *info);
protected:

View File

@ -36,7 +36,23 @@ PhysicsSprite3D* PhysicsSprite3D::create(const std::string &modelPath, Physics3D
if (ret && ret->initWithFile(modelPath))
{
auto obj = Physics3DRigidBody::create(rigidDes);
ret->_physicsComponent = Physics3DComponent::create(obj);
ret->_physicsComponent = Physics3DComponent::create(obj, translateInPhysics, rotInPhsyics);
ret->addComponent(ret->_physicsComponent);
ret->_contentSize = ret->getBoundingBox().size;
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return ret;
}
PhysicsSprite3D* PhysicsSprite3D::createWithCollider(const std::string &modelPath, Physics3DColliderDes* colliderDes, const cocos2d::Vec3& translateInPhysics, const cocos2d::Quaternion& rotInPhsyics)
{
auto ret = new (std::nothrow) PhysicsSprite3D();
if (ret && ret->initWithFile(modelPath))
{
auto obj = Physics3DCollider::create(colliderDes);
ret->_physicsComponent = Physics3DComponent::create(obj, translateInPhysics, rotInPhsyics);
ret->addComponent(ret->_physicsComponent);
ret->_contentSize = ret->getBoundingBox().size;
ret->autorelease();

View File

@ -50,6 +50,9 @@ public:
/** creates a PhysicsSprite3D*/
static PhysicsSprite3D* create(const std::string &modelPath, Physics3DRigidBodyDes* rigidDes, const cocos2d::Vec3& translateInPhysics = cocos2d::Vec3::ZERO, const cocos2d::Quaternion& rotInPhsyics = cocos2d::Quaternion::ZERO);
/** creates a PhysicsSprite3D*/
static PhysicsSprite3D* createWithCollider(const std::string &modelPath, Physics3DColliderDes* colliderDes, const cocos2d::Vec3& translateInPhysics = cocos2d::Vec3::ZERO, const cocos2d::Quaternion& rotInPhsyics = cocos2d::Quaternion::ZERO);
/** Get the Physics3DObject. */
Physics3DObject* getPhysicsObj() const;

View File

@ -584,13 +584,12 @@ bool Physics3DTerrainDemo::init()
terrain->setCameraMask((unsigned short)CameraFlag::USER1);
//create terrain
Physics3DRigidBodyDes rbDes;
rbDes.mass = 0.0f;
std::vector<float> heidata = terrain->getHeightData();
auto size = terrain->getTerrainSize();
rbDes.shape = Physics3DShape::createHeightfield(size.width, size.height, &heidata[0], 1.0f, terrain->getMinHeight(), terrain->getMaxHeight(), true, false, true);
auto rigidBody = Physics3DRigidBody::create(&rbDes);
auto component = Physics3DComponent::create(rigidBody);
Physics3DColliderDes colliderDes;
colliderDes.shape = Physics3DShape::createHeightfield(size.width, size.height, &heidata[0], 1.0f, terrain->getMinHeight(), terrain->getMaxHeight(), true, false, true);
auto collider = Physics3DCollider::create(&colliderDes);
auto component = Physics3DComponent::create(collider);
terrain->addComponent(component);
this->addChild(terrain);
component->syncNodeToPhysics();
@ -598,6 +597,7 @@ bool Physics3DTerrainDemo::init()
//create several spheres
Physics3DRigidBodyDes rbDes;
rbDes.mass = 1.f;
rbDes.shape = Physics3DShape::createSphere(0.5f);
float start_x = START_POS_X - ARRAY_SIZE_X/2 + 5.0f;
@ -629,16 +629,15 @@ bool Physics3DTerrainDemo::init()
//create mesh
std::vector<Vec3> trianglesList = Bundle3D::getTrianglesList("Sprite3DTest/boss.c3b");
rbDes.mass = 0.0f;
rbDes.shape = Physics3DShape::createMesh(&trianglesList[0], (int)trianglesList.size() / 3);
rigidBody = Physics3DRigidBody::create(&rbDes);
component = Physics3DComponent::create(rigidBody);
auto sprite = Sprite3D::create("Sprite3DTest/boss.c3b");
sprite->addComponent(component);
colliderDes.shape = Physics3DShape::createMesh(&trianglesList[0], (int)trianglesList.size() / 3);
auto sprite = PhysicsSprite3D::createWithCollider("Sprite3DTest/boss.c3b", &colliderDes);
sprite->setRotation3D(Vec3(-90.0f, 0.0f, 0.0f));
sprite->setPosition3D(Vec3(0.0f, 15.0f, 0.0f));
sprite->setCameraMask(2);
this->addChild(sprite);
sprite->syncNodeToPhysics();
sprite->setSyncFlag(Physics3DComponent::PhysicsSyncFlag::NONE);
std::vector<std::pair<Physics3DShape*, Mat4> > shapeList;
{
@ -660,7 +659,7 @@ bool Physics3DTerrainDemo::init()
rbDes.mass = 10.0f;
rbDes.shape = Physics3DShape::createCompoundShape(shapeList);
rigidBody = Physics3DRigidBody::create(&rbDes);
auto rigidBody = Physics3DRigidBody::create(&rbDes);
component = Physics3DComponent::create(rigidBody);
auto sprite = Sprite3D::create("Sprite3DTest/orc.c3b");
sprite->addComponent(component);
@ -744,7 +743,7 @@ bool Physics3DCollisionCallbackDemo::init()
std::string Physics3DColliderDemo::subtitle() const
{
return "Physics3D Collider";
return "Physics3D Trigger";
}
bool Physics3DColliderDemo::init()
@ -769,7 +768,10 @@ bool Physics3DColliderDemo::init()
this->addChild(sprite);
{
auto collider = Physics3DCollider::create(Physics3DShape::createSphere(10.0f));
Physics3DColliderDes colliderDes;
colliderDes.shape = Physics3DShape::createSphere(10.0f);
colliderDes.isTrigger = true;
auto collider = Physics3DCollider::create(&colliderDes);
auto component = Physics3DComponent::create(collider);
auto node = Node::create();
node->addComponent(component);

View File

@ -165,4 +165,4 @@ private:
#endif
#endif
#endif