void AxonometricLookAt::rebuild() const { if(!m_rebuild) return; const mat4 vrot = mat4_cast(angleAxis(m_verticalAngle, vec3( 0.f, 1.f, 0.f))); const mat4 hrot = mat4_cast(angleAxis(m_horizontalAngle, vec3( 1.f, 0.f, 0.f))); const mat4 zoom = scale(m_zoom, m_zoom, -1.f); const mat4 t = translate(m_position); m_axonometric = zoom * t * hrot * vrot * m_rotation; m_rebuild = false; }
void Lux::Core::Transform::Rotate(const float a_Angle, const vec3& a_Axis, SpaceRelativeTo a_Space) { switch (a_Space) { case SELF: m_LocalRotation = angleAxis(radians(a_Angle), normalize(a_Axis)) * m_LocalRotation; break; case WORLD: m_Rotation = angleAxis(radians(a_Angle), normalize(a_Axis)) * m_Rotation; break; } m_TransformDirty = true; }
void Lux::Core::FreeLookCamera::Update(const float a_DeltaTime) { vec2 currMousePos = m_EventListener->GetCursorPosition(); if (m_EventListener->GetMouseButtonDown(LUX_MOUSE_BUTTON_RIGHT)) { vec2 mouseDelta = currMousePos - m_LastMousePos; m_EulerRotation.x = clamp<float>(m_EulerRotation.x + mouseDelta.y * m_RotationMultiplier * a_DeltaTime, radians(-80.0f), radians(80.0f)); m_EulerRotation.y += mouseDelta.x * m_RotationMultiplier * a_DeltaTime; m_EulerRotation.z = 0.0f; m_Transform->GetRawPtr()->SetLocalRotation(angleAxis(m_EulerRotation.x, vec3(1, 0, 0)) * angleAxis(m_EulerRotation.y, vec3(0, 1, 0)) * angleAxis(m_EulerRotation.z, vec3(0, 0, 1))); } if (m_EventListener->GetKeyDown(KeyCode::LUX_KEY_W)) { m_Transform->GetRawPtr()->Translate(m_Transform->GetRawPtr()->GetForwardVector() * m_SpeedMultiplier * a_DeltaTime); } if (m_EventListener->GetKeyDown(KeyCode::LUX_KEY_S)) { m_Transform->GetRawPtr()->Translate(-m_Transform->GetRawPtr()->GetForwardVector() * m_SpeedMultiplier * a_DeltaTime); } if (m_EventListener->GetKeyDown(KeyCode::LUX_KEY_A)) { m_Transform->GetRawPtr()->Translate(-m_Transform->GetRawPtr()->GetRightVector() * m_SpeedMultiplier * a_DeltaTime); } if (m_EventListener->GetKeyDown(KeyCode::LUX_KEY_D)) { m_Transform->GetRawPtr()->Translate(m_Transform->GetRawPtr()->GetRightVector() * m_SpeedMultiplier * a_DeltaTime); } m_LastMousePos = currMousePos; }
void Rotate::update(float deltaTime, float totalTime) { if (Input::wasControlPressed("randomize")) axis = RAND_VEC3; t->rotate(angleAxis(speed * deltaTime, axis)); }
/// \brief Log: SO3 -> so3. /// /// Pseudo-inverse of log from SO3 -> { v \in so3, ||v|| < 2pi }. template <typename D> Eigen::Matrix<typename D::Scalar,3,1,D::Options> log3(const Eigen::MatrixBase<D> & R) { EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(D, 3, 3); Eigen::AngleAxis<typename D::Scalar> angleAxis(R); return angleAxis.axis() * angleAxis.angle(); }
void Aircraft::applyForces(float dt) { vec3 netF(0, 0, 0); netF = netF + fGravity(); netF = netF + fWing(); netF = netF + fThrust(); netF = netF + fDrag(); float rollA = tAileron() / rollmoi; float pitchA = tElevator() / pitchmoi; float yawA = tRudder() / yawmoi; vec3 accel = netF / mass; this->pos = this->pos + this->velocity * dt + accel * (0.5f * dt * dt); this->velocity = this->velocity + accel * dt; vec3 alpha(pitchA, yawA, rollA); omega = omega + alpha * dt; if(dot(omega, omega) > 1e-10) { quat omegaVersor = angleAxis(length(omega) * dt, facing * omega); facing = omegaVersor * facing; facing = normalize(facing); } //std::cout << "s:" << this->pos << ", v:" << this->velocity << ", a:" << accel << ", f:" << (facing * (vec3(0, 0, 1))) << std::endl; }
void TrackballCamera::computeQuat() {//calculate rotate quaternion float d, as; vec3 preVec, curVec, vMove, axis; //transform from window coordinate to projection plane preVec.x = (2.0f * preMousePosition.x / windowWidth - 1.0f) / mProj[0][0]; preVec.y = (-2.0f * preMousePosition.y / windowHeight + 1.0f) / mProj[1][1]; preVec.z = 1.0f; d = preVec.x * preVec.x + preVec.y * preVec.y; curVec.x = (2.0f * curMousePosition.x / windowWidth - 1.0f) / mProj[0][0]; curVec.y = (-2.0f * curMousePosition.y / windowHeight + 1.0f) / mProj[1][1]; curVec.z = 1.0f; vMove = curVec - preVec; axis = cross(curVec, preVec); axis = normalize(axis); as = (length(vMove) * length(vMove) - length(curVec) * length(curVec) - length(preVec) * length(preVec)) / (2.0f*length(curVec)*length(preVec)); mQuat = angleAxis(as, axis); preMousePosition = curMousePosition; }
vec3 Aircraft::fWing() { quat wingang = angleAxis(aoi, vec3(-1, 0, 0)); vec3 wn = facing * (wingang * vec3(0, 1, 0)); vec3 v = -1.0f * this->velocity; vec3 lift = wn * rho * wingarea * (float) fabs(dot(v, wn)) * (dot(v, wn)); return lift; }
void TrfmRotateQuat::applyGLTransform(renderer::RenderInterface* _ri) const { Quaterniond q(mDofs[0]->getValue(), mDofs[1]->getValue(), mDofs[2]->getValue(), mDofs[3]->getValue()); q.normalize(); AngleAxisd angleAxis(q); Vector3d a = angleAxis.axis(); double theta = angleAxis.angle(); if(a.dot(a)>M_EPSILON*M_EPSILON && theta>M_EPSILON) { if (_ri) _ri->rotate(a, theta*180/M_PI); } }
float Aircraft::tAileron() { float effect = left * maxaileron + right * minaileron; quat ailangl = angleAxis(-aoi - effect, vec3(-1, 0, 0)); quat ailangr = angleAxis(aoi - effect, vec3( 1, 0, 0)); vec3 anl = facing * (ailangl * (vec3(0, 1, 0))); vec3 anr = facing * (ailangr * (vec3(0, 1, 0))); vec3 v = this->velocity * -1.0f; vec3 vl = v + rolldampcoeff * (facing * (vec3(0, -this->omega.z * aileronradius, 0))); vec3 vr = v + rolldampcoeff * (facing * (vec3(0, this->omega.z * aileronradius, 0))); vec3 liftl = anl * rho * aileronarea * (float) fabs(dot(vl, anl)) * (dot(vl, anl)); vec3 liftr = anr * rho * aileronarea * (float) fabs(dot(vr, anr)) * (dot(vr, anr)); vec3 lt = cross(liftl, (facing * (vec3(-aileronradius, 0, 0)))); vec3 rt = cross(liftr, (facing * (vec3(aileronradius, 0, 0)))); vec3 torque = lt + rt; return dot(torque, facing * (vec3(0, 0, 1))); }
const mat4 rotate( const vec3 & a , const vec3 & b) { const vec3 anorm(normalize(a)); const vec3 bnorm(normalize(b)); const vec3 axis = cross(anorm, bnorm); const float angle = acos(dot(anorm, bnorm)); return mat4_cast(angleAxis(degrees(angle), axis)); }
Quaternionf Orientation(int i, const std::vector<std::string>& strings, const std::vector<float>& f) { Quaternionf q(1,0,0,0); // Unit quaternion while (i<strings.size()) { std::string c = strings[i++]; if (c == "x") q *= angleAxis(f[i++]*Radians, Vector3f::UnitX()); else if (c == "y") q *= angleAxis(f[i++]*Radians, Vector3f::UnitY()); else if (c == "z") q *= angleAxis(f[i++]*Radians, Vector3f::UnitZ()); else if (c == "q") { q *= Quaternionf(f[i+0], f[i+1], f[i+2], f[i+3]); i+=4; } else if (c == "a") { q *= angleAxis(f[i+0]*Radians, Vector3f(f[i+1], f[i+2], f[i+3]).normalized()); i+=4; } } return q; }
void BulletWrapper::updateObjectHolding(const SkeletonHand& skeletonHand, BulletHandRepresentation& handRepresentation, float deltaTime) { const float strengthThreshold = 1.8f; if (skeletonHand.grabStrength >= strengthThreshold && handRepresentation.m_HeldBody == NULL) { // Find body to pick EigenTypes::Vector3f underHandDirection = -skeletonHand.rotationButNotReally * EigenTypes::Vector3f::UnitY(); btRigidBody* closestBody = utilFindClosestBody(skeletonHand.getManipulationPoint(), underHandDirection); handRepresentation.m_HeldBody = closestBody; } if (skeletonHand.grabStrength < strengthThreshold) { // Let body go handRepresentation.m_HeldBody = NULL; } if (handRepresentation.m_HeldBody != NULL) { btRigidBody* body = handRepresentation.m_HeldBody; // Control held body s_LastHeldBody = body; m_FramesTilLastHeldBodyReset = 12; //handRepresentation.m_HeldBody->setCollisionFlags(0); // apply velocities or apply positions btVector3 target = ToBullet(skeletonHand.getManipulationPoint());// - skeletonHand.rotationButNotReally * EigenTypes::Vector3f(0.1f, 0.1f, 0.1f)); btVector3 current = body->getWorldTransform().getOrigin(); btVector3 targetVelocity = 0.5f * (target - current) / deltaTime; body->setLinearVelocity(targetVelocity); // convert from-to quaternions to angular velocity in world space { Eigen::Quaternionf currentRotation = FromBullet(body->getWorldTransform().getRotation()); Eigen::Quaternionf targetRotation = Eigen::Quaternionf(skeletonHand.arbitraryRelatedRotation()); // breaks for left hand ??? Eigen::Quaternionf delta = currentRotation.inverse() * targetRotation; Eigen::AngleAxis<float> angleAxis(delta); EigenTypes::Vector3f axis = angleAxis.axis(); float angle = angleAxis.angle(); const float pi = 3.1415926536f; if (angle > pi) angle -= 2.0f * pi; if (angle < -pi) angle += 2.0f * pi; EigenTypes::Vector3f angularVelocity = currentRotation * (axis * angle * 0.5f / deltaTime); body->setAngularVelocity(ToBullet(angularVelocity)); } } }
void Ship::reset () { position = vec3 (0); velocity = vec3 (0); orientation = angleAxis (0.f, vec3 (0, 0, 1)); throttle = false; fire = false; throttleParticles->reset (); throttleParticles->setParticleSize (engine.width/16.0); fireParticles->reset (); exploded = false; }
float Aircraft::tElevator() { float effect = -up * minelevator + -down * maxelevator; quat elangl = angleAxis(-effect, vec3(1, 0, 0)); vec3 en = facing * (elangl * (vec3(0, 1, 0))); vec3 v = this->velocity * -1.0f + facing * (vec3(0, omega.x * elevatorradius, 0)); vec3 lift = en * rho * elevatorarea * (float) fabs(dot(v, en)) * (dot(v, en)); vec3 et = cross(lift, (facing * (vec3(0, 0, elevatorradius)))); return dot(et, (facing * (vec3(1, 0, 0)))); }
mat4x4 Camera::getViewMatrix( void ) { m_Dir = normalize(m_LookAt - m_Pos); vec3 pitchAxis = cross(m_Dir, m_Up); quat pitchQ = angleAxis(m_Pitch, pitchAxis); quat yawQ = angleAxis(m_Yaw, m_Up); quat tmp = normalize(cross(pitchQ, yawQ)); m_Dir = glm::rotate(tmp, m_Dir); m_Pos += m_PosDelta; m_LookAt = m_Pos + m_Dir; m_View = glm::lookAt(getPos(), m_LookAt, getUp()); m_Pitch *= 0.5f; m_Yaw *= 0.5f; m_PosDelta *= 0.8f; return m_View; }
vector <Edge2> CueTable::generatePocketEdges( const Circle& circle, const Edge2& startEdge, const Edge2& endEdge, int quality ){ vector <Edge2> pocketEdges; float startAngle = orientedAngle(-startEdge.getDirection(),vec2(0,1)); float endAngle = orientedAngle(endEdge.getDirection(), vec2(0,1)); float startRad = radians( startAngle ); float endRad = radians( endAngle ); float alpha = (endAngle - startAngle) / (float)(quality - 1); float alphaRad = radians( alpha ); float tangetialFactor = tan( alphaRad ); float radialFactor = cos( alphaRad ); float x = circle.radius * cos( 0.0f ); float y = circle.radius * sin( 0.0f ); auto rotate = [=]( float x, float y) -> vec2 { vec3 i = vec3( x, 0, y ); vec3 r = angleAxis(startAngle + 90, vec3(0,1,0) ) * i; return vec2( r.x, r.z ); }; for (int i = 0; i < quality - 1; i++){ vec2 v0 = circle.center + rotate( x,y ); float tx = -y; float ty = x; x += tx * tangetialFactor; y += ty * tangetialFactor; x *= radialFactor; y *= radialFactor; vec2 v1 = circle.center + rotate( x,y ); pocketEdges.push_back( Edge2(v0, v1) ); } return pocketEdges; }
Ship::Ship () { stride = 3 * 3 * sizeof (GLfloat); position = vec3 (0); velocity = vec3 (0); orientation = angleAxis (0.f, vec3 (0, 0, 1)); restoreState (); throttle = false; fire = false; throttleParticles = unique_ptr<Particles> (new Particles (vec3(1,0.5,0.1), 1024, engine.width/16.0, 1/256.0)); fireParticles = unique_ptr<Particles> (new Particles (vec3(1,0.1,0.01), 512, engine.width/16.0, 1/1024.0)); throttleTime = 0; fireTime = 0; guideParticles = unique_ptr<Particles> (new Particles (vec3{0.1, 1, 0.1}, 10, engine.width/32.0, 1/1024.0)); exploded = false; }
// points1, points2, void computeRot(vector<double>& template_vextex, vector<double>& vertex, vector<vector<double> >& template_nbor_vertices, vector<vector<double > >& nbor_vertices, vector<unsigned int>& neighbors, vector<double>& weights, vector<double>& output_rot, bool deform) { int num_neighbors = neighbors.size(); MatrixXd Xt(3, num_neighbors), X(3, num_neighbors); for(int i = 0; i < num_neighbors; ++i) { for(int j = 0; j < 3; ++j) { Xt(j,i) = weights[i] * ( template_vextex[j] - template_nbor_vertices[ neighbors[i] ][j]); X(j,i) = weights[i] * ( vertex[j] - nbor_vertices[ neighbors[i] ][j]); if(deform) X(j,i) += Xt(j,i); } } Eigen::Matrix3d sigma = Xt * X.transpose(); Eigen::JacobiSVD<Eigen::Matrix3d> svd(sigma, Eigen::ComputeFullU | Eigen::ComputeFullV); Eigen::Matrix3d Rot; if(svd.matrixU().determinant()*svd.matrixV().determinant() < 0.0) { Eigen::Vector3d S = Eigen::Vector3d::Ones(); S(2) = -1.0; Rot = svd.matrixV()*S.asDiagonal()*svd.matrixU().transpose(); } else { Rot = svd.matrixV()*svd.matrixU().transpose(); } // ceres::RotationMatrixToAngleAxis(&Rot(0, 0), &output_rot[0]); Eigen::AngleAxisd angleAxis(Rot); for(int i = 0; i < 3; ++i) output_rot[i] = angleAxis.axis()[i] * angleAxis.angle(); }
void World::init(Context context_) { context = context_; { auto player = make_unique<SceneNode>(); player->name = "player"; player->transform.position = {2, 0.5, 2}; player->transform.orientation = angleAxis(Degree{45}, {0, 1, 0}); //player->addComponent<FaceCamera>(mainCamera); player->addComponent<MeshRenderer>( &context.meshHolder->get("sprite"), &context.materialHolder->get("cat")); this->player = player.get(); sceneGraph.attachChild(std::move(player)); } { auto level = make_unique<Level>(); level->material = &context.materialHolder->get("terrain"); level->generate(); this->level = level.get(); sceneGraph.attachChild(std::move(level)); } Random random{1}; for (int i{0}; i < 20; i++) { PointLight light; f32 radius{random.getFloat(0.1f, 16.0f)}; Radian angle{random.getFloat(0, Math::Tau)}; light.position.x = 4.0f + radius * Math::cos(angle); light.position.y = random.getFloat(0.5, 2.5); light.position.z = 4.0f + radius * Math::sin(angle); light.intensity = 1.0; light.color.r = random.getInt(50, 255); light.color.g = random.getInt(50, 255); light.color.b = random.getInt(50, 255); pointLights.emplace_back(light); } { DirectionalLight light; light.color = Color{255, 255, 250}; light.direction = Vector3{-1, -1, 0.5}; light.intensity = 0.1f; directionalLights.emplace_back(light); } { SpotLight light; light.color = Color{255, 255, 250}; light.direction = {0, -1, 0}; light.position = {4, 1.5f, 4}; light.intensity = 2.0f; light.coneAngle = Degree{50}; spotLights.emplace_back(light); } { // Init Camera playerCamera.transform.position = {5, 2, 5}; playerCamera.transform.orientation = angleAxis(Degree{45}, {0, 1, 0}) * angleAxis(Degree{-30}, {1, 0, 0}); playerCamera.fieldOfView = Degree{50.0f}; playerCamera.orthoScale = 8; mainCamera = playerCamera; playerCamera.projectionType = ProjectionType::Orthographic; } currentCamera = &mainCamera; sceneGraph.init(); }