My first post here becouse I just like 2 days ago, for the first time, made box2d compile whit qt.
My problem is that i can't remove more then 2 bodies at the same time. The app crashes for some reson
when i try to. I try removin some stuff before I do the world step. When i try to only remove one everything
works great.
i get the error code 1073741819 if it says anything
the implemetation file for the world
#include "world.h"
World::World(){
}
void World::init_world( GLfloat worldWidth, GLfloat worldHeight, b2Vec2 gravity, bool doSleep ){
maxBodies = 70;
bodyCount = 0;
worldAABB.lowerBound.Set( -1.0f*( worldWidth/2.0f ), -1.0f*( worldHeight/2.0f ) );
worldAABB.upperBound.Set( worldWidth/2.0f, worldHeight/2.0f );
this->gravity = gravity;
world = new b2World( worldAABB, gravity, doSleep );
worldBody = new WorldBody( 0.0f, 0.0f, worldWidth, worldHeight );
emit new_object_made( worldBody );
emit need_repaint();
}
void World::build_world( QList<BodyForm>bodyList ){
QListIterator<BodyForm> list( bodyList );
while( list.hasNext() ){
if( bodyCount != maxBodies ){
BodyForm temp = list.next();
if( temp.type == BOX ){
Square *ob = new Square( temp.x, temp.y, temp.width, temp.height, temp.angle, temp.friction, world );
if( temp.isDynamic )
ob->make_dynamic( temp.density );
this->bodyList << ob;
emit new_object_made( ob );
}
else if( temp.type == CIRCLE ){
Circle *ob = new Circle( temp.x, temp.y, temp.r, temp.angle, temp.friction, world );
if( temp.isDynamic )
ob->make_dynamic( temp.density );
this->bodyList << ob;
emit new_object_made( ob );
}
bodyCount += 1;
} else
return;
}
emit need_repaint();
}
void World::Step( GLfloat timeStep, int iterations ){
world->Step( timeStep, iterations );
emit need_repaint();
}
void World::build_new_object( BodyForm &form ){
if( bodyCount != maxBodies ){
if( form.type == BOX ){
Square *ob = new Square( form.x, form.y, form.width, form.height, form.angle, form.friction, world );
if( form.isDynamic )
ob->make_dynamic( form.density );
bodyList << ob;
emit new_object_made( ob );
}
else if( form.type == CIRCLE ){
Circle *ob = new Circle( form.x, form.y, form.r, form.angle, form.friction, world );
if( form.isDynamic )
ob->make_dynamic( form.density );
bodyList << ob;
emit new_object_made( ob );
}
bodyCount += 1;
}
}
void World::delete_inactive_objects(){
QListIterator<RBodyInWorld *> list( bodyList );
bool objectRemoved = false;
for( int i = 0; list.hasNext(); i++ ){
RBodyInWorld *temp = list.next();
bool remove = false;
if( temp->get_frozen() )
remove = true;
else if( temp->get_time_for_desposal() )
remove = true;
if( remove ){
temp->delete_body( world );
temp->set_type( DELETED );
bodyList.removeAt( i );
objectRemoved = true;
}
}
emit object_has_been_deleted();
}
void World::set_gravity( b2Vec2 gravity ){
world->SetGravity( gravity );
this->gravity = gravity;
wake_up_all();
}
void World::wake_up_all(){
QListIterator<RBodyInWorld *> list( bodyList );
while( list.hasNext() )
list.next()->set_wake_up( true );
}
void World::remove_all_dynamic_bodies(){
QListIterator<RBodyInWorld *> list( bodyList );
while( list.hasNext() ){
RBodyInWorld *temp = list.next();
if( temp->get_dynamic() )
temp->set_time_for_desposal( true );
}
}
the code in the box that removes it
i relly would like to be able to remove more then one box at a time.
My problem is that i can't remove more then 2 bodies at the same time. The app crashes for some reson
when i try to. I try removin some stuff before I do the world step. When i try to only remove one everything
works great.
i get the error code 1073741819 if it says anything
the implemetation file for the world
Code:
#include "world.h"
World::World(){
}
void World::init_world( GLfloat worldWidth, GLfloat worldHeight, b2Vec2 gravity, bool doSleep ){
maxBodies = 70;
bodyCount = 0;
worldAABB.lowerBound.Set( -1.0f*( worldWidth/2.0f ), -1.0f*( worldHeight/2.0f ) );
worldAABB.upperBound.Set( worldWidth/2.0f, worldHeight/2.0f );
this->gravity = gravity;
world = new b2World( worldAABB, gravity, doSleep );
worldBody = new WorldBody( 0.0f, 0.0f, worldWidth, worldHeight );
emit new_object_made( worldBody );
emit need_repaint();
}
void World::build_world( QList<BodyForm>bodyList ){
QListIterator<BodyForm> list( bodyList );
while( list.hasNext() ){
if( bodyCount != maxBodies ){
BodyForm temp = list.next();
if( temp.type == BOX ){
Square *ob = new Square( temp.x, temp.y, temp.width, temp.height, temp.angle, temp.friction, world );
if( temp.isDynamic )
ob->make_dynamic( temp.density );
this->bodyList << ob;
emit new_object_made( ob );
}
else if( temp.type == CIRCLE ){
Circle *ob = new Circle( temp.x, temp.y, temp.r, temp.angle, temp.friction, world );
if( temp.isDynamic )
ob->make_dynamic( temp.density );
this->bodyList << ob;
emit new_object_made( ob );
}
bodyCount += 1;
} else
return;
}
emit need_repaint();
}
void World::Step( GLfloat timeStep, int iterations ){
world->Step( timeStep, iterations );
emit need_repaint();
}
void World::build_new_object( BodyForm &form ){
if( bodyCount != maxBodies ){
if( form.type == BOX ){
Square *ob = new Square( form.x, form.y, form.width, form.height, form.angle, form.friction, world );
if( form.isDynamic )
ob->make_dynamic( form.density );
bodyList << ob;
emit new_object_made( ob );
}
else if( form.type == CIRCLE ){
Circle *ob = new Circle( form.x, form.y, form.r, form.angle, form.friction, world );
if( form.isDynamic )
ob->make_dynamic( form.density );
bodyList << ob;
emit new_object_made( ob );
}
bodyCount += 1;
}
}
void World::delete_inactive_objects(){
QListIterator<RBodyInWorld *> list( bodyList );
bool objectRemoved = false;
for( int i = 0; list.hasNext(); i++ ){
RBodyInWorld *temp = list.next();
bool remove = false;
if( temp->get_frozen() )
remove = true;
else if( temp->get_time_for_desposal() )
remove = true;
if( remove ){
temp->delete_body( world );
temp->set_type( DELETED );
bodyList.removeAt( i );
objectRemoved = true;
}
}
emit object_has_been_deleted();
}
void World::set_gravity( b2Vec2 gravity ){
world->SetGravity( gravity );
this->gravity = gravity;
wake_up_all();
}
void World::wake_up_all(){
QListIterator<RBodyInWorld *> list( bodyList );
while( list.hasNext() )
list.next()->set_wake_up( true );
}
void World::remove_all_dynamic_bodies(){
QListIterator<RBodyInWorld *> list( bodyList );
while( list.hasNext() ){
RBodyInWorld *temp = list.next();
if( temp->get_dynamic() )
temp->set_time_for_desposal( true );
}
}
the code in the box that removes it
Code:
#ifndef RBODYINWORLD_H
#define RBODYINWORLD_H
#include "rbody.h"
#include "Box2D.h"
#include "../../values.h"
#include <QtOpenGL>
class RBodyInWorld : public RBody{
public:
virtual GLfloat get_x(){ return this->body->GetPosition().x; }
virtual GLfloat get_y(){ return this->body->GetPosition().y; }
virtual GLfloat get_angle(){ return (GLfloat)this->body->GetAngle() * ( 180.0f / (GLfloat)b2_pi ); }
virtual bool get_dynamic(){ return this->isDynamic; }
virtual bool get_frozen(){ return this->body->IsFrozen(); }
virtual void set_dynamic( bool isDynamic){ this->isDynamic = isDynamic; }
virtual void set_wake_up( bool wakeUp ){ if( wakeUp ) this->body->WakeUp(); }
virtual void delete_body( b2World *world ){ world->DestroyBody( this->body ); }
protected:
b2Body *body;
bool isDynamic;
};
#endif // RBODYINWORLD_H
#define RBODYINWORLD_H
#include "rbody.h"
#include "Box2D.h"
#include "../../values.h"
#include <QtOpenGL>
class RBodyInWorld : public RBody{
public:
virtual GLfloat get_x(){ return this->body->GetPosition().x; }
virtual GLfloat get_y(){ return this->body->GetPosition().y; }
virtual GLfloat get_angle(){ return (GLfloat)this->body->GetAngle() * ( 180.0f / (GLfloat)b2_pi ); }
virtual bool get_dynamic(){ return this->isDynamic; }
virtual bool get_frozen(){ return this->body->IsFrozen(); }
virtual void set_dynamic( bool isDynamic){ this->isDynamic = isDynamic; }
virtual void set_wake_up( bool wakeUp ){ if( wakeUp ) this->body->WakeUp(); }
virtual void delete_body( b2World *world ){ world->DestroyBody( this->body ); }
protected:
b2Body *body;
bool isDynamic;
};
#endif // RBODYINWORLD_H
i relly would like to be able to remove more then one box at a time.