Exemple #1
0
void Package::update (uint32_t deltaTime)
{
	CollectableEntity::update(deltaTime);
	if (_addRopeJointTo) {
		addRopeJoint(_addRopeJointTo);
		_addRopeJointTo = nullptr;
	}

	if ((_arrived || _delivered || isDestroyed()) && _ropeJoint) {
		removeRopeJoint();
	}

	if (isCollected()) {
		setLinearDamping(0.0f);
		setAngularDamping(0.0);
	}

	if (!_target) {
		_target = _map.getPackageTarget();
		// if we can't find a package target, we will destroy the package
		if (!_target)
			setDestroyed();
	}

	if (!isCounted() && isDelivered()) {
		_map.countTransferedPackage();
		setCounted();
	}
}
Exemple #2
0
void Package::endContact (b2Contact* contact, IEntity* entity)
{
	CollectableEntity::endContact(contact, entity);

	if (entity->isSolid() || entity->isPackage() || entity->isStone() || entity->isBorder()) {
		setLinearDamping(0.0f);
		setAngularDamping(0.0);
	}
}
Exemple #3
0
void Package::onContact (b2Contact* contact, IEntity* entity)
{
	const bool oldCollectedState = isCollected();
	CollectableEntity::onContact(contact, entity);
	if (!oldCollectedState && isCollected()) {
		_addRopeJointTo = entity;
	} else {
		if (entity->isSolid() || entity->isStone() || entity->isPackage()) {
			setLinearDamping(3.0f);
			setAngularDamping(1.0);
			if (isImpactVelocityMoreThan(contact, 1.5f))
				_map.sendSound(getVisMask(), SoundTypes::SOUND_PACKAGE_COLLIDE, getPos());
		} else if (entity->isBorder()) {
			const Border *b = assert_cast<const Border*, const IEntity*>(entity);
			if (!b->isTop()) {
				setLinearDamping(4.0f);
				setAngularDamping(1.0);
			}
		} else if (entity->isNpcAttacking()) {
			setDestroyed(true);
		}
	}
}
Exemple #4
0
cocos2d::PhysicsBody* RubeBody::createPhysicsBody() {
    if (0 == fixtures.size()) {
        return nullptr;
    }
    auto pb = cocos2d::PhysicsBody::create();
    pb->setRotationEnable(this->getRotationEnable());
    if (this->getBodyType() == 0) { //0 = static, 1 = kinematic, 2 = dynamic
        pb->setDynamic(false);
    }
    for (auto fixture: fixtures) {
        auto shape = fixture->getShape(this->scale);
        pb->addShape(shape);
    }
    pb->setLinearDamping(this->getLinearDamping());
    pb->setAngularDamping(this->getAngularDamping());
    //pb->setCollisionBitmask(1);
    //pb->setCategoryBitmask(1);
    
    return pb;
}
Exemple #5
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);
}
void CollisionMesh::loadImpl (void)
{
    APP_ASSERT(masterShape==NULL);

    Ogre::DataStreamPtr file;
    try {
        file = Ogre::ResourceGroupManager::getSingleton().openResource(name.substr(1), "GRIT");
    } catch (Ogre::Exception &e) {
        GRIT_EXCEPT(e.getDescription());
    }

    std::string ext = name.substr(name.length()-5);

    uint32_t fourcc = 0;
    for (int i=0 ; i<4 ; ++i) {
        unsigned char c;
        file->read(&c, 1);
        fourcc |= c << (i*8);
    }
    file->seek(0);

    std::string dir = grit_dirname(name);

    const btVector3 ZV(0,0,0);
    const btQuaternion ZQ(0,0,0,1);

    bool compute_inertia = false;
    bool is_static = false;

    if (fourcc==0x4c4f4342) { //BCOL

        Ogre::MemoryDataStreamPtr mem = 
            Ogre::MemoryDataStreamPtr(OGRE_NEW Ogre::MemoryDataStream(name,file));

        BColFile &bcol = *reinterpret_cast<BColFile*>(mem->getPtr());

        is_static = bcol.mass == 0.0f; // static

        masterShape = new btCompoundShape();

        BColMaterialMap mmap(dir,name);

        for (unsigned i=0 ; i<bcol.hullNum ; ++i) {
            BColHull &p = *bcol.hulls(i);
            btConvexHullShape *s2 = new btConvexHullShape();
            s2->setMargin(p.margin);
            for (unsigned j=0 ; j<p.vertNum ; ++j) {
                BColVert &v = *p.verts(j);
                s2->addPoint(btVector3(v.x, v.y, v.z));
            }
            masterShape->addChildShape(btTransform(ZQ,ZV), s2);
            partMaterials.push_back(mmap(p.mat.name()));
        }

        for (unsigned i=0 ; i<bcol.boxNum ; ++i) {
            BColBox &p = *bcol.boxes(i);
            btBoxShape *s2 = new btBoxShape(btVector3(p.dx/2,p.dy/2,p.dz/2));
            s2->setMargin(p.margin);
            masterShape->addChildShape(btTransform(btQuaternion(p.qx,p.qy,p.qz,p.qw),
                                                   btVector3(p.px,p.py,p.pz)), s2);
            partMaterials.push_back(mmap(p.mat.name()));
        }

        for (unsigned i=0 ; i<bcol.cylNum ; ++i) {
            BColCyl &p = *bcol.cyls(i);
            btCylinderShape *s2 = new btCylinderShapeZ(btVector3(p.dx/2,p.dy/2,p.dz/2));
            s2->setMargin(p.margin);
            masterShape->addChildShape(btTransform(btQuaternion(p.qx,p.qy,p.qz,p.qw),
                                                   btVector3(p.px,p.py,p.pz)), s2);
            partMaterials.push_back(mmap(p.mat.name()));
        }

        for (unsigned i=0 ; i<bcol.coneNum ; ++i) {
            BColCone &p = *bcol.cones(i);
            btConeShape *s2 = new btConeShapeZ(p.radius,p.height);
            s2->setMargin(p.margin);
            masterShape->addChildShape(btTransform(btQuaternion(p.qx,p.qy,p.qz,p.qw),
                                                   btVector3(p.px,p.py,p.pz)), s2);
            partMaterials.push_back(mmap(p.mat.name()));
        }

        for (unsigned i=0 ; i<bcol.planeNum ; ++i) {
            BColPlane &p = *bcol.planes(i);
            btStaticPlaneShape *s2 = new btStaticPlaneShape(btVector3(p.nx,p.ny,p.nz),p.d);
            masterShape->addChildShape(btTransform(ZQ,ZV), s2);
            partMaterials.push_back(mmap(p.mat.name()));
        }

        for (unsigned i=0 ; i<bcol.sphereNum ; ++i) {
            BColSphere &p = *bcol.spheres(i);
            btSphereShape *s2 = new btSphereShape(p.radius);
            masterShape->addChildShape(btTransform(ZQ, btVector3(p.px,p.py,p.pz)), s2);
            partMaterials.push_back(mmap(p.mat.name()));
        }


        if (bcol.triMeshFaceNum > 0) {

            bcolVerts.resize(bcol.triMeshVertNum);
            bcolFaces.resize(bcol.triMeshFaceNum);

            memcpy(&bcolVerts[0], bcol.triMeshVerts(0), bcol.triMeshVertNum * sizeof(BColVert));
            memcpy(&bcolFaces[0], bcol.triMeshFaces(0), bcol.triMeshFaceNum * sizeof(BColFace));

            faceMaterials.reserve(bcol.triMeshFaceNum);

            int counter = 0;
            float accum_area = 0;
            for (unsigned i=0 ; i<bcol.triMeshFaceNum ; ++i) {
                BColFace &face = *bcol.triMeshFaces(i);
                PhysicalMaterial *mat = mmap(face.mat.name());
                faceMaterials.push_back(mat);
                CollisionMesh::ProcObjFace po_face(to_v3(bcolVerts[face.v1]), 
                                                   to_v3(bcolVerts[face.v2]),
                                                   to_v3(bcolVerts[face.v3]));
                procObjFaceDB[mat->id].faces.push_back(po_face);
                float area = (po_face.AB.cross(po_face.AC)).length();
                APP_ASSERT(area>=0);
                procObjFaceDB[mat->id].areas.push_back(area);
                procObjFaceDB[mat->id].totalArea += area;
                if (++counter = 10) {
                    counter = 0;
                    accum_area = 0;
                    procObjFaceDB[mat->id].areas10.push_back(accum_area);
                }
                accum_area += area;
                    
            }

            btTriangleIndexVertexArray *v = new btTriangleIndexVertexArray(
                bcolFaces.size(), reinterpret_cast<int*>(&(bcolFaces[0].v1)), sizeof(BColFace),
                bcolVerts.size(), &(bcolVerts[0].x), sizeof(BColVert));


            if (is_static) {
                btBvhTriangleMeshShape *tm = new btBvhTriangleMeshShape(v,true,true);
                tm->setMargin(bcol.triMeshMargin);
                btTriangleInfoMap* tri_info_map = new btTriangleInfoMap();
                tri_info_map->m_edgeDistanceThreshold = bcol.triMeshEdgeDistanceThreshold;

                btGenerateInternalEdgeInfo(tm,tri_info_map);
                masterShape->addChildShape(btTransform::getIdentity(), tm);
            } else {
                // skip over dynamic trimesh
            }
        }

        setMass(bcol.mass);
        setLinearDamping(bcol.linearDamping);
        setAngularDamping(bcol.angularDamping);
        setLinearSleepThreshold(bcol.linearSleepThreshold);
        setAngularSleepThreshold(bcol.angularSleepThreshold);
        setCCDMotionThreshold(bcol.ccdMotionThreshold);
        setCCDSweptSphereRadius(bcol.ccdSweptSphereRadius);
        setInertia(Vector3(bcol.inertia[0],bcol.inertia[1],bcol.inertia[2]));

        compute_inertia = !bcol.inertiaProvided;

    } else if (fourcc==0x4c4f4354) { //TCOL

        ProxyStreamBuf proxy(file);

        std::istream stream(&proxy);
        quex::tcol_lexer qlex(&stream);
        TColFile tcol;
        parse_tcol_1_0(name,&qlex,tcol);

        is_static = tcol.mass == 0.0f; // static

        masterShape = new btCompoundShape();

        if (tcol.usingCompound) {

            TColCompound &c = tcol.compound;

            for (size_t i=0 ; i<c.hulls.size() ; ++i) {
                const TColHull &h = c.hulls[i];
                btConvexHullShape *s2 = new btConvexHullShape();
                s2->setMargin(h.margin);
                for (unsigned j=0 ; j<h.vertexes.size() ; ++j) {
                    const Vector3 &v = h.vertexes[j];
                    s2->addPoint(to_bullet(v));
                }
                masterShape->addChildShape(btTransform(ZQ,ZV), s2);
                partMaterials.push_back(phys_mats.getMaterial(dir,name,h.material));
            }

            for (size_t i=0 ; i<c.boxes.size() ; ++i) {
                const TColBox &b = c.boxes[i];
                /* implement with hulls
                btConvexHullShape *s2 = new btConvexHullShape();
                s2->addPoint(btVector3(-b.dx/2+b.margin, -b.dy/2+b.margin, -b.dz/2+b.margin));
                s2->addPoint(btVector3(-b.dx/2+b.margin, -b.dy/2+b.margin,  b.dz/2-b.margin));
                s2->addPoint(btVector3(-b.dx/2+b.margin,  b.dy/2-b.margin, -b.dz/2+b.margin));
                s2->addPoint(btVector3(-b.dx/2+b.margin,  b.dy/2-b.margin,  b.dz/2-b.margin));
                s2->addPoint(btVector3( b.dx/2-b.margin, -b.dy/2+b.margin, -b.dz/2+b.margin));
                s2->addPoint(btVector3( b.dx/2-b.margin, -b.dy/2+b.margin,  b.dz/2-b.margin));
                s2->addPoint(btVector3( b.dx/2-b.margin,  b.dy/2-b.margin, -b.dz/2+b.margin));
                s2->addPoint(btVector3( b.dx/2-b.margin,  b.dy/2-b.margin,  b.dz/2-b.margin));
                */
                btBoxShape *s2 =new btBoxShape(btVector3(b.dx/2,b.dy/2,b.dz/2));
                s2->setMargin(b.margin);
                masterShape->addChildShape(btTransform(btQuaternion(b.qx,b.qy,b.qz,b.qw),
                                 btVector3(b.px,b.py,b.pz)), s2);
                partMaterials.push_back(phys_mats.getMaterial(dir,name,b.material));
            }

            for (size_t i=0 ; i<c.cylinders.size() ; ++i) {
                const TColCylinder &cyl = c.cylinders[i];
                btCylinderShape *s2 =
                    new btCylinderShapeZ(btVector3(cyl.dx/2,cyl.dy/2,cyl.dz/2));
                s2->setMargin(cyl.margin);
                masterShape->addChildShape(
                    btTransform(btQuaternion(cyl.qx,cyl.qy,cyl.qz,cyl.qw),
                            btVector3(cyl.px,cyl.py,cyl.pz)), s2);
                partMaterials.push_back(phys_mats.getMaterial(dir,name,cyl.material));
            }

            for (size_t i=0 ; i<c.cones.size() ; ++i) {
                const TColCone &cone = c.cones[i];
                btConeShapeZ *s2 = new btConeShapeZ(cone.radius,cone.height);
                s2->setMargin(cone.margin);
                masterShape->addChildShape(
                      btTransform(btQuaternion(cone.qx,cone.qy,cone.qz,cone.qw),
                          btVector3(cone.px,cone.py,cone.pz)), s2);
                partMaterials.push_back(phys_mats.getMaterial(dir,name,cone.material));
            }

            for (size_t i=0 ; i<c.planes.size() ; ++i) {
                const TColPlane &p = c.planes[i];
                btStaticPlaneShape *s2 =
                    new btStaticPlaneShape(btVector3(p.nx,p.ny,p.nz),p.d);
                masterShape->addChildShape(btTransform(ZQ,ZV), s2);
                partMaterials.push_back(phys_mats.getMaterial(dir,name,p.material));
            }

            for (size_t i=0 ; i<c.spheres.size() ; ++i) {
                const TColSphere &sp = c.spheres[i];
                btSphereShape *s2 = new btSphereShape(sp.radius);
                masterShape->addChildShape(btTransform(ZQ,
                                 btVector3(sp.px,sp.py,sp.pz)), s2);
                partMaterials.push_back(phys_mats.getMaterial(dir,name,sp.material));
            }
        }

        if (tcol.usingTriMesh) {

            TColTriMesh &t = tcol.triMesh;

            std::swap(verts, t.vertexes);
            std::swap(faces, t.faces);


            faceMaterials.reserve(faces.size());
            int counter = 0;
            float accum_area = 0;
            for (TColFaces::const_iterator i=faces.begin(), i_=faces.end() ; i!=i_ ; ++i) {
                //optimisation possible here by changing the TCol struct to be more liek what
                //bullet wants, and then re-using memory
                PhysicalMaterial *mat = phys_mats.getMaterial(dir,name,i->material);
                faceMaterials.push_back(mat);
                CollisionMesh::ProcObjFace po_face(verts[i->v1], verts[i->v2], verts[i->v3]);
                procObjFaceDB[mat->id].faces.push_back(po_face);
                float area = (po_face.AB.cross(po_face.AC)).length();
                APP_ASSERT(area>=0);
                procObjFaceDB[mat->id].areas.push_back(area);
                procObjFaceDB[mat->id].totalArea += area;
                if (++counter = 10) {
                    counter = 0;
                    accum_area = 0;
                    procObjFaceDB[mat->id].areas10.push_back(accum_area);
                }
                accum_area += area;
                    
            }

            btTriangleIndexVertexArray *v = new btTriangleIndexVertexArray(
                faces.size(), &(faces[0].v1), sizeof(TColFace),
                verts.size(), &(verts[0].x), sizeof(Vector3));

            if (is_static) {
                btBvhTriangleMeshShape *tm = new btBvhTriangleMeshShape(v,true,true);
                tm->setMargin(t.margin);
                btTriangleInfoMap* tri_info_map = new btTriangleInfoMap();
                tri_info_map->m_edgeDistanceThreshold = t.edgeDistanceThreshold;

                btGenerateInternalEdgeInfo(tm,tri_info_map);
                masterShape->addChildShape(btTransform::getIdentity(), tm);
            } else {
                // Skip over dynamic trimesh
            }

        }

        setMass(tcol.mass);
        setInertia(Vector3(tcol.inertia_x,tcol.inertia_y,tcol.inertia_z));
        setLinearDamping(tcol.linearDamping);
        setAngularDamping(tcol.angularDamping);
        setLinearSleepThreshold(tcol.linearSleepThreshold);
        setAngularSleepThreshold(tcol.angularSleepThreshold);
        setCCDMotionThreshold(tcol.ccdMotionThreshold);
        setCCDSweptSphereRadius(tcol.ccdSweptSphereRadius);

        compute_inertia = !tcol.hasInertia;

    } else {
        GRIT_EXCEPT("Collision mesh \""+name+"\" seems to be corrupt.");
    }


    if (is_static) {
        setInertia(Vector3(0,0,0));
    } else {
        if (faceMaterials.size() > 0) {
            CERR << "While loading \"" + name + "\": Dynamic trimesh not supported." << std::endl;
        }
        if (compute_inertia) {
            btVector3 i;
            masterShape->calculateLocalInertia(mass,i);
            setInertia(from_bullet(i));
        }
    }
}
void PhysicsBody::setDamping (Real32 linear_scale, Real32 angular_scale)
{
    setLinearDamping(linear_scale);
    setAngularDamping(angular_scale);
}
//Damping
void PhysicsBody::setDampingDefaults (void)
{
    dBodySetDampingDefaults(_BodyID);
    setLinearDamping(dBodyGetLinearDamping(_BodyID));
    setAngularDamping(dBodyGetAngularDamping(_BodyID));
}
Exemple #9
0
void CIrrOdeWorld::initPhysics() {
    if (m_bPhysicsInitialized) return;
    m_iNodesInitialized=0;
    CIrrOdeSceneNode::initPhysics();

    m_iWorldId=m_pOdeDevice->worldCreate();
    m_iJointGroupId=m_pOdeDevice->jointGroupCreate(0);

    if (!m_pWorldSpace) m_pWorldSpace=new CIrrOdeSpace(this,m_pSceneManager);
    if (m_pWorldSpace!=NULL) m_pWorldSpace->initPhysics();

    setLinearDamping          (m_fDampingLinear          );
    setAngularDamping         (m_fDampingAngular         );
    setLinearDampingThreshold (m_fDampingLinearThreshold );
    setAngularDampingThreshold(m_fDampingAngularThreshold);

    setAutoDisableFlag            (m_iAutoDisableFlag            );
    setAutoDisableLinearThreshold (m_fAutoDisableLinearThreshold );
    setAutoDisableAngularThreshold(m_fAutoDisableAngularThreshold);
    setAutoDisableSteps           (m_iAutoDisableSteps           );
    setAutoDisableTime            (m_fAutoDisableTime            );

    setGravity(m_cGravity);

    if (m_fMaxAngularSpeed!=_DEFAULT_MAX_ANGULAR_SPEED) setMaxAngularSpeed(m_fMaxAngularSpeed);

#ifdef _TRACE_INIT_PHYSICS
    printf("world created .. id=%i\n",(int)m_iWorldId);
#endif

    irr::core::list<CIrrOdeSpace *>::Iterator s;
    for (s=m_pSpaces.begin(); s!=m_pSpaces.end(); s++) {
#ifdef _TRACE_INIT_PHYSICS
        printf("CIrrOdeWorld::initPhysics: init space\n");
#endif
        CIrrOdeSpace *pSpace=(*s);
        pSpace->initPhysics();
    }

    irr::core::list<CIrrOdeGeom *>::Iterator i;

    for (i=m_pGeoms.begin(); i!=m_pGeoms.end(); i++) {
#ifdef _TRACE_INIT_PHYSICS
        printf("CIrrOdeWorld::initPhysics: init geom\n");
#endif
        CIrrOdeGeom *pGeom=(*i);
        pGeom->initPhysics();
    }

    irr::core::list<CIrrOdeBody *>::Iterator b,ib2;
    for (b=m_pBodies.begin(); b!=m_pBodies.end(); b++) {
#ifdef _TRACE_INIT_PHYSICS
        printf("CIrrOdeWorld::initPhysics: init body\n");
#endif
        CIrrOdeBody *b1=*b;
        b1->initPhysics();
    }

    irr::core::list<irr::ode::IIrrOdeStepMotor *>::Iterator it;
    for (it=m_lStepMotors.begin(); it!=m_lStepMotors.end(); it++) (*it)->initPhysics();

    m_bPhysicsInitialized = true;
}