Hi guys!
Big fan of box2d, coming from the actionscript version. However, I'm having trouble getting my code structured properly.
I'm trying to create a PhysicsController class to contain my b2World instance. However, I am now trying to learn C++ and am getting stuck on some instantiation stuff.
What I'd like to have is a public method update() as a member of the PhysicsController. However, in the HelloWorld examples, the entire code is in the default constructor scope. When I try to break it apart into smaller bits (e.g. making the b2World instance a public member) I run into problems, for example the "no default constructor" error that has been mentioned elsewhere.
Could someone please provide me with the correct syntax to do what I want? I realise this is maybe an overly basic question, but I'm really stumped and haven't been able to get this working for ages.
Current code:
Header:
Main Class
Any help much appreciated. Like I said, I'm from a Java/Actionscript background, so I'm still trying to get my head around C++. Sorry for being such a n00b 😉
Thanks in advance,
Alias
Big fan of box2d, coming from the actionscript version. However, I'm having trouble getting my code structured properly.
I'm trying to create a PhysicsController class to contain my b2World instance. However, I am now trying to learn C++ and am getting stuck on some instantiation stuff.
What I'd like to have is a public method update() as a member of the PhysicsController. However, in the HelloWorld examples, the entire code is in the default constructor scope. When I try to break it apart into smaller bits (e.g. making the b2World instance a public member) I run into problems, for example the "no default constructor" error that has been mentioned elsewhere.
Could someone please provide me with the correct syntax to do what I want? I realise this is maybe an overly basic question, but I'm really stumped and haven't been able to get this working for ages.
Current code:
Header:
Code:
//
// PhysicsController.h
//
#pragma once
#import <Box2D/Box2D.h>
class PhysicsController{
public:
PhysicsController();
b2Vec2 gravity(float x, float y);
b2World world(b2Vec2 gravity);
void setup();
void update();
void draw();
};
// PhysicsController.h
//
#pragma once
#import <Box2D/Box2D.h>
class PhysicsController{
public:
PhysicsController();
b2Vec2 gravity(float x, float y);
b2World world(b2Vec2 gravity);
void setup();
void update();
void draw();
};
Main Class
Code:
//
// PhysicsController.cpp
// cinderIOSTest1
//
//
#include "PhysicsController.h"
#include "cinder/Rand.h"
#include "cinder/gl/gl.h"
#include "cinder/app/AppBasic.h"
#include <Box2D/Box2D.h>
using namespace std;
PhysicsController::PhysicsController(){
}
void PhysicsController::setup(){
gravity = b2Vec2(0,0.9);
world = b2World(gravity);
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
// Define the ground box shape.
b2PolygonShape groundBox;
// The extents are the half-widths of the box.
groundBox.SetAsBox(50.0f, 10.0f);
// Add the ground fixture to the ground body.
groundBody->CreateFixture(&groundBox, 0.0f);
// Define the dynamic body. We set its position and call the body factory.
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(0.0f, 4.0f);
b2Body* body = world.CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(1.0f, 1.0f);
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
// Set the box density to be non-zero, so it will be dynamic.
fixtureDef.density = 1.0f;
// Override the default friction.
fixtureDef.friction = 0.3f;
// Add the shape to the body.
body->CreateFixture(&fixtureDef);
// 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 velocityIterations = 6;
int32 positionIterations = 2;
}
void update(){
// This is our little game loop.
for (int32 i = 0; i < 60; ++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, velocityIterations, positionIterations);
// Now print the position and angle of the body.
b2Vec2 position = body->GetPosition();
float32 angle = body->GetAngle();
printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
}
}
// PhysicsController.cpp
// cinderIOSTest1
//
//
#include "PhysicsController.h"
#include "cinder/Rand.h"
#include "cinder/gl/gl.h"
#include "cinder/app/AppBasic.h"
#include <Box2D/Box2D.h>
using namespace std;
PhysicsController::PhysicsController(){
}
void PhysicsController::setup(){
gravity = b2Vec2(0,0.9);
world = b2World(gravity);
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
// Define the ground box shape.
b2PolygonShape groundBox;
// The extents are the half-widths of the box.
groundBox.SetAsBox(50.0f, 10.0f);
// Add the ground fixture to the ground body.
groundBody->CreateFixture(&groundBox, 0.0f);
// Define the dynamic body. We set its position and call the body factory.
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(0.0f, 4.0f);
b2Body* body = world.CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(1.0f, 1.0f);
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
// Set the box density to be non-zero, so it will be dynamic.
fixtureDef.density = 1.0f;
// Override the default friction.
fixtureDef.friction = 0.3f;
// Add the shape to the body.
body->CreateFixture(&fixtureDef);
// 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 velocityIterations = 6;
int32 positionIterations = 2;
}
void update(){
// This is our little game loop.
for (int32 i = 0; i < 60; ++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, velocityIterations, positionIterations);
// Now print the position and angle of the body.
b2Vec2 position = body->GetPosition();
float32 angle = body->GetAngle();
printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
}
}
Any help much appreciated. Like I said, I'm from a Java/Actionscript background, so I'm still trying to get my head around C++. Sorry for being such a n00b 😉
Thanks in advance,
Alias