// Uses absolute values // Eg: 5 and -100. -100 is the maximum. Vec2 MaximumValue( Vec2 a, Vec2 b ) { Vec2 c = Vec2( abs(a.x),abs(a.y)); Vec2 d = Vec2( abs(b.x),abs(b.y)); float aLen = c.Length(); float bLen = d.Length(); if( aLen < bLen ) { return b; } else return a; }
void Boid::Collision(GameObject* other, const Vec2& point, const Vec2& normal) { if (other->GetType() == TPlayer) { // bounce effect Vec2 normal = other->GetPosition() - GetPosition(); normal = normal.Normalize(); Vec2 relVel = other->GetVelocity() - GetVelocity(); float relVelMag = relVel.Length(); float mass = GetMass() + other->GetMass(); Vec2 pVel = other->GetVelocity(); pVel += -normal * ((Player*)other)->GetShieldForce(); Vec2 v1 = ((GetMass() - other->GetMass()) / mass) * GetVelocity() + (2.f * other->GetMass() / mass) * pVel - GetVelocity(); Vec2 v2 = ((other->GetMass() - GetMass()) / mass) * other->GetVelocity() + (2.f * GetMass() / mass) * GetVelocity() - other->GetVelocity(); AddForce(v1 / g_FrameTime); other->AddForce(v2 / g_FrameTime); SetPosition(other->GetPosition() + -normal * (GetScale() + other->GetScale() * 0.5f)); } else if (other->GetType() == TBoid) { m_Anger += g_FrameTime; } else if (other->GetType() == TWall) { if (GetVelocity().Length() > 4.f) Destroy(); } }
void Boid::AvoidWalls() { for (GameObject* obj = GameObject::All(); obj != NULL; obj = obj->GetNext()) { // do strong separation for walls if (obj->GetType() == GameObject::TWall) { Vec2 delta = GetPosition() - obj->GetPosition(); float dist = delta.Length(); if (dist < 2.f && dist > 0.f) AddForce(Steer(delta.Normalize() / dist)); } } // avoid outer walls Vec2 steer; if (GetPosition().x < 1.f) steer += Steer(Vec2(1.f, 0.f)); if (GetPosition().x > 19.f) steer += Steer(Vec2(-1.f, 0.f)); if (GetPosition().y < 1.f) steer += Steer(Vec2(0.f, 1.f)); if (GetPosition().y > 14.f) steer += Steer(Vec2(0.f, -1.f)); AddForce(2.f * steer); }
Vec2 Boid::Attack() { // steer towards nearest player Player* nearest = NULL; Vec2 nearestV; float nearestD = FLT_MAX; for (int i = 0; i != MAX_PLAYERS; ++i) { if (g_Players[i] != NULL) { Vec2 v = g_Players[i]->GetPosition() - GetPosition(); float d = v.Length(); if (d < nearestD) { nearest = g_Players[i]; nearestD = d; nearestV = v; } } } Vec2 player; if (nearest != NULL) return Steer(nearestV); else return Vec2(); }
void DistanceJointDef::Initialize(Body* b1, Body* b2, const Vec2& anchor1, const Vec2& anchor2) { bodyA = b1; bodyB = b2; localAnchorA = bodyA->GetLocalPoint(anchor1); localAnchorB = bodyB->GetLocalPoint(anchor2); Vec2 d = anchor2 - anchor1; length = d.Length(); }
void UnderWaterGodRay::_updateRays() { Camera * cam = World::Instance()->MainCamera(); const Vec3 * corner = cam->GetCorner(); float FarWidth = (corner[4] - corner[5]).Length(); float RaysLength = cam->GetFarClip(); VertexBufferPtr vb = mRender.vxStream.GetStream(0); Vec3 * vert = (Vec3*)vb->Lock(0, 0, LOCK_DISCARD); Vec2 Pos; float Dis, RayLength; for(int k = 0; k < mNumberOfRays; k++) { Pos = _calculateRayPosition(k); Dis = mRaysSize * RaysLength; RayLength = RaysLength * (0.3f + Pos.Length()); Pos *= FarWidth/2; // 4 Planes, 3 vertices each plane, 12 vertices per ray // ----> 1/4 // 0 *vert++ = Vec3(0, 0, 0); // A *vert++ = Vec3(Pos.x, Pos.y, -RayLength); // B *vert++ = Vec3(Pos.x+Dis, Pos.y, -RayLength); // ----> 2/4 // 0 *vert++ = Vec3(0, 0, 0); // D *vert++ = Vec3(Pos.x+Dis, Pos.y+Dis, -RayLength); // B *vert++ = Vec3(Pos.x+Dis, Pos.y, -RayLength); // ----> 3/4 // 0 *vert++ = Vec3(0, 0, 0); // C *vert++ = Vec3(Pos.x, Pos.y+Dis, -RayLength); // D *vert++ = Vec3(Pos.x+Dis, Pos.y+Dis, -RayLength); // ----> 4/4 // 0 *vert++ = Vec3(0, 0, 0); // C *vert++ = Vec3(Pos.x, Pos.y+Dis, -RayLength); // A *vert++ = Vec3(Pos.x, Pos.y, -RayLength); } vb->Unlock(); }
void Player::on_move(Vec2 dir) { Vec3 direction = Mat4::Rotate(this->get_world_rotation()) * Vec3{ dir.X, 0, -dir.Y }; this->get_world().get_object(this->character_movement)->walk(Vec2{ direction.X, direction.Z } / 5); auto* animation = weapon.get_object(this->get_world())->animation.get_object(this->get_world()); animation->play_speed = dir.Length(); if (!first_helmet.is_null()) { first_helmet.get_object(this->get_world())->wobble(dir * -2); } }
void hull_sort(vector<City> &hull_points, int i){ long double min_dist = -1; int min_index = 0; for(int j = i+1; j < hull_points.size(); j++){ Vec2 temp = hull_points[j].point() - hull_points[i].point(); // cout << j << min_dist <<endl; if( temp.Length() < min_dist || min_dist < 0){ min_dist = temp.Length(); min_index = j; } } // hull_points[min_index].Print("closest points"); City temp = hull_points[i+1]; hull_points[i+1] = hull_points[min_index]; hull_points[min_index] = temp; //swap with the one that is i + 1 if(i+1 < hull_points.size()-1) hull_sort(hull_points, i+1); }
void Postprocessing_UpdateQuake(float deltaTime) { if (!quake->enabled) return; quake->time += deltaTime; // Update quake bool toCenter = true; if (quake->time - quake->lastHitTime > quake->hitLength) { const float hitSizeX = 0.02f; const float hitSizeY = 0.1f; const float startFadeOutTime = 0.2f; const float fadeOutTime = 0.15f; quake->targetOffset.Set( hitSizeX * ((Random::GetInt() & 1) ? 1.0f : -1.0f), hitSizeY * ((Random::GetInt() & 1) ? 1.0f : -1.0f)); if (quake->lastHitTime > startFadeOutTime) { quake->targetOffset *= max(0.0f, (fadeOutTime - (quake->lastHitTime - startFadeOutTime)) / fadeOutTime); } quake->lastHitTime = quake->time; quake->hitLength = Random::GetFloat(0.1f, 0.1f); if (toCenter) quake->offset = quake->targetOffset; } Vec2 toTargetDir; if (toCenter) toTargetDir = Vec2(0, 0) - quake->offset; else toTargetDir = quake->targetOffset - quake->offset; float toTargetLength = toTargetDir.Length(); toTargetDir.Normalize(); float moveBy = clamp(toTargetLength, 0.0f, (toCenter ? 0.8f : 1.0f) * deltaTime); quake->offset += toTargetDir * moveBy; if (quake->offset.Length() <= 0.001f) quake->enabled = false; }
void Boid::FindClosest(BoidFriend* boids, int& count, int maxCount) { count = 0; for (GameObject* obj = GameObject::All(); obj != NULL; obj = obj->GetNext()) { if (obj != this) { Vec2 delta = GetPosition() - obj->GetPosition(); float dist = delta.Length(); // average position and direction for boids close enough to matter if (dist < 2.25f) InsertClosest((Boid*)obj, dist, boids, count, maxCount); } } }
void Follow::Update() { Rotate( M_PT_ONE ); if ( Player::CountKilledPlayers() >= GetPlayers().size() ) return; _timer++; if ( !_target || _timer > TIME ) { _target = GetNearestPlayer(); _timer = 0; } Vec2 d = _target->GetPosition() - GetPosition(); if ( d.Length() > 0 ) { d.Normalise(); d *= SPEED; Move( d ); } }
int main(int argc, char** argv) { GLFWwindow* window; if (!glfwInit()) { std::cout << "Failed to init GLFW!" << std::endl; return EXIT_FAILURE; } window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL", nullptr, nullptr); if (!window) { std::cout << "Failed to create GLFW window!" << std::endl; return EXIT_FAILURE; } glfwMakeContextCurrent(window); glewInit(); Shader vert = Shader(GL_VERTEX_SHADER, "data/shaders/basic.vert"); Shader frag = Shader(GL_FRAGMENT_SHADER, "data/shaders/fractal.frag"); ShaderProgram program = ShaderProgram(vert, frag); program.Enable(); Vec2 size = Vec2(800, 600); std::cout << size << std::endl; Log(size.Length()); Log(size.Normalize()); std::vector<Vec2> verticies; verticies.push_back(Vec2(-1, 1)); verticies.push_back(Vec2(1, 1)); verticies.push_back(Vec2(-1, -1)); verticies.push_back(Vec2(1, -1)); Vec2 test; test = test.Normalize(verticies[0]); Log(test); Log(test.AngleRadians()); Log(test.AngleDegrees()); GLint sizeID = glGetUniformLocation(program.ID(), "uSize"); glUniform2f(sizeID, WIDTH, HEIGHT); GLint offset = glGetUniformLocation(program.ID(), "offset"); glUniform2f(offset, 0,0); GLint scale = glGetUniformLocation(program.ID(), "scale"); glUniform1f(scale, 2.0f); while (!glfwWindowShouldClose(window)) { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLE_STRIP); for (size_t i = 0; i < verticies.size(); i++) { glVertex2f(verticies[i].X,verticies[i].Y); } glEnd(); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); std::cin.ignore(); return 0; }