コード例 #1
0
ファイル: PhysicObject.cpp プロジェクト: muhaos/MHEngine
void PhysicObject::createPhysObject() {
	btDefaultMotionState* fallMotionState =
			new btDefaultMotionState(btTransform(btQuaternion((initialOrientation.y*GRAD_TO_RAD_COEF), (initialOrientation.x*GRAD_TO_RAD_COEF), (initialOrientation.z*GRAD_TO_RAD_COEF)),btVector3(initialPosition.x, initialPosition.y, initialPosition.z)));
	btVector3 fallInertia(0,0,0);
	collisionShape->getCollisionShape()->calculateLocalInertia(mass, fallInertia);
	btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, collisionShape->getCollisionShape(), fallInertia);
	rigidBody = new btRigidBody(fallRigidBodyCI);
	setAngularFactor(angularFactor);
	setLinearFactor(linearFactor);
	setLinearVelocity(linearVelocity);
	rigidBody->setFriction(friction);
	rigidBody->setRestitution(restitution);
	setAngularVelocity(angularVelocity);
	rigidBody->setDamping(linearDumping, angularDumping);
	if (_isTrigger) {
		rigidBody->setCollisionFlags(rigidBody->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE);
	} else
	if (_isKinematic) {
		rigidBody->setCollisionFlags(rigidBody->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
	}

	rigidBody->setCollisionFlags(rigidBody->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
	rigidBody->setUserPointer(this);

	if (!isEnableDeactivation())
		rigidBody->setActivationState(DISABLE_DEACTIVATION);
	Game::instance->dynamicsWorld->addRigidBody(rigidBody);
}
コード例 #2
0
ファイル: PhysicsRigidBody.cpp プロジェクト: dtbinh/Game3D
PhysicsRigidBody::PhysicsRigidBody(Node* node, const PhysicsCollisionShape::Definition& shape, const Parameters& parameters, int group, int mask)
        : PhysicsCollisionObject(node, group, mask), _body(NULL), _mass(parameters.mass), _constraints(NULL), _inDestructor(false)
{
    GP_ASSERT(Game::getInstance()->getPhysicsController());
    GP_ASSERT(_node);

    // Create our collision shape.
    kmVec3 centerOfMassOffset = vec3Zero;
    _collisionShape = Game::getInstance()->getPhysicsController()->createShape(node, shape, &centerOfMassOffset, parameters.mass != 0.0f);
    GP_ASSERT(_collisionShape && _collisionShape->getShape());

    // Create motion state object.
	_motionState = new PhysicsMotionState(node, this, (kmVec3LengthSq(&centerOfMassOffset) > MATH_EPSILON) ? &centerOfMassOffset : NULL);

    // If the mass is non-zero, then the object is dynamic so we calculate the local 
    // inertia. However, if the collision shape is a triangle mesh, we don't calculate 
    // inertia since Bullet doesn't currently support this.
    btVector3 localInertia(0.0, 0.0, 0.0);
    if (parameters.mass != 0.0)
        _collisionShape->getShape()->calculateLocalInertia(parameters.mass, localInertia);

    // Create the Bullet physics rigid body object.
    btRigidBody::btRigidBodyConstructionInfo rbInfo(parameters.mass, NULL, _collisionShape->getShape(), localInertia);
    rbInfo.m_friction = parameters.friction;
    rbInfo.m_restitution = parameters.restitution;
    rbInfo.m_linearDamping = parameters.linearDamping;
    rbInfo.m_angularDamping = parameters.angularDamping;

    // Create + assign the new bullet rigid body object.
    _body = bullet_new<btRigidBody>(rbInfo);

    // Set motion state after rigid body assignment, since bullet will callback on the motion state interface to query
    // the initial transform and it will need to access to rigid body (_body).
    _body->setMotionState(_motionState);

    // Set other initially defined properties.
    setKinematic(parameters.kinematic);
    setAnisotropicFriction(parameters.anisotropicFriction);
    setAngularFactor(parameters.angularFactor);
    setLinearFactor(parameters.linearFactor);

    // Add ourself to the physics world.
    Game::getInstance()->getPhysicsController()->addCollisionObject(this);

    if (_collisionShape->getType() == PhysicsCollisionShape::SHAPE_HEIGHTFIELD)
    {
        // Add a listener on the node's transform so we can track dirty changes to calculate
        // an inverse kmMat4 for transforming heightfield points between world and local space.
        _node->addListener(this);
    }
}
コード例 #3
0
ファイル: PhysicObject.cpp プロジェクト: muhaos/MHEngine
void PhysicObject::loadPhysProperties(FILE* f) {
	enablePhysics(readChar(f));
	if (!isEnabledPhysics())
		return;
	setMass(readFloat(f));
	setAngularFactor(readVector(f));
	setLinearFactor(readVector(f));
	//writeVector(getLinearVelocity(), f);
	setTrigger(readChar(f));
	setCollisionShapeType((CollisionShapeType)readChar(f));
	setEnableDeactivation(readChar(f));
	setFriction(readFloat(f));
	setRestitution(readFloat(f));
	setLinearDumping(readFloat(f));
	setAngularDumping(readFloat(f));

	// load custom collision shape
	if (getCollisionShapeType() == CST_CUSTOM) {
		switch ((GOCollisionShapeType)readChar(f)) {
			case  GOCST_COMPOUND_SHAPE:
				setCollisionShape(new GOCompoundCollisionShape(f));
				break;
			case  GOCST_BOX:
				setCollisionShape(new GOBoxCollisionShape(f));
				break;
			case  GOCST_SPHERE:
				setCollisionShape(new GOSphereCollisionShape(f));
				break;
			case  GOCST_CYLINDER:
				setCollisionShape(new GOCylinderCollisionShape(f));
				break;
			case  GOCST_UNDEFINED:
				setCollisionShape(new GOUndefinedCollisionShape(f));
				break;
			case  GOCST_CAPSULE:
				setCollisionShape(new GOCapsuleCollisionShape(f));
				break;
			case  GOCST_CONE:
				setCollisionShape(new GOConeCollisionShape(f));
				break;
			case  GOCST_MESH:
				setCollisionShape(new GOMeshCollisionShape(f));
				break;
			default:
				Log::error("PhysicObject::loadPhysProperties(FILE* f): Undefined custom shape type.");
		}
	}

	// load constraints
	int c_count = readChar(f);
	for (int i = 0; i < c_count; i++) {
		GOConstraint* cs;
		unsigned char type = readChar(f);
		switch (type) {
			case  GO_P2P_CONSTRAINT:
				cs = new GOP2PConstraint(f);
				break;
			case  GO_HINGE_CONSTRAINT:
				cs = new GOHingeConstraint(f);
				break;
			case  GO_SLIDER_CONSTRAINT:
				cs = new GOSliderConstraint(f);
				break;
			default:
				Log::error("PhysicObject::loadPhysProperties(FILE* f): Undefined constraint type.");
		}
		addConstraint(cs);
	}
}
コード例 #4
0
ファイル: PhysicObject.cpp プロジェクト: muhaos/MHEngine
int PhysicObject::methodsBridge(lua_State* luaVM) {
	if (isCurrentMethod("applyImpulse")) {
		applyImpulse(CVector(lua_tonumber(luaVM, 1), lua_tonumber(luaVM, 2), lua_tonumber(luaVM, 3)), CVector(lua_tonumber(luaVM, 4), lua_tonumber(luaVM, 5), lua_tonumber(luaVM, 6)));
		return 0;
	}
	if (isCurrentMethod("applyForce")) {
		applyForce(CVector(lua_tonumber(luaVM, 1), lua_tonumber(luaVM, 2), lua_tonumber(luaVM, 3)), CVector(lua_tonumber(luaVM, 4), lua_tonumber(luaVM, 5), lua_tonumber(luaVM, 6)));
		return 0;
	}
	if (isCurrentMethod("setLinearVelocity")) {
		setLinearVelocity(CVector(lua_tonumber(luaVM, 1), lua_tonumber(luaVM, 2), lua_tonumber(luaVM, 3)));
		return 0;
	}
	if (isCurrentMethod("getLinearVelocity")) {
		luaPushVector(luaVM, getLinearVelocity().x, getLinearVelocity().y, getLinearVelocity().z);
		return 1;
	}
	if (isCurrentMethod("setMass")) {
		setMass(lua_tonumber(luaVM, 1));
		return 0;
	}
	if (isCurrentMethod("getMass")) {
		lua_pushnumber(luaVM, getMass());
		return 1;
	}
	if (isCurrentMethod("setCollisionShapeType")) {
		setCollisionShapeType((CollisionShapeType)lua_tointeger(luaVM, 1));
		return 0;
	}
	if (isCurrentMethod("getCollisionShapeType")) {
		lua_pushinteger(luaVM, getCollisionShapeType());
		return 1;
	}
	if (isCurrentMethod("enablePhysics")) {
		enablePhysics(lua_toboolean(luaVM, 1));
		return 0;
	}
	if (isCurrentMethod("isEnabledPhysics")) {
		lua_pushboolean(luaVM, isEnabledPhysics());
		return 1;
	}
	if (isCurrentMethod("setAngularFactor")) {
		setAngularFactor(CVector(lua_tonumber(luaVM, 1), lua_tonumber(luaVM, 2), lua_tonumber(luaVM, 3)));
		return 0;
	}
	if (isCurrentMethod("getAngularFactor")) {
		luaPushVector(luaVM, getAngularFactor().x, getAngularFactor().y, getAngularFactor().z);
		return 1;
	}
	if (isCurrentMethod("setLinearFactor")) {
		setLinearFactor(CVector(lua_tonumber(luaVM, 1), lua_tonumber(luaVM, 2), lua_tonumber(luaVM, 3)));
		return 0;
	}
	if (isCurrentMethod("getLinearFactor")) {
		luaPushVector(luaVM, getLinearFactor().x, getLinearFactor().y, getLinearFactor().z);
		return 1;
	}
	if (isCurrentMethod("setTrigger")) {
		setTrigger(lua_toboolean(luaVM, 1));
		return 0;
	}
	if (isCurrentMethod("isTrigger")) {
		lua_pushboolean(luaVM, isTrigger());
		return 1;
	}
	if (isCurrentMethod("getCollisionShape")) {
		if (getCollisionShape() == NULL) {
			lua_pushnil(luaVM);
		} else {
			lua_getglobal(luaVM, getCollisionShape()->getGOID().c_str());
		}
		return 1;
	}
	if (isCurrentMethod("setCollisionShape")) {
		if (lua_isnil(luaVM, 1)) {
			setCollisionShape(NULL);
			return 0;
		}
		lua_pushstring(luaVM, "cpointer");
		lua_gettable(luaVM, -2);
		GOCollisionShape* o = (GOCollisionShape*)lua_tointeger(luaVM, -1);
		setCollisionShape(o);
		lua_pop(luaVM, -1);
		return 0;
	}
	if (isCurrentMethod("setEnableDeactivation")) {
		setEnableDeactivation(lua_toboolean(luaVM, 1));
		return 0;
	}
	if (isCurrentMethod("isEnableDeactivation")) {
		lua_pushboolean(luaVM, isEnableDeactivation());
		return 1;
	}
	if (isCurrentMethod("setFriction")) {
		setFriction(lua_tonumber(luaVM, 1));
		return 0;
	}
	if (isCurrentMethod("getFriction")) {
		lua_pushnumber(luaVM, getFriction());
		return 1;
	}
	if (isCurrentMethod("setRestitution")) {
		setRestitution(lua_tonumber(luaVM, 1));
		return 0;
	}
	if (isCurrentMethod("getRestitution")) {
		lua_pushnumber(luaVM, getRestitution());
		return 1;
	}
	if (isCurrentMethod("setLinearDumping")) {
		setLinearDumping(lua_tonumber(luaVM, 1));
		return 0;
	}
	if (isCurrentMethod("getLinearDumping")) {
		lua_pushnumber(luaVM, getLinearDumping());
		return 1;
	}
	if (isCurrentMethod("setAngularDumping")) {
		setAngularDumping(lua_tonumber(luaVM, 1));
		return 0;
	}
	if (isCurrentMethod("getAngularDumping")) {
		lua_pushnumber(luaVM, getAngularDumping());
		return 1;
	}
	if (isCurrentMethod("setAngularVelocity")) {
		setAngularVelocity(CVector(lua_tonumber(luaVM, 1), lua_tonumber(luaVM, 2), lua_tonumber(luaVM, 3)));
		return 0;
	}
	if (isCurrentMethod("getAngularVelocity")) {
		CVector av = getAngularVelocity();
		luaPushVector(luaVM, av.x, av.y, av.z);
		return 1;
	}
	if (isCurrentMethod("addConstraint")) {
		if (lua_isnil(luaVM, 1)) {
			return 0;
		}
		lua_pushstring(luaVM, "cpointer");
		lua_gettable(luaVM, -2);
		GOConstraint* o = (GOConstraint*)lua_tointeger(luaVM, -1);
		addConstraint(o);
		lua_pop(luaVM, -1);
		return 0;
	}
	if (isCurrentMethod("getConstraints")) {
		lua_newtable(luaVM);
		int tableIndex = lua_gettop(luaVM);
		vector<GOConstraint*> objs;
		for (unsigned int i = 0; i < constraints.size(); ++i) {
			if (constraints.at(i)->id == "undefined" || constraints.at(i)->id == "") continue;
			objs.push_back(constraints.at(i));
		}
		for (unsigned int i = 0; i < objs.size(); ++i) {
			lua_pushinteger(luaVM, i+1);
			lua_getglobal(luaVM, objs.at(i)->id.c_str());
			lua_settable (luaVM, tableIndex);
		}
		return 1;
	}
	if (isCurrentMethod("removeConstraint")) {
		if (lua_isnil(luaVM, 1)) {
			return 0;
		}
		lua_pushstring(luaVM, "cpointer");
		lua_gettable(luaVM, -2);
		GOConstraint* o = (GOConstraint*)lua_tointeger(luaVM, -1);
		removeConstraint(o);
		lua_pop(luaVM, -1);
		return 0;
	}
	if (isCurrentMethod("removeAllConstrains")) {
		removeAllConstraints();
		return 0;
	}
	if (isCurrentMethod("secondObjectForConstraint")) {
		if (lua_isnil(luaVM, 1)) {
			return 0;
		}
		lua_pushstring(luaVM, "cpointer");
		lua_gettable(luaVM, -2);
		GOConstraint* o = (GOConstraint*)lua_tointeger(luaVM, -1);
		secondObjectForConstraint(o);
		lua_pop(luaVM, -1);
		return 0;
	}
	if (isCurrentMethod("isSecondObjectForConstraints")) {
		lua_newtable(luaVM);
		int tableIndex = lua_gettop(luaVM);
		vector<GOConstraint*> objs;
		for (unsigned int i = 0; i < secondObjectForConstraints.size(); ++i) {
			if (secondObjectForConstraints.at(i)->id == "undefined" || secondObjectForConstraints.at(i)->id == "") continue;
			objs.push_back(secondObjectForConstraints.at(i));
		}
		for (unsigned int i = 0; i < objs.size(); ++i) {
			lua_pushinteger(luaVM, i+1);
			lua_getglobal(luaVM, objs.at(i)->id.c_str());
			lua_settable (luaVM, tableIndex);
		}
		return 1;
	}
	if (isCurrentMethod("notUseAsSecondObjectForConstraint")) {
		if (lua_isnil(luaVM, 1)) {
			return 0;
		}
		lua_pushstring(luaVM, "cpointer");
		lua_gettable(luaVM, -2);
		GOConstraint* o = (GOConstraint*)lua_tointeger(luaVM, -1);
		notUseAsSecondObjectForConstraint(o);
		lua_pop(luaVM, -1);
		return 0;
	}

	return LuaBridge::methodsBridge(luaVM);
}