b2bodies dont rotating right

Hi

i am testing little bit this engine with hge grafik engine.

Collesion is ok .

The Problem is my bodies rotating very bad .


#define PI 3.1415926535f
#define DEGTORAD(angle) (angle * (PI / 180.0f))
#define RADTODEG(angle) (angle * (180 / PI))


#include "hge.h"
#include "Box2D.h"
#include <String.h>
#include "..\..\include\hgefont.h"

hgeFont* fnt;
b2PolygonDef shapeDef;
b2PolygonDef shapeDef2;

b2PolygonDef shapeDef3;


b2World *world;
b2BodyDef bodyDef;
b2BodyDef bodyDef2;
b2BodyDef bodyDef3;


b2AABB worldAABB;

b2Body* body;
b2Body* body2;

b2Vec2 gravity(0.0f, +0.05f);

float testy=0;

float h=0.0f;

bool tests=false;


HGE *hge=0;

// Quad is the basic primitive in HGE
// used for rendering graphics.
// Quad contains 4 vertices, numbered
// 0 to 3 clockwise.
hgeQuad quad;
hgeQuad quad2;


// Handle for a sound effect
HEFFECT snd;

// Some "gameplay" variables and constants
float x=100.0f, y=100.0f;
float dx=0.0f, dy=0.0f;

float x2=100.0f, y2=100.0f;

const float speed=90;
const float friction=0.5f;

float32 a;
float32 a2;
// This function plays collision sound with
// parameters based on sprite position and speed


b2Vec2 MatrixMath_Rotate(b2Vec2 vertex, b2Vec2 Hotspot, float angle);


void boom() {
int pan=int((x-400)/4);
float pitch=(dx*dx+dy*dy)*0.0005f+0.2f;
hge->Effect_PlayEx(snd,100,pan,pitch);
}

bool FrameFunc()
{
// Get the time elapsed since last call of FrameFunc().
// This will help us to synchronize on different
// machines and video modes.
float dt=hge->Timer_GetDelta();

// in most game scenarios.
float32 timeStep = 1.0f / 30.0f;
int32 iterations = 10;





world->Step(timeStep, iterations);


b2Vec2 position = body->GetPosition();
b2Vec2 position2 = body2->GetPosition();


hge->System_SetState(HGE_TITLE, "hallo");


x=position.x;
y=position.y;

x2=position2.x;
y2=position2.y;


// Process keys

//if(y>=600){ body->SetXForm(newPos,ag);}

tests=body->IsSleeping();
dy=0;dx=0;
if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;
if (hge->Input_GetKeyState(HGEK_LEFT)) dx=-speed*dt;
if (hge->Input_GetKeyState(HGEK_RIGHT)) dx=speed*dt;
if (hge->Input_GetKeyState(HGEK_UP)) dy=-speed*dt;
if (hge->Input_GetKeyState(HGEK_DOWN)) dy=speed*dt;




b2Vec2 newPos(dx*100,dy*100);
testy=dy;


body->ApplyImpulse(newPos,body->GetPosition());


// Set up quad's screen coordinates
quad.v[0].x=x-16; quad.v[0].y=y-16;
quad.v[1].x=x+16; quad.v[1].y=y-16;
quad.v[2].x=x+16; quad.v[2].y=y+16;
quad.v[3].x=x-16; quad.v[3].y=y+16;


for(int i=0;i<=3;i++)
{

b2Vec2 a(quad.v[i].x,quad.v[i].y);
b2Vec2 a2(x,y);

b2Vec2 newa=MatrixMath_Rotate(a, a2, body->GetAngle());
quad.v[i].x=newa.x;
quad.v[i].y=newa.y;
}


quad2.v[0].x=x2-16; quad2.v[0].y=y2-16;
quad2.v[1].x=x2+16; quad2.v[1].y=y2-16;
quad2.v[2].x=x2+16; quad2.v[2].y=y2+16;
quad2.v[3].x=x2-16; quad2.v[3].y=y2+16;




for(int i=0;i<=3;i++)
{

b2Vec2 a(quad2.v[i].x,quad2.v[i].y);
b2Vec2 a2(x2,y2);

b2Vec2 newa=MatrixMath_Rotate(a, a2, body2->GetAngle());
quad2.v[i].x=newa.x;
quad2.v[i].y=newa.y;
}



// Continue execution
return false;
}

