Esempio n. 1
0
void ofxRParticleSystem::init()
{
    uniqueIDs = 0;
    count = new float;
    
    damping = new float;
    restitution = new float;
    accLimit = new float;
    velLimit = new float;
    dt = new float;
    
    setDt(1.0);
    setCount(0);
    setDamping(.25);
    setRestitution(1.0);
    setAccelerationLimit(5.0);
    setVelocityLimit(10.0);
    
    renderer = new ofxRParticleRenderer();
    renderer->setParticlesPtr(&particles);

    solver = NULL;
    solver = new ofxSolver(1);
    setSolver(*solver);
}
Esempio n. 2
0
PhysicsInterface::BodyObject Bullet::createCapsuleBody(float height, float radius, float mass, bool fixed,
                                                       const Entity* entity, const SimpleTransform& initialTransform)
{
    auto collisionShape = new btCapsuleShape(radius, height);

    // Calculate inertia
    auto localInertia = btVector3(0.0f, 0.0f, 0.0f);
    if (!fixed)
        collisionShape->calculateLocalInertia(mass, localInertia);

    // Motion state for this body
    auto motionState = new btDefaultMotionState(toBullet(initialTransform));

    // Create Bullet rigid body
    auto bulletBody = new btRigidBody(
        btRigidBody::btRigidBodyConstructionInfo(fixed ? 0.0f : mass, motionState, collisionShape, localInertia));

    bulletBody->setDamping(DefaultLinearDamping, DefaultAngularDamping);
    bulletBody->setSleepingThresholds(DefaultLinearSleepingThreshold, DefaultAngularSleepingThreshold);
    bulletBody->setRestitution(0.0f);

    // Add body to the simulation
    dynamicsWorld_->addRigidBody(bulletBody);

    // Create local body
    auto body = new Body(bulletBody, entity, fixed);
    bodies_.append(body);

    body->ownedCollisionShape = collisionShape;

    bulletBody->setUserPointer(body);

    return body;
}
Esempio n. 3
0
	void CSubStructure::onInit(IGameObject& object, IGameWorld& world)
	{
		// init all components
		foreachComponent([](IComponent& component, IGameObject& object){component.init(object);}, std::ref(object));

		// need to write the component subobject loop ourselves here, because we need the shared_ptr
		// build the physic body
		for(auto& cell : mCells)
		{
			auto fixture = physics::Fixture::create(object.getBody(), cell->shape(), 10.f);
			fixture.setRestitution(0.1);
			fixture.setFriction(0.5);
			for(std::size_t i = 0; i < cell->component_count(); ++i)
			{
				addChild(cell->getComponent(i));
				fixture.addMass(cell->getComponent(i)->weight());
			}
		}

		// add the armour segments to the object definition
		for(unsigned i = 0; i < mArmour.size(); ++i)
		{
			auto pob = std::make_shared<property::CPropertyObject>("armour" + to_string(i));
			addChild(pob);
			pob->addChild(mArmour[i].armour->getSharedPropertyObject());
			pob->addProperty( property::CProperty::create("p1", pob.get(), mArmour[i].p1) );
			pob->addProperty( property::CProperty::create("p2", pob.get(), mArmour[i].p2) );
		}
	}
