b2Fixture * Box2DMeshEntity::createFixture(bool _circle){ sweet::Box bb = mesh->calcBoundingBox(); float scaleX = 1, scaleY = 1; if(parents.size() > 0){ scaleX = firstParent()->getScaleVector().x; scaleY = firstParent()->getScaleVector().y; } b2FixtureDef d; d.density = 1; if(!_circle){ b2Vec2 verts[4]; verts[0] = b2Vec2(bb.x * scaleX, bb.y * scaleY); verts[1] = b2Vec2((bb.x + bb.width) * scaleX, bb.y * scaleY); verts[2] = b2Vec2((bb.x + bb.width) * scaleX, (bb.y + bb.height) * scaleY); verts[3] = b2Vec2(bb.x * scaleX, (bb.y + bb.height) * scaleY); b2PolygonShape t; t.Set(verts, 4); d.shape = &t; return body->CreateFixture(&d); }else{ b2CircleShape t; t.m_p.x = (bb.x + bb.width*0.5f) * scaleX; t.m_p.y = (bb.y + bb.height*0.5f) * scaleY; t.m_radius = std::max(bb.width, bb.height) * 0.5f * scaleY; d.shape = &t; return body->CreateFixture(&d); } }
void Bullet::update(Step* _step) { firstParent()->translate(0.f, dir * 2.f, 0.f); glm::vec3 worldPos = getWorldPos(); bbox.x = worldPos.x; bbox.y = worldPos.y; MeshEntity::update(_step); }
glm::mat4 Transform::getCumulativeModelMatrix(){ // if the transform has no parent, return the identity matrix if(parents.size() == 0){ return getModelMatrix(); } if(cumulativeModelMatrixDirty){ cumulativeModelMatrix = firstParent()->getCumulativeModelMatrix() * getModelMatrix(); cumulativeModelMatrixDirty = false; } return cumulativeModelMatrix; }
void MY_Scene::update(Step * _step){ bulletWorld->update(_step); glm::vec3 playerPos = ship->meshTransform->getWorldPos(); trailTimeout->update(_step); obstacleTimeout->update(_step); // update scrolling floor tex for(Vertex & v : bulletFloor->mesh->vertices){ v.v += 0.1f; //v.v += floorW; } bulletFloor->mesh->dirty = true; // clear and update trail for(unsigned long int i = trail.size(); i > 0; --i){ auto p = trail.at(i-1); if(p->meshTransform->getWorldPos().z > playerCam->childTransform->getWorldPos().z){ childTransform->removeChild(p->firstParent()); delete p->firstParent(); trail.erase(trail.begin() + (i-1)); }else{ p->body->applyCentralImpulse(btVector3(0,0,2)); } } // clear and update rings for(unsigned long int i = obstacles.size(); i > 0; --i){ auto p = obstacles.at(i-1); glm::vec3 pos = p->meshTransform->getWorldPos(); if(pos.z > playerCam->childTransform->getWorldPos().z){ childTransform->removeChild(p->firstParent()); delete p->firstParent(); obstacles.erase(obstacles.begin() + (i-1)); }else if(glm::distance2(pos, playerPos) < accuracy){ childTransform->removeChild(p->firstParent()); delete p->firstParent(); obstacles.erase(obstacles.begin() + (i-1)); score += 10; std::stringstream ss; ss << "SCORE: " << score; scoreIndicator->setText(ss.str()); scoreIndicator->invalidateLayout(); MY_ResourceManager::scenario->getAudio("BLIP")->sound->play(); }else{ p->body->translate(btVector3(0,0,1)); //p->realign(); } } if(keyboard->keyJustDown(GLFW_KEY_ESCAPE)){ game->exit(); } // player controls float playerSpeed = 30; if (keyboard->keyDown(GLFW_KEY_UP) || keyboard->keyDown(GLFW_KEY_W)){ ship->body->applyCentralImpulse(btVector3(0, playerSpeed, 0)); }if (keyboard->keyDown(GLFW_KEY_DOWN) || keyboard->keyDown(GLFW_KEY_S)){ ship->body->applyCentralImpulse(btVector3(0, -playerSpeed, 0)); }if (keyboard->keyDown(GLFW_KEY_LEFT) || keyboard->keyDown(GLFW_KEY_A)){ ship->body->applyCentralImpulse(btVector3(-playerSpeed, 0, 0)); //ship->body->applyTorqueImpulse(btVector3(0, 0, 1)); }if (keyboard->keyDown(GLFW_KEY_RIGHT) || keyboard->keyDown(GLFW_KEY_D)){ ship->body->applyCentralImpulse(btVector3(playerSpeed, 0, 0)); //ship->body->applyTorqueImpulse(btVector3(0, 0, -1)); } btVector3 vel = ship->body->getLinearVelocity(); if(playerPos.x > bounds){ if(vel.x() > 10){ ship->body->setLinearVelocity(btVector3(vel.x() * 0.1f, vel.y(), vel.z())); } }if(playerPos.x < -bounds){ if(vel.x() < -10){ ship->body->setLinearVelocity(btVector3(vel.x() * 0.1f, vel.y(), vel.z())); } }if(playerPos.y < -bounds){ if(vel.y() < -10){ ship->body->setLinearVelocity(btVector3(vel.x(), vel.y() * 0.1f, vel.z())); } }if(playerPos.y > bounds){ if(vel.y() > 10){ ship->body->setLinearVelocity(btVector3(vel.x(), vel.y() * 0.1f, vel.z())); } } shipAngle += (-vel.x()*3.f - shipAngle)*0.1f; ship->meshTransform->setOrientation(glm::angleAxis(shipAngle, glm::vec3(0,0,1))); // keep camera on player zoom += (glm::max(3.0f, vel.length()*0.5f) - zoom) * 0.2f; playerCam->firstParent()->translate(0, 1.f, zoom, false); glm::uvec2 sd = sweet::getWindowDimensions(); uiLayer.resize(0, sd.x, 0, sd.y); Scene::update(_step); uiLayer.update(_step); }
TTFB_NewsArticle::TTFB_NewsArticle(Shader * _shader, std::string _play, int _score) : NodeUI(nullptr), soundPlayed(false), scale(1.0f) { if(_score <= ONE_STAR) { _score = 1; }else if(_score > ONE_STAR && _score < THREE_STARS){ _score = 2; }else { _score = 3; } if(_play == SPAMALOT) { switch(_score) { case 1: article = TTFB_ResourceManager::scenario->getTextureSampler("spamalot_one_star")->textureSampler->texture; sound = TTFB_ResourceManager::scenario->getAudio("spamalot_one_star_sound")->sound; break; case 2: article = TTFB_ResourceManager::scenario->getTextureSampler("spamalot_two_stars")->textureSampler->texture; sound = TTFB_ResourceManager::scenario->getAudio("spamalot_two_stars_sound")->sound; break; case 3: article = TTFB_ResourceManager::scenario->getTextureSampler("spamalot_three_stars")->textureSampler->texture; sound = TTFB_ResourceManager::scenario->getAudio("spamalot_three_stars_sound")->sound; break; default: throw "Bad score"; } }else if(_play == GRINCH) { switch(_score) { case 1: article = TTFB_ResourceManager::scenario->getTextureSampler("grinch_one_star")->textureSampler->texture; sound = TTFB_ResourceManager::scenario->getAudio("grinch_one_star_sound")->sound; break; case 2: article = TTFB_ResourceManager::scenario->getTextureSampler("grinch_two_stars")->textureSampler->texture; sound = TTFB_ResourceManager::scenario->getAudio("grinch_two_stars_sound")->sound; break; case 3: article = TTFB_ResourceManager::scenario->getTextureSampler("grinch_three_stars")->textureSampler->texture; sound = TTFB_ResourceManager::scenario->getAudio("grinch_two_stars_sound")->sound; break; default: throw "Bad score"; } }else if(_play == DRACULA) { switch(_score) { case 1: article = TTFB_ResourceManager::scenario->getTextureSampler("dracula_one_star")->textureSampler->texture; sound = TTFB_ResourceManager::scenario->getAudio("dracula_one_star_sound")->sound; break; case 2: article = TTFB_ResourceManager::scenario->getTextureSampler("dracula_two_stars")->textureSampler->texture; sound = TTFB_ResourceManager::scenario->getAudio("dracula_two_stars_sound")->sound; break; case 3: article = TTFB_ResourceManager::scenario->getTextureSampler("dracula_three_stars")->textureSampler->texture; sound = TTFB_ResourceManager::scenario->getAudio("dracula_three_stars_sound")->sound; break; default: throw "Bad score"; } }else { throw "Bad play name"; } container = new Transform(); container->addChild(this); background->mesh->pushTexture2D(article); setPixelWidth(800); setPixelHeight(1000); firstParent()->translate(-350, -650, 0.f); }
void FollowCamera::update(Step * _step){ lastOrientation = childTransform->getOrientationQuat(); glm::quat newOrientation = glm::quat(1.f, 0.f, 0.f, 0.f); newOrientation = glm::rotate(newOrientation, yaw, upVectorLocal); newOrientation = glm::rotate(newOrientation, pitch, rightVectorLocal); newOrientation = glm::slerp(lastOrientation, newOrientation, 0.15f * static_cast<float>(sweet::deltaTimeCorrection)); childTransform->setOrientation(newOrientation); forwardVectorRotated = newOrientation * forwardVectorLocal; rightVectorRotated = newOrientation * rightVectorLocal; upVectorRotated = newOrientation * upVectorLocal; lookAtSpot = glm::vec3(0.f, 0.f, 0.f); float targetMinX = 9999999999.f; float targetMinY = 9999999999.f; float targetMaxX = -9999999999.f; float targetMaxY = -9999999999.f; for(signed long int i = targets.size()-1; i >= 0; --i){ if(!targets.at(i).active){ if(targets.at(i).weight <= 0.001f){ targets.erase(targets.begin() + i); } }else{ targets.at(i).pos = targets.at(i).target->getWorldPos(); } } for(Target & t : targets){ targetMinX = std::min((t.pos.x-buffer)*t.weight, targetMinX); targetMaxX = std::max((t.pos.x+buffer)*t.weight, targetMaxX); targetMinY = std::min((t.pos.y-buffer)*t.weight, targetMinY); targetMaxY = std::max((t.pos.y+buffer)*t.weight, targetMaxY); if(t.active){ t.weight = std::min(1.f, t.weight + 0.05f); }else{ t.weight = std::max(0.f, t.weight - 0.01f); } } float screenWidth = targetMaxX - targetMinX; float screenHeight = targetMaxY - targetMinY; // move camera lookAtSpot.x = targetMinX; lookAtSpot.y = targetMinY; lookAtSpot += offset; if(useBounds){ if(minBounds.height != 0){ if(lookAtSpot.y < minBounds.y){ lookAtSpot.y = minBounds.y; } if(lookAtSpot.y + screenHeight > minBounds.x + minBounds.height){ lookAtSpot.y -= (lookAtSpot.y + screenHeight - (minBounds.y + minBounds.height)); } if(lookAtSpot.y < minBounds.y){ screenHeight -= minBounds.y - lookAtSpot.y; lookAtSpot.y = minBounds.y; } } if(minBounds.width != 0){ if(lookAtSpot.x < minBounds.x){ lookAtSpot.x = minBounds.x; } if(lookAtSpot.x + screenWidth > minBounds.x + minBounds.width){ lookAtSpot.x -= (lookAtSpot.x + screenWidth - (minBounds.x + minBounds.width)); } if(lookAtSpot.x < minBounds.x){ screenWidth -= minBounds.x - lookAtSpot.x; lookAtSpot.x = minBounds.x; } } } // calculate zoom and account for FoV (the camera FoV seems to be vertical, so if the screen w > screen h, we need to take the h / the intended aspect ratio) float ar1 = screenWidth/screenHeight; glm::vec2 screenDimensions = sweet::getWindowDimensions(); float ar2 = static_cast<float>(screenDimensions.x)/static_cast<float>(screenDimensions.y); float zoom; if(ar1 > ar2){ zoom = std::max(minimumZoom, screenWidth / ar2); }else if(ar1 < ar2){ zoom = std::max(minimumZoom, screenHeight); }else{ zoom = std::max(minimumZoom, screenHeight); } lookAtSpot.x += screenWidth * 0.5f; lookAtSpot.y += screenHeight * 0.5f; lookAtSpot += offset; float dist = zoom / (tan(glm::radians(fieldOfView) * 0.5f) * 2.f); firstParent()->translate(lookAtSpot.x, lookAtSpot.y, dist, false); }
void Effector::setPos(glm::vec3 _mpos, glm::vec3 _pos) { if(active) { solver->target = glm::vec2(_pos.x, _pos.y ); firstParent()->translate(_mpos.x - 12.5, _mpos.y - 12.5f, 0.f, false); } }