// This function will be called by HGE when
// the application window should be redrawn.
// Put your rendering code here.
bool RenderFunc()
{
// Begin rendering quads.
// This function must be called
// before any actual rendering.
hge->Gfx_BeginScene();

// Clear screen with black color
hge->Gfx_Clear(0);

// Render quads here. This time just
// one of them will serve our needs.


hge->Gfx_RenderQuad(&quad);

hge->Gfx_RenderQuad(&quad2);




fnt->printf(5, 5, HGETEXT_LEFT, "body angle %f",body->GetAngle());


fnt->printf(5, 25, HGETEXT_LEFT, "sleep %s",(tests)?"true":"false");

fnt->printf(550, 5, HGETEXT_LEFT, "FPS:%d ",hge->Timer_GetFPS());




// End rendering and update the screen
hge->Gfx_EndScene();

// RenderFunc should always return false
return false;
}


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
worldAABB.lowerBound.Set(-600.0f, -600.0f);
worldAABB.upperBound.Set(800.0f, 800.0f);

world=new b2World(worldAABB, gravity, true);


// Define the gravity vector.


// Do we want to let bodies sleep?



b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, 600.0f);


b2Body* ground = world->CreateBody(&groundBodyDef);

b2PolygonDef groundShapeDef;
groundShapeDef.SetAsBox(800.0f, 10.0f);


ground->CreateShape(&groundShapeDef);



bodyDef.position.Set(15.0f, 15.0f);
body = world->CreateBody(&bodyDef);


bodyDef2.position.Set(300.0f, 400.0f);
body2 = world->CreateBody(&bodyDef2);

// Define another box shape for our dynamic body.



shapeDef.SetAsBox(15.0f, 15.0f);

// Set the box density to be non-zero, so it will be dynamic.
shapeDef.density = 1.0f;

// Override the default friction.
shapeDef.friction = 0.3f;

shapeDef.restitution=0.0f;




// Add the shape to the body.
body->CreateShape(&shapeDef);




shapeDef2.SetAsBox(15.0f, 15.0f);

// Set the box density to be non-zero, so it will be dynamic.

//schwere
shapeDef2.density = 0.0f;

// Override the default friction.
shapeDef2.friction = 1.0;

shapeDef2.restitution=0.0f;


body2->CreateShape(&shapeDef2);

// Now tell the dynamic body to compute it's mass properties base
// on its shape.
body->SetMassFromShapes();

body2->SetMassFromShapes();



// Get HGE interface

hge = hgeCreate(HGE_VERSION);

// Set up log file, frame function, render function and window title
hge->System_SetState(HGE_LOGFILE, "hge_tut02.log");
hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
hge->System_SetState(HGE_TITLE, "HGE Tutorial 02 - Using input, sound and rendering");

// Set up video mode
hge->System_SetState(HGE_WINDOWED, true);
hge->System_SetState(HGE_SCREENWIDTH, 800);
hge->System_SetState(HGE_SCREENHEIGHT, 600);
hge->System_SetState(HGE_SCREENBPP, 32);

