hi guys!
http://www.gamedev.net/page/resources/_/technical/math-and-physics/a-verlet-based-approach-for-2d-game-physics-r2714
I read it and decided to finish a bit for themselves.
In general, the essence of what I needed was a regular polygon? ????? ???? ? ???, ??? ????? ??? ?????????? ?????????????
By the topography object is specified vertices and edges, respectively, as follows:For example vertex created so:
Vertex* V1 = new Vertex( this, X , Y );
and the face, say between two vertices as follows:
new Edge( this, V1, V2, true );
P.S fourth parameter is responsible for whether to participate in the face of conflict (ie, whether the outside of the face shape, or it will be framed)
Suppose that's to create a simple rectangle:???????? ??? ????????? ???????? ???????? ????????????????:
void PhysicsBody::CreateBox( int X, int Y, int Width, int Height ) {
Vertex* V1 = new Vertex( this, X , Y );
Vertex* V2 = new Vertex( this, X + Width, Y );
Vertex* V3 = new Vertex( this, X + Width, Y + Height );
Vertex* V4 = new Vertex( this, X , Y + Height );
new Edge( this, V1, V2, true );
new Edge( this, V2, V3, true );
new Edge( this, V3, V4, true );
new Edge( this, V4, V1, true );
new Edge( this, V1, V3, false );
new Edge( this, V2, V4, false );
}
Similarly, I added a method to create a regular polygon:
void PhysicsBody::CreateCircle(int X, int Y, int r, int n) {
Vertex * V = new Vertex[n];
float a=0.0;
Vertex * V1 = new Vertex( this, X , Y );
for (int i=0; i<n; i++) {
Vertex * V1 = new Vertex( this, X + r*std::cos(a*PI/180.0f), Y + r*std::sin(a*PI/180.0f));
V[i] = *V1;
a+=360.0f/n;
}
new Edge( this, &V[n-1], &V[0], true );
for (int i=0; i<n-1; i++)
new Edge( this, &V[i], &V[i+1], true );
for (int i=0; i<n; i++)
new Edge( this, V1, &V[i], false );
//delete(V);
}
And here's the problem, if the vertices of creating, describing each, then all would be OK, but here's a situation where I need to, say, 40-angle polygon, I will not each node individually prescribe ..It turns out that, for some reason, remembering addresses are not the top, when I click the mouse button, the polygon is created, but the top is not just tied to its faces and trite fall down
attached was only one vertex, the central
Vertex * V1 = new Vertex( this, X , Y );
All vertices and edges are stored in the respective arrays.description of the basic structures, ie, faces, vertices and body:
#ifndef __PHYSICS_H__
#define __PHYSICS_H__
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/glut.h>
#include <math.h>
#include <iostream>
#include "Vector.h"
#define MAX_BODIES 2048 //Maximum body/vertex/edgecount the physics simulation can handle
#define MAX_VERTICES 5096
#define MAX_EDGES 5096
#define MAX_BODY_VERTICES 64 //Maximum body/edge count a body can contain
#define MAX_BODY_EDGES 64
#define PI 3.14159
struct PhysicsBody; //Prototypes
struct Vertex {
Vec2 Position;
Vec2 OldPosition;
Vec2 Acceleration;
PhysicsBody* Parent;
Vertex( PhysicsBody* Body, float PosX, float PosY );
Vertex(){};
};
struct Edge;
class Physics {
Vec2 Gravity; //Most of this should be clear after reading the article
int BodyCount;
int VertexCount;
int EdgeCount;
Vertex* Vertices[ MAX_VERTICES ];
Edge* Edges [ MAX_EDGES ];
PhysicsBody* Bodies [ MAX_BODIES ];
float Timestep;
int Iterations;
void UpdateForces();
void UpdateVerlet();
void UpdateEdges ();
void IterateCollisions();
bool DetectCollision( PhysicsBody* B1, PhysicsBody* B2 );
void ProcessCollision();
float IntervalDistance( float MinA, float MaxA, float MinB, float MaxB );
bool BodiesOverlap( PhysicsBody* B1, PhysicsBody* B2 ); //Used for optimization to test if the bounding boxes of two bodies overlap
struct {
float Depth;
Vec2 Normal;
Edge* E;
Vertex* V;
} CollisionInfo;
public:
void Update();
void Render();
void AddBody ( PhysicsBody* Body );
void AddEdge ( Edge* E );
void AddVertex( Vertex* V );
Vertex* FindVertex( int X, int Y );
Physics( float GravitationX = 0.0f, float GravitationY = 0.0f, int pIterations = 1 ) :
BodyCount( 0 ), VertexCount( 0 ), EdgeCount( 0 ),
Gravity( Vec2( GravitationX, GravitationY ) ),
Iterations( pIterations ), Timestep( 1.0f ) {}
};
struct PhysicsBody {
Vec2 Center; //Center of mass
int MinX, MinY, MaxX, MaxY; //Min/max coordinates of the bounding box
int VertexCount;
int EdgeCount;
Vertex* Vertices[ MAX_BODY_VERTICES ];
Edge* Edges [ MAX_BODY_EDGES ];
PhysicsBody();
void AddEdge ( Edge* E );
void AddVertex( Vertex* V );
void ProjectToAxis( Vec2& Axis, float& Min, float& Max );
void CalculateCenter(); //Calculates the venter of mass
void CreateBox( int X, int Y, int Width, int Height );
void CreateCircle(int X, int Y, int r, int n);
};
struct Edge {
Vertex* V1;
Vertex* V2;
float Length;
int Boundary; //Value used for optimization - see Physics::DetectCollision for more information
PhysicsBody* Parent;
Edge( PhysicsBody* Body, Vertex* pV1, Vertex* pV2, int pBoundary = true );
};
#endif
actually referred to add components just add them to the array
void Physics::AddBody( PhysicsBody* Body ) {
Bodies[ BodyCount++ ] = Body;
}
void Physics::AddVertex( Vertex* V ) {
Vertices[ VertexCount++ ] = V;
}
void Physics::AddEdge( Edge* E ) {
Edges[ EdgeCount++ ] = E;
}
So appropriate constructors for the components:
Edge::Edge( PhysicsBody* Body, Vertex* pV1, Vertex* pV2, int pBoundary ) {
V1 = pV1;
V2 = pV2;
Length = ( pV2->Position - pV1->Position ).Length();
Boundary = pBoundary;
Parent = Body;
Body->AddEdge( this );
World.AddEdge( this );
}
Vertex::Vertex( PhysicsBody* Body, float PosX, float PosY ) {
Position = Vec2( PosX, PosY );
OldPosition = Vec2( PosX, PosY );
Parent = Body;
Body->AddVertex( this );
World.AddVertex( this );
}
PhysicsBody::PhysicsBody() {
VertexCount = EdgeCount = 0;
World.AddBody( this );
}
How do I solve the problem with falling vertices?
P.S demo: http://yadi.sk/d/Qg0_nZ7h1lOXC