Esempio n. 4
0
//private
void TilemapDemoState::buildScene()
{
    if (m_tilemap.load("assets/maps/platform.tmx"))
    {
        auto entity = xy::Entity::create(m_messageBus);
        const auto& layers = m_tilemap.getLayers();
        for (const auto& l : layers)
        {
            if (l->getType() == xy::tmx::Layer::Type::Object)
            {
                xy::Logger::log("found object layer - attempting to create physics components", xy::Logger::Type::Info);
                auto rb = m_tilemap.createRigidBody(m_messageBus, *l);
                entity->addComponent(rb);
            }
            else
            {
                auto drawable = m_tilemap.getDrawable(m_messageBus, *l, m_textureResource, m_shaderResource);
                if (drawable)
                {
                    xy::Logger::log("created layer drawable, adding to scene...");
                    entity->addComponent(drawable);
                }
            }
        }
        m_scene.addEntity(entity, xy::Scene::Layer::BackFront);

        static const float radius = 30.f;

        auto body = xy::Component::create<xy::Physics::RigidBody>(m_messageBus, xy::Physics::BodyType::Dynamic);
        auto cs = xy::Physics::CollisionCircleShape(radius);
        cs.setDensity(0.9f);
        cs.setRestitution(1.f);
        body->addCollisionShape(cs);

        auto drawable = xy::Component::create<xy::SfDrawableComponent<sf::CircleShape>>(m_messageBus);
        drawable->getDrawable().setRadius(radius);
        drawable->getDrawable().setOrigin({ radius, radius });
        drawable->getDrawable().setFillColor({ 255, 255, 255, 200 });
        drawable->getDrawable().setOutlineThickness(2.f);

        auto cam = xy::Component::create<xy::Camera>(m_messageBus, getContext().defaultView);
        cam->lockTransform(xy::Camera::TransformLock::Rotation, true);
        cam->lockBounds(m_tilemap.getBounds());
        
        entity = xy::Entity::create(m_messageBus);
        entity->setPosition(800.f, 400.f);
        entity->addComponent(body);
        entity->addComponent(drawable);
        auto camPtr = entity->addComponent(cam);

        ent = m_scene.addEntity(entity, xy::Scene::Layer::FrontFront);
        m_scene.setActiveCamera(camPtr);
    }
}
Esempio n. 5
0
	void PhysicsObject::Init(Vec2 Pos,double e,double mu,double mass){
		this->Pos=Pos;
		Vel.SetValue(0,0);
		angle=0;
		angleSpeed=0;
		setRestitution(e);
		setFriction(mu); 
		setMass(mass);
		
		
		ID = runningNr;
		runningNr += 1;
	}
