/*!-------------------------- Implicit conversation from Polygon, Bounding box generation. ---------------------------*/ Rectangle::Rectangle( const Polygon& other ) { if( other.getVertexCount() >= 3 ) { float minx = (other.getPosition() + other.getVertex(0) ).getX(); float miny = (other.getPosition() + other.getVertex(0) ).getY(); float maxx = (other.getPosition() + other.getVertex(0) ).getX(); float maxy = (other.getPosition() + other.getVertex(0) ).getY(); for(unsigned int i = 1; i < other.getVertexCount(); ++i) { Vector2d vertex = other.getPosition() + other.getVertex(i); if( vertex.getX() < minx ) minx = vertex.getX(); if( vertex.getY() < miny ) miny = vertex.getY(); if( vertex.getX() > maxx ) maxx = vertex.getX(); if( vertex.getY() > maxy ) maxy = vertex.getY(); } position.setX( minx ); position.setY( miny ); size.setX( maxx - minx ); size.setY( maxy - miny ); } else { *this = Rectangle(); } }
Line2d::Line2d(const Vector2d &direction, const Vector2d &point) { Vector2d d = direction; _a = d.getY() / d.getX(); _b = -1; if (_b == 0) { _c = -point.getX(); } else { _c = point.getY() - (d.getY() / d.getX()) * point.getX(); } }
double Vector2d::angle(Vector2d& other) { double value = m_x * other.getX() + m_y * other.getY(); double radians = acos(value); double degrees = radians * (180 / 3.14159265); return degrees; }
float Line2d::getDistanceTo(const Vector2d &point, Vector2d *intersection) const { float dist = fabsf(_a * point.getX() + _b * point.getY() + _c) / sqrt(_a * _a + _b * _b); if (intersection) { intersectsLine(getPerpendicular(point), intersection); } return dist; }
bool StringElement::containsPoint(const Vector2d &point) const { if (this->isErased()) return false; Vector2d axis = getEndPosition() - getStartPosition(); Vector2d translation = -getStartPosition(); double angle = -(Vector2d(1, 0).angleTo(axis)); Vector2d translatedPoint = translation + point; double transformedX = cos(angle) * translatedPoint.getX() - sin(angle) * translatedPoint.getY(); double transformedY = sin(angle) * translatedPoint.getX() + cos(angle) * translatedPoint.getY(); return transformedX >= 0 && transformedX <= axis.norm() && fabs(transformedY) <= (getStringWidth() / 2); }
void MetalPlatformDesignRendererHelper::renderMetalPlatform(const MetalPlatform &platform) const { if (platform.isErased()) return; Color white = Color::WHITE; Vector2d ul = platform.getUpperLeftCorner(); Vector2d ll = platform.getLowerLeftCorner(); Vector2d lr = platform.getLowerRightCorner(); Vector2d ur = platform.getUpperRightCorner(); drawingContext->save(); drawingContext->set_source_rgb(white.getRed(), white.getGreen(), white.getBlue()); drawingContext->move_to(ul.getX(), ul.getY()); drawingContext->line_to(ll.getX(), ll.getY()); drawingContext->line_to(lr.getX(), lr.getY()); drawingContext->line_to(ur.getX(), ur.getY()); drawingContext->line_to(ul.getX(), ul.getY()); drawingContext->fill(); drawingContext->restore(); }
bool DrawableObject::isClicked(Vector2d mousePosition) { bool isInside = false; // For no particular reason i will shoot the vector straight down on this ray trace for (unsigned int i = 0; i <points.size(); i++) { Vector2d vector = points[i]; // Handle the case that our ray goes through an actual pointsVector (a corner) if (vector.getX() == mousePosition.getX()) { isInside = !isInside; continue; } //check if it hits a "wall" which means we need the next vector in the box int secondPoint = i + 1; if (secondPoint >= points.size()) { secondPoint = 0; } Vector2d secondVector = points[secondPoint]; // Check that the mouse's X is lower than 1 vectors x and larger than one And that its Y position // is above the the Y on the line between the two vectors by using simple slope formula float yOnLine = (secondVector.getY() - vector.getY()) * ( (mousePosition.getX() - vector.getX()) / (secondVector.getX() - vector.getX()) ) + vector.getY(); if ( (vector.getX() > mousePosition.getX()) != (secondVector.getX() > mousePosition.getX()) && mousePosition.getY() > yOnLine ) { isInside = !isInside; } } return isInside; }
const CollisionResult CollisionManager::test( const Polygon& a, const Polygon& b, const Vector2d& v ) { // A place to store our collision result. CollisionResult result; result.colliding = true; // Do a radius test first. if ( (a.getPosition()-a.getPosition()).getMagnitude() > (a.getRadius() + a.getRadius()) ){ result.colliding = false; return result; } // The minimum distance required to separate the two polygons. float mintranslation = FLT_MAX; // Loop through both polygon's vertices and test them. for( unsigned int i = 0; i < a.getVertexCount() + b.getVertexCount(); ++i ) { // Get the edge, polygons are stored as point clouds, so we have to subtract two points to get an edge. Vector2d edge; if( i < a.getVertexCount() ) edge = a.getVertex( i ) - a.getVertex( i+1 ); else edge = b.getVertex( i - a.getVertexCount() ) - b.getVertex( i - a.getVertexCount()+1 ); // Get a perpendicular ( right-hand ) to the edge. edge = Vector2d( -edge.getY(), edge.getX() ); edge.normalize(); // Project the two polygons onto the edge. float minA,maxA,minB,maxB; projectPolygon( edge, a, minA, maxA ); projectPolygon( edge, b, minB, maxB ); // Get the distance of separation. float distance = intervalDistance(minA, maxA, minB, maxB); // If the distance is greater than 0, they aren't overlapping, so let's try a velocity. if (distance > 0){ float velocityProjection = edge * v; // Get the projection of object A during the movement if (velocityProjection < 0) minA += velocityProjection; else maxA += velocityProjection; // Do the same as above, but now with velocity projected. float distance = intervalDistance(minA, maxA, minB, maxB); } // If the distance is greater than 0, they aren't overlapping nor will they collide, so break. if (distance > 0){ result.colliding = false; break; } // Check to see if this is the minimum translation so far. if( abs(distance) < mintranslation ){ mintranslation = abs(distance); result.separation = edge; } } // Calculate the minimum translation vector if a collision occured. if( result.colliding == true ){ // multiply it by the minimum translation distance. result.separation *= mintranslation; //make sure it's facing the right way. if (result.separation * (a.getPosition() - b.getPosition()) < 0) result.separation = -result.separation; } return result; }
bool Line2d::containsPoint(const Vector2d &point) const { float n = _a * point.getX() + _b * point.getY() + _c; return (n < 0.0001 && n > -0.0001); }
void RunApplication::display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // reset OpenGL transformation matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); // reset transformation matrix to identity //Use a perspective of a square with the same aspect as our application //We dont want to clip anything in the Z direction so thats why its so far back gluPerspective(90.f, ApplicationState::Instance().getAspectRatio(), 0, 100 ); // setup look at transformation so that // eye is at : (0,0, 10) // look at center is at : (0,0,0) // up direction is +y axis //TODO: Find a way to have the camera slowly track the main player Vector2d cameraPos = ApplicationState::Instance().getCameraPosition(); float cameraZ = ApplicationState::Instance().getCameraZ(); gluLookAt(cameraPos.getX(), cameraPos.getY(), cameraZ+0.00000001f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f); /* Storing for reference will make sure to delete it glPushMatrix(); glTranslatef(ship[1], ship[5], 0.f); glRotatef(ship[2], 0.f, 0.f, 1); drawship(); glPopMatrix(); */ //TODO: current idea is that we will call the physics engine on each object // in the GameState's physical objects, and continue to move them forward // then we will do the rendering for the actual objects so it would look // something like this? /* //TODO: add in code to stop a "lag spiral of death" ie if the physics engine // takes 1/30'th a second to determine the positions then we should slow our frame // rate to match that GameState::Instance().processPhysics(); for( DrawableObject* drawable : ApplicationState::Instance().getDrawableObjects() ) { if( drawable != NULL ) //F**k segfaults { drawable->draw(); } } */ #if DEBUG ApplicationState::Instance().getClickableObjects(1).drawTree(); #endif for (DrawableObject* drawable : ApplicationState::Instance().getDrawableObjects()) { if (drawable != NULL) //F**k segfaults { drawable->drawObject(); } } if ( Editor::Instance()->isPluggedIn()) { Editor::Instance()->drawEditorObjects(); } glFlush(); glutSwapBuffers(); // swap front/back framebuffer to avoid flickering }
bool Space2d<T>::isIn(const Vector2d<T>& point) const { const bool xIsIn = (origin.getX() <= point.getX()) && (point.getX() <= origin.getX() + vector.getX()); const bool yIsIn = (origin.getY() <= point.getY()) && (point.getY() <= origin.getY() + vector.getY()); return xIsIn && yIsIn; }