3Dgs Box2D wrapper

Posted By: 3dgs_snake

3Dgs Box2D wrapper - 03/01/11 06:20

Hi to All,

I have created a 3dgs Box2D(Box2D 2.1.2) physics plugin (Working with all A7/A8 editions). Actually it has all the Box2D functions, but I plan to integrate other additional functionalities, like buoyancy, gravity and others. All functions are quite the same as the C++ version except member properties are accessed through getters an setters, so Box2D knowledge can be used with Lite-C. I also plan to create some demos that use more advanced Box2D features.

To use it with 3DGS, Box2D performs the simulation, the simulation results are then used to animate (move and rotate) 2D Objects accordingly(ex PANELS).



Below are some testbed screens :

Ragdoll (From http://box2dflash.sourceforge.net/ )



Bridge (From Box2D TestBed)



Theo Jansen Walker (From box2D Testbed)


Best regards.
Posted By: fogman

Re: 3Dgs Box2D wrapper - 03/01/11 07:26

How cool is that, please? laugh
Great work!
Posted By: 3dgs_snake

Re: 3Dgs Box2D wrapper - 03/01/11 08:00

Hi Mr fogman,
This is the thing I told you grin. Unfortunately, I wasn't able to use the code you gave me, but I'm still finding a way to draw (and fill) primitives, so your code will be usefull.

Best regards.
Posted By: flits

Re: 3Dgs Box2D wrapper - 03/01/11 08:46

wow this is some good stuff
Posted By: 3dgs_snake

Re: 3Dgs Box2D wrapper - 03/01/11 09:03

Thanks,

Actually, it is my contest entry. You will be able to get it in the free resources very soon I think.

Have a nice day.
Posted By: ventilator

Re: 3Dgs Box2D wrapper - 03/01/11 11:02

awesome! laugh

i have some ideas for 2d physics games so this will be helpful. if only i had more time...

a while ago i tried to use chipmunk which is written in c. i thought maybe no wrapper would be needed but lite-c was too incompatible.
Posted By: Helghast

Re: 3Dgs Box2D wrapper - 03/01/11 11:22

YAY! we can make Angry Birds now wink
Posted By: 3dgs_snake

Re: 3Dgs Box2D wrapper - 03/01/11 11:23

Hi ventilator,

In the first time, I also thought about using chipmunk, and like you said, there were incompatibilities (I wanted to rewrite it but I failed), And after some tests I finally wrapped Box2D.

I am really waiting for the awesome things that others will create with it laugh .
Posted By: WretchedSid

Re: 3Dgs Box2D wrapper - 03/01/11 13:53

Originally Posted By: Helghast
YAY! we can make Angry Birds now wink


Na, that was already possible:



Nice contribution!
Posted By: 3run

Re: 3Dgs Box2D wrapper - 03/01/11 17:41

Man, I love it! Thanks a lot!
Posted By: 3dgs_snake

Re: 3Dgs Box2D wrapper - 03/02/11 09:11

Hi,

I have good news laugh , the plugin is now linked statically with runtime and doesn't require Visual C Runtime anymore (Box2D was generated with the MultiThreaded DLL, it is now fixed). I will upload it here after the contest entries are uploaded in the free ressources.

Best regards.
Posted By: 3dgs_snake

Re: 3Dgs Box2D wrapper - 03/04/11 08:20

Hello to all,

The plugin has been updated (monolithic header, static linking with runtime, added two advanced demos - using callbacks), it is more like a real plugin now. The download link is on
the first post .


Best regards.
Posted By: Inestical

Re: 3Dgs Box2D wrapper - 03/07/11 11:50

Awesome. I use Box2D in my current project, and it's great to see it's capabilities extended to to 3DGS laugh
Posted By: 3dgs_snake

Re: 3Dgs Box2D wrapper - 03/07/11 12:33

Hi,

Thanks and good luck for your project.
And I wish you could use the plugin to create some amazing things with 3DGS for your next project smile .

Best regards.
Posted By: Joozey

Re: 3Dgs Box2D wrapper - 03/07/11 17:45

Very nice work! laugh
Posted By: 3dgs_snake

Re: 3Dgs Box2D wrapper - 03/21/11 05:31

Hi to all,

The updated version of the plugin is now available on the first page of this post.

I'm now planning to add next stuffs like buoyancy and some little things. If you have some questions, or want some changes to be made, I will really do my best to answer them.

Download link on the first post

Best regards.
Posted By: 3dgs_snake

Re: 3Dgs Box2D wrapper - Test - 03/21/11 09:41

Me again,

I was obsessed by it for some times now, and I finally made a quick little test. It consist of manipulating a 3D entity with box2D so it will be 3D (with all the camera stuff), but the movement is constrained in 2 axis grin. Test it by yourself (Header and dll needed - download link on the first post).

Code:
//////////////////////////////
// @file
// Test - using box2D with entities
//
// Entity movement is constrained in y, z axis - Corresponding to box2D x, y
//////////////////////////////

#include <acknex.h>
#include <default.c>

#include "ackb2d.h"

#define PI 3.14159265
#define INV_PI 0.318309886

// Variables
b2World *world = NULL;
VECTOR gravity;
var phys_scale = 30;

// Box update function
void box_update()
{
	// The current entity body
	b2Body *box_body = NULL;
	
	// Create a box2D representation for the entity
	b2BodyDef *bodyDef = NULL;
	bodyDef = b2BodyDef_Create();
	b2BodyDef_SetType(bodyDef, B2_DYNAMICBODY);
	b2BodyDef_SetPosition(bodyDef, vector(-my->y / phys_scale, my->z / phys_scale, 0.0));
	// Body
	box_body = b2World_CreateBody(world, bodyDef);
	
	/// Box Polygon
	b2PolygonShape *dynamicBox = NULL;
	dynamicBox = b2PolygonShape_Create();
	b2PolygonShape_SetAsBox1(dynamicBox, 8 / phys_scale, 8 / phys_scale);
	
	// Fixture
	b2FixtureDef *fixtureDef = NULL;
	fixtureDef = b2FixtureDef_Create();
	b2FixtureDef_SetShape(fixtureDef, dynamicBox);
	b2FixtureDef_SetDensity(fixtureDef, 1.0);
	b2FixtureDef_SetRestitution(fixtureDef, 0.2);
	b2FixtureDef_SetFriction(fixtureDef, 0.3);
	
	b2Body_CreateFixture(box_body, fixtureDef);
	
	// Destroy unused objects
	b2BodyDef_Destroy(bodyDef);
	b2PolygonShape_Destroy(dynamicBox);
	b2FixtureDef_Destroy(fixtureDef);
		
	// Color entity
	set(my, LIGHT);
	vec_set(my->blue, vector(10+random(150), random(10), random(150)));
	
	// Display shadow
	set(my, SHADOW);
	
	VECTOR position;
	var angle;
	
	while(1)
	{
		// Get properties from physics engine
		b2Body_GetPosition(box_body, &position);
		angle = b2Body_GetAngle(box_body);
		
		// Copy changes into shape objects
		vec_set(my.x, vector(0, position.x * phys_scale, position.y * phys_scale));
		my->roll = 180 * angle * INV_PI;
		
		wait(1);
	}
}

// Sphere update function
void sphere_update()
{
	// The current entity body
	b2Body *box_body = NULL;
	
	// Create a box2D representation for the entity
	b2BodyDef *bodyDef = NULL;
	bodyDef = b2BodyDef_Create();
	b2BodyDef_SetType(bodyDef, B2_DYNAMICBODY);
	b2BodyDef_SetPosition(bodyDef, vector(-my->y / phys_scale, my->z / phys_scale, 0.0));
	// Body
	box_body = b2World_CreateBody(world, bodyDef);
	
	/// Box circle
	b2CircleShape *dynamicCircle = b2CircleShape_Create();
	b2CircleShape_SetRadius(dynamicCircle, 8 / phys_scale);
	
	// Fixture
	b2FixtureDef *fixtureDef = NULL;
	fixtureDef = b2FixtureDef_Create();
	b2FixtureDef_SetShape(fixtureDef, dynamicCircle);
	b2FixtureDef_SetDensity(fixtureDef, 1.0);
	b2FixtureDef_SetRestitution(fixtureDef, 0.6);
	b2FixtureDef_SetFriction(fixtureDef, 0.3);
	
	b2Body_CreateFixture(box_body, fixtureDef);
	
	// Destroy unused objects
	b2BodyDef_Destroy(bodyDef);
	b2CircleShape_Destroy(dynamicCircle);
	b2FixtureDef_Destroy(fixtureDef);
		
	// Color entity
	set(my, LIGHT);
	vec_set(my->blue, vector(10+random(150), random(10), random(150)));
	
	// Display shadow
	set(my, SHADOW);
	
	VECTOR position;
	var angle;
	
	while(1)
	{
		// Get properties from physics engine
		b2Body_GetPosition(box_body, &position);
		angle = b2Body_GetAngle(box_body);
		
		// Copy changes into shape objects
		vec_set(my.x, vector(0, position.x * phys_scale, position.y * phys_scale));
		my->roll = 180 * angle * INV_PI;
		
		wait(1);
	}
}

//////////////////////////////
// Close engine on escape
//////////////////////////////
void esc_handler()
{
	//////////////////////////////
	// Destroy world
	//////////////////////////////	
	b2World_Destroy(world);
	sys_exit("");
}

// Main test
void main()
{
	// Set video mode
	video_mode = 4;
	// limit fps to be in sync with box2D timestep
	fps_max = 60;
	
	// Randomize
	random_seed(0);
	
	// Shadow type
	shadow_stencil = 2;
	
	// Wait for graphics to be ready
	// Wait for the execution of startup functions
	wait(1);
	
	// Set key handler
	on_esc = esc_handler;
	
	// load empty level
	level_load(NULL);
	
	vec_set(&gravity, vector(0.0, -10.0, 0.0));
	var do_sleep = 1;
	
	world = b2World_Create(&gravity, do_sleep);
	
	// Set debug draw to default
	ackDebugDraw *debug = ackDebugDraw_Create(phys_scale);
	ackDebugDraw_SetFlags(debug, E_SHAPEBIT | E_JOINTBIT);
	b2World_SetDebugDraw(world, debug);
	
	// Create entities
	ent_create("cube.mdl", vector(0, -8, 120), box_update);
	ent_create("cube.mdl", vector(0, 8, 150), box_update);
	ent_create("sphere.mdl", vector(0, -8, 200), sphere_update);
	ent_create("sphere.mdl", vector(0, 8, 200), sphere_update);
	
	// position camera
	vec_set(camera.x, vector(-212, -3, 56));
	//set(camera, ISOMETRIC);
	
	/// Body def
	b2BodyDef *groundBodyDef = NULL;
	groundBodyDef = b2BodyDef_Create();
	b2BodyDef_SetPosition(groundBodyDef, vector(0.0, -10.0, 0.0));
	
	/// Body
	b2Body *groundBody = b2World_CreateBody(world, groundBodyDef);
	// Create ground entity
	ENTITY *ground_ent = ent_create("cube.mdl", nullvector, NULL);
	//set(ground_ent, LIGHT);
	vec_set(ground_ent->blue, vector(0, 255, 255));
	
	ground_ent->scale_x = 20;
	ground_ent->scale_y = 40;
	c_updatehull(ground_ent, 1);
	ground_ent->z -= 8;
	
	/// Ground Polygon
	b2PolygonShape *groundBox = NULL;
	groundBox = b2PolygonShape_Create();
	b2PolygonShape_SetAsBox1(groundBox, 50.0, 10.0);
	
	b2Body_CreateFixture2(groundBody, groundBox, 0);
		
	var velocity_iterations = 6;
	var position_iteration = 2;
	var timeStep = 1.0 / 60.0;
	
	while(1)
	{
		// Step in 2d world
		b2World_Step(world, timeStep, velocity_iterations, position_iteration);
		b2World_ClearForces(world);
				
		wait(1);
	}
}



Best regards.
Posted By: ventilator

Re: 3Dgs Box2D wrapper - Test - 03/21/11 12:04

as a successor to the 256kb contest a 2d physics game contest would be fun. laugh
Posted By: 3dgs_snake

Re: 3Dgs Box2D wrapper - Test - 03/21/11 12:31

Yes, that is a good idea,

we will then see plenty of good 2d examples grin
Posted By: 3dgs_snake

Re: 3Dgs Box2D wrapper - Test - 10/10/12 08:58

Hi,

It is an old thread, I know ( tongue ), but some days ago someone asked me about an example using the plugin (2.5D with WED created level), so I will post here some (I hope ...) usefull links :




The first link points to a simple (not verry useful) platformer with all the files I used to create it. The sample uses a 3d level created in wed. For this kind of setup, you need 2 types of representations for your model (3D representation created in wed and a 2D representation to be used in box2D). My workflow was the following :

  • Created a level in WED (The details for the collisions)
  • Save the level grin
  • Export the level to MDL
  • Export the MDL to 3ds using MED
  • Open the 3ds with blender
  • Created a plane at the position you want your player to move (For a 2D outline)
  • Created the outline using the boolean modifier
  • Exported the model to 3ds
  • Reimporrt the outline into MED (to be loadable in 3DGS - You can also load a 3ds but have not tried)
  • Used the outline to create collision data (with ent_buffers)
  • Load the level and create collision data
  • Create player and colision data
  • ...


The second link points to a Box2D tutorial. The plugin is more like a wrapper than a real plugin (But you can create some superset functions based on your needs (PANEL, ENTITY or draw_) so it is quite easy to translate any Box2D application from other languages (Flash, C++) to Lite-C.

Hope that was useful.
Posted By: rojart

Re: 3Dgs Box2D wrapper - Test - 10/19/12 06:57

Thanks, will be useful to the next project.
Posted By: catsup

Re: 3Dgs Box2D wrapper - 09/18/14 23:38

This contribution actually got me back to 3D GameStudio for another 2D project. Thanks laugh
© 2024 lite-C Forums