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