B2Vec2 B2World B2Bodydef B2Body B2Polygonshape
B2Vec2 B2World B2Bodydef B2Body B2Polygonshape
B2Vec2 B2World B2Bodydef B2Body B2Polygonshape
#include <iostream>
#include <stdio.h>
#include <Box2D\Box2D.h>
int main()
{
b2Vec2 gravity(0.0f,-10.0f);
b2World world(gravity);
//ground body
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f,-10.0f);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
groundBox.SetAsBox(50.0f,10.0f);
groundBody->CreateFixture(&groundBox,0.0f);
//dynamic body which is going to fall under the worlds gravity
b2BodyDef bodydef;
bodydef.type=b2_dynamicBody;
bodydef.position.Set(0.0f,4.0f);
b2Body* body=world.CreateBody(&bodydef);
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(1.0f,1.0f);
b2FixtureDef fixturedef;
fixturedef.shape = &dynamicBox;
fixturedef.density=1.0f;
fixturedef.friction=0.3f;
body->CreateFixture(&fixturedef);
float32 timestep=1.0f/60.0f;
int32 velocityIterations=6;
int32 positionIterations=2;
for(int32 i=0;i<60;++i)
{
world.Step(timestep,velocityIterations,positionIterations);
b2Vec2 position=body->GetPosition();
float32 angle=body->GetAngle();
printf("%4.2f,%4.2f,%4.2f\n",position.x,position.y,angle);
}
getchar();
return 0;
}
OpenGL API Window Creation
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const GLint WIDTH=800,HEIGHT=600;
int main()
{
glfwInit();//Intialiing the framework
GLFWwindow* window=glfwCreateWindow(WIDTH,HEIGHT,"Firstexample",NULL,NULL);//setting
//the height and width of the window
glfwMakeContextCurrent(window);
glewInit();
glfwSetInputMode(window,GLFW_STICKY_KEYS,GL_TRUE);//outside the game loop
while(glfwGetKey(window,GLFW_KEY_A)!=GLFW_PRESS && glfwWindowShouldClose(window)==0)
//game loop
{
glfwPollEvents();
glClearColor(0.5f,0.2f,0.5f,1.0f);// R(red) G(Green) B(Blue) A(alpha)
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
OpenGL Windows with Shapes display
Display.h
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include<math.h>
#define PI 3.1415926535898
class DrawOpenglPrimitives{
GLfloat angle;
public:
void displayTraingle();
void displayQuad();
void DrawCircle(float cx, float cy, float r, int num_segments);
void DrawFilledCircle(float x,float y,double radius,int num_segment);
};
void DrawOpenglPrimitives::displayTraingle(){
glBegin(GL_TRIANGLES);
glColor3f(1.0,0.0,0.0);
glVertex3f(0.2f,0.2f,0.0f);
glVertex3f(0.4f,0.4f,0.0f);
glVertex3f(0.6f,0.2f,0.0f);
glEnd();
}
void DrawOpenglPrimitives::displayQuad()
{
glBegin(GL_QUADS);
glColor3f(0.0,1.0,0.5);
glVertex3f(-0.2f,0.2f,0.0f);
glVertex3f(-0.4f,0.2f,0.0f);
glVertex3f(-0.4f,0.4f,0.0f);
glVertex3f(-0.2f,0.4f,0.0f);
glEnd();
}
void DrawOpenglPrimitives::DrawCircle(float cx, float cy, float r, int num_segments)
{
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
float angle = 2.0f * PI * float(ii) / float(num_segments);//get the current angle
}
glEnd();
}
void DrawOpenglPrimitives::DrawFilledCircle(float x,float y,double radius,int num_segment)
{
glColor3f(0.5f,1.0f,0.8f);
double twicePi = 2.0 * PI;
glBegin(GL_TRIANGLE_FAN); //BEGIN CIRCLE
glVertex2f(x, y); // center of circle
for (int i = 0; i <= 100; i++) {
glVertex2f ((x + (radius * cos(i * twicePi / 20))), (y + (radius * sin(i * twicePi /
20))));
}
glEnd(); //EN
}
Main.cpp
#include "Display.h"
const GLint WIDTH=800,HEIGHT=600;
int main()
{
glfwInit();//Intialiing the framework
GLFWwindow* window=glfwCreateWindow(WIDTH,HEIGHT,"First
example",NULL,NULL);//setting the height and width of the window
glfwMakeContextCurrent(window);
glewInit();
glfwSetInputMode(window,GLFW_STICKY_KEYS,GL_TRUE);//outside the game loop
DrawOpenglPrimitives dop;
while(glfwGetKey(window,GLFW_KEY_A)!=GLFW_PRESS &&
glfwWindowShouldClose(window)==0)//game loop
{
glfwPollEvents();
glClearColor(0.5f,0.2f,0.5f,1.0f);// R(red) G(Green) B(Blue) A(alpha)
glClear(GL_COLOR_BUFFER_BIT);
dop.displayTraingle();
dop.displayQuad();
dop.DrawCircle(-0.3f,-0.3f,0.1f,100);
dop.DrawFilledCircle(0.4f,-0.3f,0.1f,20);
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}