Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Physics For a 2D Game

Author
Message
Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 21st Jan 2010 03:28
Basically I want to implement the Box2D physics engine into Dark GDK. I was wondering if anyone had a good idea of how to do this or if it's even possible to do. Any help would be greatly appreciated.

Also if there isn't a way to implement that engine would it be possible to use the Dark physics engine with sprites. Really all I want to do is draw a simple box as a sprite (If I got it to work that simple box would then turn into characters and game objects), and then use the physics engine to draw a collision box around the sprite.
Jeason
14
Years of Service
User Offline
Joined: 13th Jan 2010
Location:
Posted: 21st Jan 2010 16:32
Quote: "Basically I want to implement the Box2D physics engine into Dark GDK. I was wondering if anyone had a good idea of how to do this or if it's even possible to do."


This is Easy I've done this before. Just Read the Tutorials then you know how you can do it.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 21st Jan 2010 17:54
It's really easy, I made a plugin to use it with DBPro.

There are loads of examples which come with the library, just use them as a starting point.

TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 21st Jan 2010 19:01 Edited at: 21st Jan 2010 19:23
I'm implementing Box2D into S3GE's Multi-App User Interface.

1. Download the Box2D zip.

2. Build the library in Release mode to generate box2d.lib. If you build in Debug mode you may get linker errors looking for missing debug libs when you build your project.

3. After successfully building the lib, You add the box2d.lib and box2d.h to your Project:
Project Properties -> Linker -> Input -> Additional Dependencies -> box2d.lib
Project Properties -> C/C++ -> General -> Additional Include Directories -> {Where you placed the Box2D Include and Source Folders & Files}

4. If you get this far, then check out the Box2D Manual for the example code to continue on to next steps.

5. Build a World Object. This is the first object you need to create and its basically a bounding box in which the simulation is managed. The size isn't critical, but a better fit will improve performance. It is better to make the box too big than to make it too small.

Box2D uses meters for units of scale, thus you will need to do some math conversions for equivalent pixel scale for DGDK. 1 meter = ~ 30 pixels.

I define some macros to ease calculations


Creating a World Object. You can follow the example code in the Box2D manual. I only list code snippets that require math conversions.


6. Build a Body Objects. Bodies are physics objects and they are created in a very specific order:
i. Define a body with a position, damping, etc.
ii. Use the world object to create the body.
iii. Define shapes with geometry, friction, density, etc.
iv. Create shapes on the body.
v. Optionally adjust the body's mass to match the attached shapes.

For simple Rectangles Box2D creates them using half-width, half-height.
.
These measurements require you to offset the sprite/image position to be displayed to match DGDK position coords.

Creating a Body. You can follow the example code in the Box2D manual. I only list code snippets that require math conversions.


7. DGDK Sprites. As stated before you have to do some math conversions to display a Sprite equivalent to the physics sim. This requires you to offset the sprite prior to display. I offset them upon creation.

Offsetting DGDK Sprites


Updating Objects. When updating each Body, perform your conversion and then display.


I hope you find this helpful. Good Luck.

Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 21st Jan 2010 21:41
Hey guys,
Thanks a lot for all this info. I actually was messing with it last night and got past all the linker errors so now I am on to the actual coding. I am going to spend most of the day trying to implement a sprite with physics. I will keep everyone updated on my progress and would greatly appreciate any help.

Also Techlord thanks a lot that's a great start. I hope you will be able to continue to help by following this thread, seeing as how this is my first time trying this. I will be posting as often as I can.

Poof Master
TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 22nd Jan 2010 00:03 Edited at: 22nd Jan 2010 00:04
Quote: "Also Techlord thanks a lot that's a great start. I hope you will be able to continue to help by following this thread, seeing as how this is my first time trying this. I will be posting as often as I can."
I'll try. Its my first time trying it too and I've only been programming with DGDK/C++ for a lil over 4 months - hehe. I have to try any of Box2D's fancy features, so we will be walking this path virtually together.

Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 28th Jan 2010 07:39
Sorry I wasn't able to post sooner I've been swamped in school work and haven't had much time to work on this.

So I followed your code TechLord and also went through the box2d documentation but I am having a few problems with getting it to work

I will post the different sections of the code:

So first is the creating the world and the box2d ground


Before I ask any questions I will just post the rest of the code which is in my main loop:




So when I run this the rigid body box goes through the static floor sprite but the center of the rigid body sprite collides with the bottom of the screen and/or the side of the screen(depending on how I change gravity). Does anyone have an idea of why this is happening?

- Poof Master
Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 29th Jan 2010 03:33
Update:

Basically I have boiled it down to one problem:


For some reason the dbSpriteWidth and Height doesn't work inside of this function. When I hard code the numbers to fit the parameters of my sprite the collision works fine. Like So:



This code works but I need it to be able to work with the dbSpriteWidth and Height function. Anyone know why it would not work with that dbSpriteWidth? I thought at first it was because it was an integer so I tried type casting it but it still gives me the same results. Any help would greatly be appreciated.

Login to post a reply

Server time is: 2024-10-01 23:28:15
Your offset time is: 2024-10-01 23:28:15