RagdollCharacter::RagdollCharacter(b2Vec2 _position, GameWorld* _world, ResourceManager* _resources) : GameObject(_world) { //Load the textures boxTexture = _resources->LoadTexture("Textures/Character/WHITE-BOX.png"); headHappyTexture = _resources->LoadTexture("Textures/Character/happy.png"); headSadTexture = _resources->LoadTexture("Textures/Character/surprise.png"); headTexture = _resources->LoadTexture("Textures/Character/headCentered.png"); startTime = 0.0F; contacting = false; //Create Bodies //Head b2BodyDef bd1; bd1.position.Set(0.0f/2.0f, 1.2f/2.0f); bd1.type = b2_staticBody; bodies.push_back(world->GetB2World()->CreateBody(&bd1)); b2CircleShape shape; shape.m_radius = 0.15f; b2FixtureDef fd; fd.shape = &shape; fd.density = 10.0f; GetBody(0)->CreateFixture(&fd); GetBody(0)->SetUserData( this ); //torso CreateBody(1, 0.0f + _position.x, 0.5f + _position.y, 0.07f/2.0f, 0.55f/2.0f); GetBody(1)->SetUserData( this ); //Right Leg CreateBody(2, 0.25f + _position.x, -0.4f + _position.y, 0.07f/2.0f, 0.45f/2.0f, 0.6); //Left Leg CreateBody(3, -0.25f + _position.x, -0.4f + _position.y, 0.07f/2.0f, 0.45f/2.0f, -0.6); //Left Arm CreateBody(4, -0.25f + _position.x, 0.8f + _position.y, 0.40f/2.0f, 0.07f/2.0f); //Right Arm CreateBody(5, 0.25f + _position.x, 0.8f + _position.y, 0.40f/2.0f, 0.07f/2.0f); //Create Joints //torso and right arm CreateJoint(GetBody(1), GetBody(5), 0.07f/2.0f + _position.x, 0.30f/2.0f + _position.y, -0.40f/2.0f + _position.x, 0.0f + _position.y, true, -1.3f, 0.7f); ////torso and left arm CreateJoint(GetBody(1), GetBody(4), -0.07f/2.0f + _position.x, 0.30f/2.0f + _position.y, 0.40f/2.0f + _position.x, 0.0f + _position.y, true, -0.7f, 1.3f); //torso and right leg CreateJoint(GetBody(1), GetBody(2), 0.07f/2.0f + _position.x, -0.55f/2.0f + _position.y, 0.0f + _position.x, 0.45f/2.0f + _position.y, false); //torso and left leg CreateJoint(GetBody(1), GetBody(3), -0.07f/2.0f + _position.x, -0.55f/2.0f + _position.y, 0.0f + _position.x, 0.45f/2.0f + _position.y, false); //torso and head b2WeldJointDef weldJointDef; weldJointDef.bodyA = GetBody(1); weldJointDef.bodyB = GetBody(0); weldJointDef.localAnchorA.Set(0.0f + _position.x, 0.65f/2.0f + _position.y); weldJointDef.localAnchorB.Set(0.0f + _position.x,-0.05f/2.0f + _position.y); world->GetB2World()->CreateJoint( &weldJointDef ); }
void Triangle::CreatePhysicsBody () { DestroyBody(); b2BodyDef def; def.type = b2_staticBody; m_body = CreateBody(def); b2PolygonShape shape; bool flip = m_transform.scale.x * m_transform.scale.y < 0; b2Vec2 vertices[NUM_VERTS]; vertices[0] = b2Vec2(0, 0); if (flip) { vertices[2] = b2Vec2(m_transform.scale.x, 0); vertices[1] = b2Vec2(0, m_transform.scale.y); } else { vertices[1] = b2Vec2(m_transform.scale.x, 0); vertices[2] = b2Vec2(0, m_transform.scale.y); } shape.Set(vertices, NUM_VERTS); b2FixtureDef fixtureDef; fixtureDef.shape = &shape; fixtureDef.density = 1.0f; fixtureDef.restitution = 0.0f; fixtureDef.friction = 1.0f; m_body->CreateFixture(&fixtureDef); }
HealthPackage::HealthPackage(RenderWindow* window, b2World* World, TempObjectHandler* toh, float PositionX, float PositionY):Package(window, World, toh, PositionX, PositionY){ subcaste="health"; addHeal=25; CreateBody(PositionX, PositionY); LoadImage("contents/health_pack.png",packageImg,PackageSp); PackageSp.SetCenter(packageshapeDef.vertices[2].x,packageshapeDef.vertices[2].y); }
void NodeTableControlPoint::updatePhysicBody() { #if BOX2D_INTEGRATED auto world = getWorld(); if(world) { clearPhysicsBody(); if(!IsInGame()) { b2BodyDef myBodyDef; myBodyDef.type = b2_dynamicBody;; //this will be a static body const Vecteur3& pos = getPosition(); b2Vec2 posB2; utilitaire::VEC3_TO_B2VEC(pos,posB2); myBodyDef.position.Set(posB2.x, posB2.y); //set the starting position myBodyDef.angle = 0; //set the starting angle mPhysicBody = world->CreateBody(&myBodyDef); b2CircleShape circleShape; circleShape.m_p.Set(0, 0); //position, relative to body position circleShape.m_radius = (float32)getRadius()*utilitaire::ratioWorldToBox2D; //radius b2FixtureDef myFixtureDef; myFixtureDef.shape = &circleShape; //this is a pointer to the shape above myFixtureDef.density = 1; RazerGameUtilities::ApplyFilters(myFixtureDef,RAZER_KEY_CONTROL_POINT,IsInGame()); mPhysicBody->CreateFixture(&myFixtureDef); //add a fixture to the body mPhysicBody->SetUserData(this); } } #endif }
/** * Set rotation to be free */ void PhysicsBehavior::SetFreeRotation(RuntimeScene & scene) { fixedRotation = false; if ( !body ) CreateBody(scene); body->SetFixedRotation(false); }
Fireball::Fireball(float x, float y, b2World& World, Render* renderer, float d) { world = &World; initX = x; initY = y; dir = d; std::string basepath(SDL_GetBasePath()); std::string imagePath; if (dir == 2) { imagePath = basepath + "spearL.bmp"; } else { imagePath = basepath + "spearR.bmp"; } sprite = SDL_LoadBMP(imagePath.c_str()); if (spriteRect == NULL) { spriteRect = renderer->AddSurfaceToRenderer(sprite, initX, initY, 0.5f); } else { } CreateBody(); }
/** * Add a gear joint between two objects */ void PhysicsBehavior::AddGearJointBetweenObjects( RuntimeObject * object, float ratio, RuntimeScene & scene ) { if ( !body ) CreateBody(scene); if ( object == NULL || !object->HasBehaviorNamed(name) ) return; b2Body * otherBody = static_cast<PhysicsBehavior*>(object->GetBehaviorRawPointer(name))->GetBox2DBody(scene); if ( body == otherBody ) return; //Gear joint need a revolute joint to the ground for the two objects b2RevoluteJointDef jointDef1; jointDef1.Initialize(runtimeScenesPhysicsDatas->staticBody, body, body->GetWorldCenter()); b2RevoluteJointDef jointDef2; jointDef2.Initialize(runtimeScenesPhysicsDatas->staticBody, otherBody, otherBody->GetWorldCenter()); b2GearJointDef jointDef; jointDef.bodyA = body; jointDef.bodyB = otherBody; jointDef.joint1 = runtimeScenesPhysicsDatas->world->CreateJoint(&jointDef1); jointDef.joint2 = runtimeScenesPhysicsDatas->world->CreateJoint(&jointDef2); jointDef.ratio = ratio * b2_pi; runtimeScenesPhysicsDatas->world->CreateJoint(&jointDef); }
/** * Test if there is a contact with another object */ bool PhysicsBehavior::CollisionWith( std::map <gd::String, std::vector<RuntimeObject*> *> otherObjectsLists, RuntimeScene & scene) { if ( !body ) CreateBody(scene); //Getting a list of all objects which are tested std::vector<RuntimeObject*> objects; for (std::map <gd::String, std::vector<RuntimeObject*> *>::const_iterator it = otherObjectsLists.begin();it!=otherObjectsLists.end();++it) { if ( it->second != NULL ) { objects.reserve(objects.size()+it->second->size()); std::copy(it->second->begin(), it->second->end(), std::back_inserter(objects)); } } //Test if an object of the list is in collision with our object. std::vector<RuntimeObject*>::const_iterator obj_end = objects.end(); for (std::vector<RuntimeObject*>::iterator obj = objects.begin(); obj != obj_end; ++obj ) { std::set<PhysicsBehavior*>::const_iterator it = currentContacts.begin(); std::set<PhysicsBehavior*>::const_iterator end = currentContacts.end(); for (;it != end;++it) { if ( (*it)->GetObject() == (*obj) ) return true; } } return false; }
/** * Don't consider object as bullet, for faster collision handling */ void PhysicsBehavior::DontSetAsBullet(RuntimeScene & scene) { isBullet = false; if ( !body ) CreateBody(scene); body->SetBullet(false); }
void AGameplayPawn::UpdatePos(int x, int y) { FVector2D coord(x,y); if (grid[x][y] != E_TERRAIN) { int sumAdjacent = 0; for (int i = 0; i < 8; i++) { if (grid[x + dX[i]][y + dY[i]] != E_TERRAIN) sumAdjacent++; } if (sumAdjacent == 8) { DestroyBody(x,y); } else { CreateBody(x,y); } } else { DestroyBody(x,y); } }
/** * Consider object as bullet, for better collision handling */ void PhysicsBehavior::SetAsBullet(RuntimeScene & scene) { isBullet = true; if ( !body ) CreateBody(scene); body->SetBullet(true); }
void PhysicsBehavior::SetPolygonScaleY(float scY, RuntimeScene &scene) { polygonScaleY = scY; runtimeScenesPhysicsDatas->world->DestroyBody(body); CreateBody(scene); }
/** * Set rotation to be fixed */ void PhysicsBehavior::SetFixedRotation(RuntimeScene & scene) { fixedRotation = true; if ( !body ) CreateBody(scene); body->SetFixedRotation(true); }
/** * Set a body to be static */ void PhysicsBehavior::SetStatic(RuntimeScene & scene) { dynamic = false; if ( !body ) CreateBody(scene); body->SetType(b2_staticBody); }
b2World::b2World(const b2AABB& worldAABB, const b2Vec2& gravity, bool doSleep) { m_listener = NULL; m_filter = &b2_defaultFilter; m_bodyList = NULL; m_contactList = NULL; m_jointList = NULL; m_bodyCount = 0; m_contactCount = 0; m_jointCount = 0; m_bodyDestroyList = NULL; m_allowSleep = doSleep; m_gravity = gravity; m_contactManager.m_world = this; void* mem = b2Alloc(sizeof(b2BroadPhase)); m_broadPhase = new (mem) b2BroadPhase(worldAABB, &m_contactManager); b2BodyDef bd; m_groundBody = CreateBody(&bd); }
void RigidBodyData::Load(ILoad* const iload) { ULONG nwrit; int revision; dVector veloc; dVector omega; dMatrix matrix; RigidBodyWorldDesc& me = *(RigidBodyWorldDesc*) RigidBodyWorldDesc::GetDescriptor(); iload->Read((const char*)&revision, sizeof (revision), &nwrit); iload->Read((const char*)&m_oldControlerID, sizeof (m_oldControlerID), &nwrit); iload->Read((const char*)&m_collisionShape, sizeof (m_collisionShape), &nwrit); iload->Read((const char*)&m_hideGizmos, sizeof (m_hideGizmos), &nwrit); iload->Read((const char*)&m_mass, sizeof (m_mass), &nwrit); iload->Read((const char*)&m_inertia, sizeof (m_inertia), &nwrit); iload->Read((const char*)&m_origin, sizeof (m_origin), &nwrit); iload->Read((const char*)&matrix, sizeof (matrix), &nwrit); iload->Read((const char*)&veloc, sizeof (veloc), &nwrit); iload->Read((const char*)&omega, sizeof (omega), &nwrit); NewtonCollision* const collision = NewtonCreateCollisionFromSerialization (me.m_newton, LoadCollision, iload); CreateBody(collision, veloc, omega); NewtonBodySetMatrix(m_body, &matrix[0][0]); NewtonDestroyCollision(collision); }
b2World::b2World(const b2AABB& worldAABB, const b2Vec2& gravity, bool doSleep) { m_destructionListener = NULL; m_boundaryListener = NULL; m_contactFilter = &b2_defaultFilter; m_contactListener = NULL; m_debugDraw = NULL; m_bodyList = NULL; m_contactList = NULL; m_jointList = NULL; m_controllerList = NULL; m_bodyCount = 0; m_contactCount = 0; m_jointCount = 0; m_controllerCount = 0; m_warmStarting = true; m_continuousPhysics = true; m_allowSleep = doSleep; m_gravity = gravity; m_lock = false; m_inv_dt0 = 0.0f; m_contactManager.m_world = this; void* mem = b2Alloc(sizeof(b2BroadPhase)); m_broadPhase = new (mem) b2BroadPhase(worldAABB, &m_contactManager); b2BodyDef bd; m_groundBody = CreateBody(&bd); }
void PhysicsBehavior::SetLinearVelocityY( double yVelocity, RuntimeScene & scene ) { if ( !body ) CreateBody(scene); body->SetLinearVelocity(b2Vec2(body->GetLinearVelocity().x, -yVelocity)); }
Entity* EntityFactory::CreateEntity(float x, float y, float height, float width, EntityType type) { Entity* ent = entityRegistery.at(type)->EmptyClone(); ent->Init(CreateBody(x, y, height, width, type), width, height, type, bf, drawContainer); return ent; }
/** * Set a body to be dynamic */ void PhysicsBehavior::SetDynamic(RuntimeScene & scene) { dynamic = true; if ( !body ) CreateBody(scene); body->SetType(b2_dynamicBody); body->SetAwake(true); }
/** * Apply a force */ void PhysicsBehavior::ApplyForceTowardPosition(float xPosition, float yPosition, float length, RuntimeScene & scene ) { if ( !body ) CreateBody(scene); float angle = atan2(yPosition*runtimeScenesPhysicsDatas->GetInvScaleY()+body->GetPosition().y, xPosition*runtimeScenesPhysicsDatas->GetInvScaleX()-body->GetPosition().x); body->ApplyForce(b2Vec2(cos(angle)*length, -sin(angle)*length), body->GetPosition()); }
/** * Add an hinge to an object */ void PhysicsBehavior::AddRevoluteJoint( float xPosition, float yPosition, RuntimeScene & scene) { if ( !body ) CreateBody(scene); b2RevoluteJointDef jointDef; jointDef.Initialize(body, runtimeScenesPhysicsDatas->staticBody, b2Vec2( xPosition*runtimeScenesPhysicsDatas->GetInvScaleX(), -yPosition*runtimeScenesPhysicsDatas->GetInvScaleY())); runtimeScenesPhysicsDatas->world->CreateJoint(&jointDef); }
void CHangingLamp::SpawnInitPhysics (CSE_Abstract *D) { CSE_ALifeObjectHangingLamp *lamp = smart_cast<CSE_ALifeObjectHangingLamp*>(D); if (lamp->flags.is(CSE_ALifeObjectHangingLamp::flPhysic)) CreateBody(lamp); if (smart_cast<CKinematics*>(Visual())){ smart_cast<CKinematics*> (Visual())->CalculateBones_Invalidate (); smart_cast<CKinematics*> (Visual())->CalculateBones(); //.intepolate_pos } }
void RigidBody::ResetForces() { // Cannot modify server-authoritative physics object if (!HasAuthority()) return; if (!impl->body) CreateBody(); if (impl->body) impl->body->clearForces(); }
void dgWorld::AddSentinelBody() { dgCollision* collision; collision = new (m_allocator) dgCollisionNull (m_allocator, 0x4352fe67); m_sentionelBody = CreateBody(collision, dgGetIdentityMatrix()); ReleaseCollision(collision); // dgBodyMasterList::m_sentinel = m_sentionelBody; dgCollidingPairCollector::m_sentinel = m_sentionelBody; }
void RigidBody::Activate() { // Cannot modify server-authoritative physics object if (!HasAuthority()) return; if (!impl->body) CreateBody(); if (impl->body) impl->body->activate(); }
void AttractToCenter(std::vector<Body*> pBodies, float width, float height, float centerMass) { Body* Temp = CreateBody(width / 2, height / 2, centerMass); //Create a body at the center of the simulation for (unsigned int i = 0; i < pBodies.size(); i++) { CalculateForce(pBodies[i], Temp, Softener); } delete Temp; }
void RigidBody2D::OnNodeSet(Node* node) { Component::OnNodeSet(node); if (node) { node->AddListener(this); Scene* scene = GetScene(); physicsWorld_ = scene->GetOrCreateComponent<PhysicsWorld2D>(); CreateBody(); physicsWorld_->AddRigidBody(this); } }
/** * Called at each frame after events : * Update Box2D body if necessary */ void PhysicsBehavior::DoStepPostEvents(RuntimeScene & scene) { if ( !body ) CreateBody(scene); //Note: Strange bug here, using SpriteObject, the tests objectOldWidth != newWidth //and objectOldHeight != newHeight keeps being true even if the two values were exactly the same. //Maybe a floating point round error ( the values were integer yet in my tests! ) so we cast the values //to int to ensure that the body is not continuously recreated. float newWidth = object->GetWidth(); float newHeight = object->GetHeight(); if ( (int)objectOldWidth != (int)newWidth || (int)objectOldHeight != (int)newHeight ) { /*std::cout << "Changed:" << (int)objectOldWidth << "!=" << (int)newWidth << std::endl; std::cout << "Changed:" << (int)objectOldHeight << "!=" << (int)newHeight << std::endl; std::cout << "( Object name:" << object->GetName() << std::endl;*/ double oldAngularVelocity = body->GetAngularVelocity(); b2Vec2 oldVelocity = body->GetLinearVelocity(); runtimeScenesPhysicsDatas->world->DestroyBody(body); CreateBody(scene); body->SetAngularVelocity(oldAngularVelocity); body->SetLinearVelocity(oldVelocity); } runtimeScenesPhysicsDatas->stepped = false; //Prepare for a new simulation if ( objectOldX == object->GetX() && objectOldY == object->GetY() && objectOldAngle == object->GetAngle()) return; b2Vec2 oldPos; oldPos.x = (object->GetDrawableX()+object->GetWidth()/2)*runtimeScenesPhysicsDatas->GetInvScaleX(); oldPos.y = -(object->GetDrawableY()+object->GetHeight()/2)*runtimeScenesPhysicsDatas->GetInvScaleY(); //Y axis is inverted body->SetTransform(oldPos, -object->GetAngle()*b2_pi/180.0f); //Angles are inverted body->SetAwake(true); }
/** * Add an hinge between two objects */ void PhysicsBehavior::AddRevoluteJointBetweenObjects( RuntimeObject * object, RuntimeScene & scene, float xPosRelativeToMassCenter, float yPosRelativeToMassCenter ) { if ( !body ) CreateBody(scene); if ( object == NULL || !object->HasBehaviorNamed(name) ) return; b2Body * otherBody = static_cast<PhysicsBehavior*>(object->GetBehaviorRawPointer(name))->GetBox2DBody(scene); if ( body == otherBody ) return; b2RevoluteJointDef jointDef; jointDef.Initialize(body, otherBody, body->GetWorldCenter()+b2Vec2(xPosRelativeToMassCenter*runtimeScenesPhysicsDatas->GetInvScaleX(), yPosRelativeToMassCenter*runtimeScenesPhysicsDatas->GetInvScaleY())); runtimeScenesPhysicsDatas->world->CreateJoint(&jointDef); }