Entity* Test::addFloor(World* world, Material* material, vec3 pos) { if (!gFloor) { ShapeDescription descr; descr.constructionType = ShapeConstruction::HULL_FROM_BOX; descr.hullFromBox.c = vec3(0, 0, 0); descr.hullFromBox.e = vec3(30, 1, 30); gFloor = world->createShape(descr); } ColliderDescription colliderDescr; colliderDescr.material = material; colliderDescr.transform.p = vec3(0.0f, 0.0f, 0.0f); colliderDescr.transform.q = QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f); colliderDescr.shape = gFloor; colliderDescr.isSensor = false; Collider* collider = world->createCollider(colliderDescr); BodyDescription bodyDescr; bodyDescr.type = BodyType::Static; bodyDescr.transform.p= vec3(0.0f, -2.0f, 0.0f) + pos; bodyDescr.transform.q = QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f); bodyDescr.linearMomentum = vec3(0.0f, 0.0f, 0.0f); bodyDescr.angularMomentum = vec3(0.0f, 0.0f, 0.0f); Body* body = world->createBody(bodyDescr); body->addCollider(collider); return new Entity(body, vec3(1,0,0)); }
Entity* Test::addBox(World* world, BodyDescription descr, Material* material) { if (!gBox) { ShapeDescription descr; descr.constructionType = ShapeConstruction::HULL_FROM_BOX; descr.hullFromBox.c = vec3(0, 0, 0); descr.hullFromBox.e = vec3(1, 1, 1); gBox = world->createShape(descr); } ColliderDescription colliderDescr; colliderDescr.material = material; colliderDescr.transform.p = vec3(0.0f, 0.0f, 0.0f); colliderDescr.transform.q = QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f); colliderDescr.shape = gBox; colliderDescr.isSensor = false; Collider* collider = world->createCollider(colliderDescr); //BodyDescription bodyDescr; //bodyDescr.type = BodyType::Dynamic; //bodyDescr.transform = transform; //bodyDescr.linearMomentum = vec3(0.0f, 0.0f, 0.0f); //bodyDescr.angularMomentum = vec3(0.0f, 0.0f, 0.0f); Body* body = world->createBody(descr); body->addCollider(collider); return new Entity(body, vec3(0,0,1)); }
Entity* Test::addSlope(World* world, Transform t, Material* material) { if (!gSlope) { vec3 slope[6] = { vec3(-8, -1, -4), vec3(-8, 1, -4), vec3(8, -1, -4), vec3(8, 1, -4), vec3(-8, -1, 4), vec3(8, -1, 4), }; ShapeDescription descr; descr.constructionType = ShapeConstruction::HULL_FROM_POINTS; descr.hullFromPoints.points = slope; descr.hullFromPoints.numPoints = 6; gSlope = world->createShape(descr); } ColliderDescription colliderDescr; colliderDescr.material = material; colliderDescr.transform.p = vec3(0.0f, 0.0f, 0.0f); colliderDescr.transform.q = QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f); colliderDescr.shape = gSlope; colliderDescr.isSensor = false; Collider* collider = world->createCollider(colliderDescr); BodyDescription bodyDescr; bodyDescr.type = BodyType::Static; bodyDescr.transform = t; bodyDescr.linearMomentum = vec3(0.0f, 0.0f, 0.0f); bodyDescr.angularMomentum = vec3(0.0f, 0.0f, 0.0f); Body* body = world->createBody(bodyDescr); body->addCollider(collider); return new Entity(body, vec3(1,0,0)); }
void Test::init() { m_world = new World(vec3(0.0f, -10.0f, 0.0f)); //m_world = new World(vec3(0.0f, 0.0f, 0.0f)); gBox = ShapePtr(); gCapsule = ShapePtr(); gFloor = ShapePtr(); gSlope = ShapePtr(); gSphere = ShapePtr(); Material m; m.density = 10.0f; m.friction = 1.0f; m.restitution = 0.0f; Material* material = m_world->createMaterial(m); BodyDescription descr; descr.type = BodyType::Dynamic; descr.transform = Transform(vec3(0.0f, 5.0f, -4.0f), QuatFromAxisAngle(vec3(1.0f, 2.0f, 3.0f), 1.0f)); descr.linearMomentum = vec3(0.0f, 0.0f, 0.0f); descr.angularMomentum = vec3(0.0f, 0.0f, 0.0f); m_entities.push_back(addBox(m_world, descr, material)); ShapeDescription shapeDescr; shapeDescr.shapeType = ShapeType::CAPSULE; shapeDescr.capsule.r = 0.5f; shapeDescr.capsule.c1 = vec3(0.0f, 0.5f, 0.0f); shapeDescr.capsule.c2 = vec3(0.0f, -0.5f, 0.0f); gCapsule = m_world->createShape(shapeDescr); ColliderDescription colliderDescr; colliderDescr.material = material; colliderDescr.transform = Transform(vec3(0, 0, 0), QuatFromAxisAngle(vec3(1, 0, 0), 0)); colliderDescr.shape = gCapsule; colliderDescr.isSensor = false; descr.transform.p.x += 3.0f; Collider* c = m_world->createCollider(colliderDescr); Body* b = m_world->createBody(descr); b->addCollider(c); m_entities.push_back(new Entity(b, vec3(0, 0, 1))); // spherestack { shapeDescr.shapeType = ShapeType::SPHERE; shapeDescr.sphere.r = 1.0f; shapeDescr.sphere.c = vec3(0, 0, 0); gSphere = m_world->createShape(shapeDescr); colliderDescr.shape = gSphere; descr.transform.p.x += 3.0f; Collider* c = m_world->createCollider(colliderDescr); Body* b = m_world->createBody(descr); b->addCollider(c); m_entities.push_back(new Entity(b, vec3(0, 0, 1))); descr.transform = Transform(vec3(-10.0f, 3.0f, -10.0f), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)); for (int i = 0; i < 4; ++i) { Collider* c = m_world->createCollider(colliderDescr); Body* b = m_world->createBody(descr); b->addCollider(c); m_entities.push_back(new Entity(b, vec3(0, 0, 1))); descr.transform.p.y += 3.0f; } } colliderDescr.shape = gCapsule; descr.transform = Transform(vec3(-10.0f, 3.0f, 10.0f), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)); for (int i = 0; i < 4; ++i) { Collider* c = m_world->createCollider(colliderDescr); Body* b = m_world->createBody(descr); b->addCollider(c); m_entities.push_back(new Entity(b, vec3(0, 0, 1))); descr.transform.p.y += 3.0f; } //pyramid descr.transform = Transform(vec3(10.0f, 0.0f, 0.0f), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)); m_entities.push_back(addBox(m_world, descr, material)); descr.transform = Transform(vec3(10.0f, 0.0f, -2.5f), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)); m_entities.push_back(addBox(m_world, descr, material)); descr.transform = Transform(vec3(10.0f, 0.0f, +2.5f), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)); m_entities.push_back(addBox(m_world, descr, material)); descr.transform = Transform(vec3(10.0f, 2.125f, -1.25f), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)); m_entities.push_back(addBox(m_world, descr, material)); descr.transform = Transform(vec3(10.0f, 2.125f, +1.25f), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)); m_entities.push_back(addBox(m_world, descr, material)); descr.transform = Transform(vec3(10.0f, 4.25f, 0.0f), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)); m_entities.push_back(addBox(m_world, descr, material)); descr.transform = Transform(vec3(-10.0f, 3.0f, 0.0f), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)); m_entities.push_back(addBox(m_world, descr, material)); descr.transform.p.y += 3.0; m_entities.push_back(addBox(m_world, descr, material)); descr.transform.p.y += 3.0; m_entities.push_back(addBox(m_world, descr, material)); descr.transform.p.y += 3.0; m_entities.push_back(addBox(m_world, descr, material)); descr.transform.p.y += 3.0; m_entities.push_back(addBox(m_world, descr, material)); descr.transform.p.y += 3.0; m_entities.push_back(addBox(m_world, descr, material)); descr.transform.p.y += 3.0; m_entities.push_back(addBox(m_world, descr, material)); descr.transform.p.y += 3.0; m_entities.push_back(addBox(m_world, descr, material)); descr.transform.p.y += 3.0; m_entities.push_back(addBox(m_world, descr, material)); for (int i = -1; i <= 1; ++i) { for (int j = -1; j <= 1; ++j) { m_entities.push_back(addFloor(m_world, material, vec3(i *60, 0, j*60))); } } m_entities.push_back(addSlope(m_world, Transform(vec3(0, 0, -4), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)), material)); m_entities.push_back(addSlope(m_world, Transform(vec3(0, 0, 4), QuatFromAxisAngle(vec3(0.0f, 1.0f, 0.0f), ong_PI)), material)); descr.transform = Transform(vec3(-3.0f, 5.0f, 5.0f), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)); m_entities.push_back(addBox(m_world, descr, material)); m.friction /= 2.0f; material = m_world->createMaterial(m); descr.transform = Transform(vec3(0.0f, 5.0f, 5.0f), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)); m.friction /= 2.0f; material = m_world->createMaterial(m); m_entities.push_back(addBox(m_world, descr, material)); descr.transform = Transform(vec3(3.0f, 5.0f, 5.0f), QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f)); m.friction /= 2.0f; material = m_world->createMaterial(m); m_entities.push_back(addBox(m_world, descr, material)); //player Transform t = Transform(vec3(0.0f, 8.0f, -10.0f), QuatFromAxisAngle(vec3(1, 0, 0), 0)); m_player = addPlayer(m_world, t); m_entities.push_back(m_player); static int gNumSteps = 0; int numSteps = 0; for (int i = 0; i < numSteps; ++i) { printf("%d\n", gNumSteps); m_world->step(1.0f / 60.0f); ++gNumSteps; } }
Player* Test::addPlayer(World* world, const Transform& transform) { Material m; m.friction = 0.1f; m.density = 200.0f; m.restitution = 0.5f; Material* material = world->createMaterial(m); vec3 jetHull[8] = { vec3( -0.25f, -0.25f, 0.5f), vec3( -0.25f, -0.25f, -0.5f), vec3( 0.25f, -0.25f, -0.5f), vec3( 0.25f, -0.25f, 0.5f), vec3( -0.25f, 0.25f, 0.5f), vec3( -0.25f, 0.25f, -0.5f), vec3( 0.25f, 0.25f, -0.5f), vec3( 0.25f, 0.25f, 0.5f) }; ShapeDescription shapeDescr; shapeDescr.constructionType = ShapeConstruction::HULL_FROM_BOX; shapeDescr.hullFromBox.c = vec3(0, 0, 0); shapeDescr.hullFromBox.e = vec3(0.25f, 0.25f, 0.5f); ShapePtr shape = world->createShape(shapeDescr); ColliderDescription colliderDescr; colliderDescr.material = material; colliderDescr.transform.p = vec3(0.75f, 0.0f, 0.0f); colliderDescr.transform.q = QuatFromAxisAngle(vec3(1.0f, 0.0f, 0.0f), 0.0f); colliderDescr.shape = shape; colliderDescr.isSensor = false; Collider* jet1 = world->createCollider(colliderDescr); colliderDescr.transform.p = vec3(-0.75f, 0.0f, 0.0f); Collider* jet2 = world->createCollider(colliderDescr); shapeDescr.hullFromBox.e = vec3(0.5, 0.5, 1.0f); shape = world->createShape(shapeDescr); colliderDescr.transform.p = vec3(0.0f, 0.0f, 1.0f); colliderDescr.shape = shape; Collider* bodyC = world->createCollider(colliderDescr); BodyDescription bodyDescr; bodyDescr.linearMomentum = vec3(0.0f, 0.0f, 0.0f); bodyDescr.angularMomentum = vec3(0.0f, 0.0f, 0.0f); bodyDescr.transform = transform; bodyDescr.type = BodyType::Dynamic; Body* body = world->createBody(bodyDescr); body->addCollider(jet1); body->addCollider(jet2); body->addCollider(bodyC); return new Player(body, vec3(0.0f, 1.0f, 0.0f), m_window, &m_toAdd); }
void Entity::destroy(std::vector<Entity*>& entities) { World* world = m_body->getWorld(); Collider* c = m_body->getCollider(); ShapeDescription sDescr; sDescr.constructionType = ShapeConstruction::HULL_FROM_BOX; ColliderDescription cDescr; cDescr.transform.q = Quaternion(vec3(0, 0, 0), 1); cDescr.transform.p = vec3(0, 0, 0); cDescr.isSensor = false; BodyDescription bDescr; bDescr.type = BodyType::Dynamic; bDescr.transform.q = Quaternion(vec3(0, 0, 0), 1); bDescr.linearMomentum = 0.5f*m_body->getLinearMomentum(); bDescr.angularMomentum = 0.5f*m_body->getAngularMomentum(); for (; c != 0; c = c->getNext()) { float chunkSize = 0; for (int i = 0; i < 3; ++i) chunkSize += c->getAABB().e[i]; chunkSize /= 6.0f; sDescr.hullFromBox.c = vec3(0, 0, 0); sDescr.hullFromBox.e = vec3(chunkSize, chunkSize, chunkSize); cDescr.material = c->getMaterial(); cDescr.shape = world->createShape(sDescr); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { for (int i = 0; i < 3; ++i) { bDescr.transform.p[i] = (fmodf((float)rand(), (2.0f * c->getAABB().e.x)) - c->getAABB().e[i]) * 2.0f; } bDescr.transform.p += c->getAABB().c; bDescr.transform = transformTransform(bDescr.transform, m_body->getTransform()); ARGS args; args.body = m_body; auto callback = [](Collider* collider, void* userData)->bool { if (collider->getBody() != ((ARGS*)userData)->body) ((ARGS*)userData)->valid = false; return 1; }; world->queryShape(cDescr.shape, bDescr.transform, callback, &args); if (args.valid) { Body* body = world->createBody(bDescr); body->addCollider(world->createCollider(cDescr)); entities.push_back(new Entity(body, m_color)); break; } } } } }