Esempio n. 6
0
Window::Window(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Window)
{
    QGLFormat qglFormat;
    qglFormat.setVersion(3, 2);
    qglFormat.setProfile(QGLFormat::CoreProfile);
    qglFormat.setSampleBuffers(true);
    QGLFormat::setDefaultFormat(qglFormat);
    ui->setupUi(this);

    // Set the default values.
    setRadius(DEF_RADIUS);
    setRestitution(DEF_RESTITUTION);
    setMass(DEF_MASS);
    setNumSpheres(0);
}
Esempio n. 7
0
bool Physics3DCollider::init(Physics3DColliderDes *info)
{
    _physics3DShape = info->shape;
    _physics3DShape->retain();
    _btGhostObject = new btCollider(this);
    _btGhostObject->setCollisionShape(_physics3DShape->getbtShape());
    
    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;
}
Esempio n. 8
0
PhysicsInterface::CharacterControllerObject Bullet::createCharacterController(float height, float radius,
                                                                              const Entity* entity)
{
    auto ghostObject = new btPairCachingGhostObject;

    auto cylinder = new btCylinderShape({radius, height * 0.5f, radius});
    ghostObject->setCollisionShape(cylinder);
    ghostObject->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
    ghostObject->setRestitution(0.0f);

    auto stepHeight = btScalar(5.0);
    auto controller = new KinematicCharacterController(ghostObject, cylinder, stepHeight);

    dynamicsWorld_->addCollisionObject(ghostObject, btBroadphaseProxy::CharacterFilter,
                                       btBroadphaseProxy::StaticFilter | btBroadphaseProxy::DefaultFilter);

    dynamicsWorld_->addAction(controller);

    return new CharacterController(controller, ghostObject, entity);
}
Esempio n. 9
0
PhysicsInterface::BodyObject Bullet::createGeometryBodyFromTemplate(BodyTemplateObject bodyTemplateObject, float mass,
                                                                    bool fixed, const Entity* entity,
                                                                    const SimpleTransform& initialTransform)
{
    auto bodyTemplate = reinterpret_cast<BodyTemplate*>(bodyTemplateObject);

    if (!bodyTemplateObject || !bodyTemplate->collisionShape)
    {
        LOG_ERROR << "Invalid body template";
        return nullptr;
    }

    // Calculate inertia
    auto localInertia = btVector3(0.0f, 0.0f, 0.0f);
    if (!fixed)
        bodyTemplate->collisionShape->calculateLocalInertia(mass, localInertia);

    // Motion state for this body
    auto motionState = new btDefaultMotionState(toBullet(initialTransform),
                                                btTransform(btQuaternion::getIdentity(), toBullet(Vec3::Zero)));

    // Create Bullet rigid body
    auto bulletBody = new btRigidBody(
        btRigidBody::btRigidBodyConstructionInfo((fixed ? 0.0f : mass), motionState, bodyTemplate->collisionShape,
                                                 (fixed ? btVector3(0.0f, 0.0f, 0.0f) : localInertia)));

    bulletBody->setDamping(DefaultLinearDamping, DefaultAngularDamping);
    bulletBody->setSleepingThresholds(DefaultLinearSleepingThreshold, DefaultAngularSleepingThreshold);
    bulletBody->setRestitution(0.0f);

    // Add body to the simulation
    dynamicsWorld_->addRigidBody(bulletBody);

    // Create local body
    auto body = new Body(bulletBody, entity, fixed);
    bodies_.append(body);

    bulletBody->setUserPointer(body);

    return body;
}
Esempio n. 10
0
PhysicsInterface::BodyObject Bullet::createBoundingBoxBody(const AABB& aabb, float mass, bool fixed,
                                                           const Entity* entity,
                                                           const SimpleTransform& initialTransform)
{
    auto collisionShape = new btBoxShape(toBullet((aabb.getMaximum() - aabb.getMinimum()) * 0.5f));

    // Calculate inertia
    auto localInertia = btVector3(0.0f, 0.0f, 0.0f);
    if (!fixed)
        collisionShape->calculateLocalInertia(mass, localInertia);

    // Motion state for this body
    auto motionState = new btDefaultMotionState(toBullet(initialTransform),
                                                btTransform(btQuaternion::getIdentity(), toBullet(aabb.getCenter())));

    // Create Bullet rigid body
    auto bulletBody = new btRigidBody(
        btRigidBody::btRigidBodyConstructionInfo(fixed ? 0.0f : mass, motionState, collisionShape, localInertia));

    bulletBody->setDamping(DefaultLinearDamping, DefaultAngularDamping);
    bulletBody->setSleepingThresholds(DefaultLinearSleepingThreshold, DefaultAngularSleepingThreshold);
    bulletBody->setRestitution(0.0f);

    // Add body to the simulation
    dynamicsWorld_->addRigidBody(bulletBody);

    // Create local body
    auto body = new Body(bulletBody, entity, fixed);
    bodies_.append(body);

    body->ownedCollisionShape = collisionShape;

    bulletBody->setUserPointer(body);

    return body;
}
Esempio n. 11
0
Player::Player(Item* parent)
    : QBody(parent),
      m_currentPathPoint(),
      m_going(false),
      m_punchSound(std::make_shared<QSound>(":/resources/punch_sound.wav")),
      m_object(this),
      m_score() {
  setBodyType(QBody::BodyType::Dynamic);

  auto circle = std::make_unique<Box2DCircle>();
  circle->setRadius(2.5);
  circle->setFriction(0.0);
  circle->setDensity(1.0);
  circle->setPosition(QPointF(-2.5, -2.5));
  circle->setCategories(QFixture::Category2);
  circle->setCollidesWith(QFixture::Category1);
  circle->setRestitution(0.2);

  addFixture(std::move(circle));

  setPosition(QPointF(70, 950));
  setLinearDamping(5);
  setAngularDamping(5);
}
Esempio n. 12
0
void PhysicsShape::setMaterial(const PhysicsMaterial& material)
{
    setDensity(material.density);
    setRestitution(material.restitution);
    setFriction(material.friction);
}
//--------------------------------------------------------------
void ofxBulletBaseShape::setProperties(float a_restitution, float a_friction) {
	setRestitution(a_restitution);
	setFriction(a_friction);
}
Esempio n. 14
0
void Window::on_restitutionSlider_valueChanged(int value)
{
    setRestitution(MIN_RESTITUTION + (value / 100.0f) * (MAX_RESTITUTION - MIN_RESTITUTION));
}
Esempio n. 15
0
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);
	}
}
Esempio n. 16
0
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);
}