Jump to content

  • Log In with Google      Sign In   
  • Create Account


We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Member Since 24 Mar 2011
Offline Last Active Feb 24 2013 01:00 AM

Topics I've Started

The problem with temporary variable

10 January 2013 - 10:02 AM

hi guys!

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;

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 );


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>

#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 );
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;

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 ];


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 );



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

Verlet physics

20 December 2012 - 09:24 AM

Hi guys!
I was read this article on this site

And I wondered, what if upgrade it for experiment?
i downloaded it and decided to add friction.

But I did not understand how to do it correctly. We can replace the speed of this -k*N+OldVelocity;
where N as I understood the normalized velocity (well, more multiplied by -1 is not?)

Well, add a small fix that would not work, that the infinite is force the body begins to move in the opposite direction
VelNew = max(-k*N+OldVelocity, 0.0f)

how to modify this method (if not this one?) to get the friction?
[source lang="cpp"]void Physics::UpdateVerlet() { //Обновляем положение вершин for( int I = 0; I < VertexCount; I++ ) { Vertex& V = *Vertices[ I ]; Vec2 OldPosition = V.Position; V.Position += V.Position-V.OldPosition + V.Acceleration*Timestep*Timestep; V.OldPosition = OldPosition; }}[/source]