if(hge->System_Initiate())
{
// Load sound and texture
snd=hge->Effect_Load("menu.wav");
quad.tex=hge->Texture_Load("particles.png");


quad2.tex=hge->Texture_Load("particles.png");


if(!snd || !quad.tex||!quad2.tex)
{
// If one of the data files is not found, display
// an error message and shutdown.
MessageBox(NULL, "Can't load MENU.WAV or PARTICLES.PNG", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
hge->System_Shutdown();
hge->Release();
return 0;
}

// Set up quad which we will use for rendering sprite
quad.blend=BLEND_ALPHAADD | BLEND_COLORMUL | BLEND_ZWRITE;

quad2.blend=BLEND_ALPHAADD | BLEND_COLORMUL | BLEND_ZWRITE;


for(int i=0;i<4;i++)
{
// Set up z-coordinate of vertices
quad.v[i].z=1.0f;

quad2.v[i].z=1.0f;

// Set up color. The format of DWORD col is 0xAARRGGBB
quad.v[i].col=0xFFFFFFFF;

quad2.v[i].col=0xFFFFFFFF;

}

// Set up quad's texture coordinates.
// 0,0 means top left corner and 1,1 -
// bottom right corner of the texture.
quad.v[0].tx=96.0/128.0; quad.v[0].ty=64.0/128.0;
quad.v[1].tx=128.0/128.0; quad.v[1].ty=64.0/128.0;
quad.v[2].tx=128.0/128.0; quad.v[2].ty=96.0/128.0;
quad.v[3].tx=96.0/128.0; quad.v[3].ty=96.0/128.0;


quad2.v[0].tx=96.0/128.0; quad2.v[0].ty=64.0/128.0;
quad2.v[1].tx=128.0/128.0; quad2.v[1].ty=64.0/128.0;
quad2.v[2].tx=128.0/128.0; quad2.v[2].ty=96.0/128.0;
quad2.v[3].tx=96.0/128.0; quad2.v[3].ty=96.0/128.0;


fnt=new hgeFont("font1.fnt");

// Let's rock now!
hge->System_Start();

// Free loaded texture and sound
hge->Texture_Free(quad.tex);
hge->Texture_Free(quad2.tex);

hge->Effect_Free(snd);
}
else MessageBox(NULL, hge->System_GetErrorMessage(), "Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);

// Clean up and shutdown
hge->System_Shutdown();
hge->Release();
return 0;
}


b2Vec2 MatrixMath_Rotate(b2Vec2 vertex, b2Vec2 Hotspot, float angle)
{
float matRotate[4];
matRotate[0] = cosf(DEGTORAD(angle));
matRotate[1] = -sinf(DEGTORAD(angle));
matRotate[2] = sinf(DEGTORAD(angle));
matRotate[3] = cosf(DEGTORAD(angle));

b2Vec2 offsetFromHotspot( (vertex.x - Hotspot.x), (vertex.y - Hotspot.y) );

float mat1x2[2];
mat1x2[0] = (offsetFromHotspot.x * matRotate[0]) + (offsetFromHotspot.y * matRotate[2]);
mat1x2[1] = (offsetFromHotspot.x * matRotate[1]) + (offsetFromHotspot.y * matRotate[3]);

return b2Vec2( (mat1x2[0]+Hotspot.x), (mat1x2[1]+Hotspot.y) );
}
I think set the new angel wrong .
But i cant find the error.


my framefunc()
{
quad.v[0].x=x-16; quad.v[0].y=y-16;
quad.v[1].x=x+16; quad.v[1].y=y-16;
quad.v[2].x=x+16; quad.v[2].y=y+16;
quad.v[3].x=x-16; quad.v[3].y=y+16;

for(int i=0;i<=3;i++)
{

b2Vec2 a(quad.v[i].x,quad.v[i].y);
b2Vec2 a2(x,y);

b2Vec2 newa=MatrixMath_Rotate(a, a2, body->GetAngle());
quad.v[i].x=newa.x;
quad.v[i].y=newa.y;
}
}

b2Vec2 MatrixMath_Rotate(b2Vec2 vertex, b2Vec2 Hotspot, float angle)
{
float matRotate[4];
matRotate[0] = cosf(DEGTORAD(angle));
matRotate[1] = -sinf(DEGTORAD(angle));
matRotate[2] = sinf(DEGTORAD(angle));
matRotate[3] = cosf(DEGTORAD(angle));

b2Vec2 offsetFromHotspot( (vertex.x - Hotspot.x), (vertex.y - Hotspot.y) );

float mat1x2[2];
mat1x2[0] = (offsetFromHotspot.x * matRotate[0]) + (offsetFromHotspot.y * matRotate[2]);
mat1x2[1] = (offsetFromHotspot.x * matRotate[1]) + (offsetFromHotspot.y * matRotate[3]);

return b2Vec2( (mat1x2[0]+Hotspot.x), (mat1x2[1]+Hotspot.y) );
}
k now i test it with the tutorial and i dont understand how a sqare cant stop rotating on the ground with a angle of 45....

here is my test with console

#include "Box2D.h"
#include <cstdio>
bool tests=false;
// This is a simple example of building and running a simulation
// using Box2D. Here we create a large ground box and a small dynamic
// box.
int main(int argc, char** argv)
{
B2_NOT_USED(argc);
B2_NOT_USED(argv);

// Define the size of the world. Simulation will still work
// if bodies reach the end of the world, but it will be slower.
b2AABB worldAABB;
worldAABB.lowerBound.Set(-100.0f, -100.0f);
worldAABB.upperBound.Set(100.0f, 100.0f);

// Define the gravity vector.
b2Vec2 gravity(0.0f, -10.0f);

// Do we want to let bodies sleep?
bool doSleep = true;

// Construct a world object, which will hold and simulate the rigid bodies.
b2World world(worldAABB, gravity, doSleep);

// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, -10.0f);

// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
b2Body* groundBody = world.CreateBody(&groundBodyDef);

// Define the ground box shape.
b2PolygonDef groundShapeDef;

// The extents are the half-widths of the box.
groundShapeDef.SetAsBox(50.0f, 10.0f);

// Add the ground shape to the ground body.
groundBody->CreateShape(&groundShapeDef);

// Define the dynamic body. We set its position and call the body factory.
b2BodyDef bodyDef;
bodyDef.position.Set(0.0f, 4.0f);

bodyDef.angle=45.0f;
b2Body* body = world.CreateBody(&bodyDef);

// Define another box shape for our dynamic body.
b2PolygonDef shapeDef;
shapeDef.SetAsBox(1.0f, 1.0f);

// Set the box density to be non-zero, so it will be dynamic.
shapeDef.density = 1.0f;

// Override the default friction.
shapeDef.friction = 0.3f;

// Add the shape to the body.
body->CreateShape(&shapeDef);

// Now tell the dynamic body to compute it's mass properties base
// on its shape.
body->SetMassFromShapes();

// Prepare for simulation. Typically we use a time step of 1/60 of a
// second (60Hz) and 10 iterations. This provides a high quality simulation
// in most game scenarios.
float32 timeStep = 1.0f / 60.0f;
int32 iterations = 10;

// This is our little game loop.
for (int32 i = 0; i < 300; ++i)
{
// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
world.Step(timeStep, iterations);

// Now print the position and angle of the body.
b2Vec2 position = body->GetPosition();
float32 angle = body->GetAngle();

tests=body->IsSleeping();

printf("%4.2f %4.2f %4.2f %d\n ", position.x, position.y, body->GetAngle(),i);

if(tests)
{
printf("true");
}
else
{
printf("false");
}

}
// When the world destructor is called, all bodies and joints are freed. This can
// create orphaned pointers, so be careful about your world management.
system("PAUSE");
return 0;
}
k i found the error


that is right

float matRotate[4];
matRotate[0] = cosf(angle);
matRotate[1] = -sinf(angle);
matRotate[2] = -matRotate[1];
matRotate[3] = matRotate[0];

Original thread: https://box2d.org/forum/viewtopic.php?t=4309