weird simulation

General discussion about Box2D tutorials
klemen
Posts: 11
Joined: Fri Feb 20, 2015 8:27 am

Re: weird simulation

Post by klemen » Sun Feb 22, 2015 8:49 am

Thank you for taking your time on this issue.

Everything in debug draw works as it should. With or without bullets, simulation in debug draw is flawless.... so, obviously there is something wrong with my program.

It doesn't matter how simple i make my scene... if there are bullets, sprites just aren't rendered as they should be.

I just don't get this issue... why would my program render complex scene right, if there are no bullets and completely wrong if these same objects are set as bullets.

I know there is not much you can help me in this situation... so... i really appreciate taking your time.

thank you,
Klemen

klemen
Posts: 11
Joined: Fri Feb 20, 2015 8:27 am

Re: weird simulation

Post by klemen » Sun Feb 22, 2015 3:10 pm

I've found a bug in my code.

There were two same calls for loading from json file. My scene had a duplicate for every object. Of course i was rendering just one. It seems, that this worked if there were no bullets. If there were bullets, those duplicates were pulled apart - hence invisible edges of the objects.

Well... this really made my day :)
Thank you again...

bye,
Klemen

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: weird simulation

Post by iforce2d » Sun Feb 22, 2015 6:43 pm

Good to hear you figured it out :)

I get the feeling you are still not using debug draw correctly since you talk about it as being something separate from your program, and you say it worked correctly with or without bullets when you were using debug draw. If there was a bug in your code, how could the debug draw have worked correctly with bullets?

The procedure for using debug draw is outlined here: http://www.iforce2d.net/b2dtut/debug-draw
You are supposed to write a class that extends the b2Draw class (previously named b2DebugDraw in older versions of Box2D) and implement all the virtual functions of that class. They are simple generic functions like drawing lines, circles, polygons etc. which you need to implement for whatever type of rendering framework you're using. Then Box2D will call your functions, telling you exactly where everything is, and makes your life a lot easier. In this particular case, you would have seen two of everything in the world and immediately had a clue that there was a problem in the loading stage.

klemen
Posts: 11
Joined: Fri Feb 20, 2015 8:27 am

Re: weird simulation

Post by klemen » Mon Feb 23, 2015 8:14 am

Yes. It was separated from my program. I used the testbed that ships with box2d. I exported scene from RUBE editor and paste it in testbed.

If i would want to use b2Draw, I would have to completely change my program, since almost every part of it has Polycode calls in it. I would do that... but i was sure there is nothing wrong with the way I draw my sprites.

Well... i will not assume that next time. :)

Thank you again for taking your time on this...

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: weird simulation

Post by iforce2d » Mon Feb 23, 2015 11:09 am

Using debug draw should not require changing your program much at all - if it did then nobody would be interested in using it and it would be a waste of time even making it.

Debug draw assumes that whatever framework you are using, you will know how to draw basic primitives like lines and polygons etc. For example, you need to fill in the contents of a function like this:

Code: Select all

virtual void DrawPolygon(b2Vec2* vertices, int32 vertexCount, b2Color& color) {
    // draw the polygon however PolyCode draws polygons
}
Presumably you know how to draw stuff because you're doing it for other things already. In this function you just do it using the points and color that Box2D gives you - it's actually easier than drawing your own shapes because the body transforms for all the points have already been applied.

Then you make an instance of that class and pass it to b2World::SetDebugDraw, and call b2World::DrawDebugData at some point in your rendering code. Note that you also need to set which features of the world you want to draw, by using the SetFlags function of the instance you made. All the details are on the page I linked to above.

As you have already noticed, trying to figure out what is going on without the debug draw is literally like being blindfolded. I would highly recommend stopping whatever else you are doing and get it set up. Even if it takes 2 hours it would be 2 hours that serves you well in the future, because if you use Box2D to any degree you can bet this wont be the last time you find yourself looking at something and wondering what the hell is going on :)

klemen
Posts: 11
Joined: Fri Feb 20, 2015 8:27 am

Re: weird simulation

Post by klemen » Mon Feb 23, 2015 11:53 am

You are right. Since drawing functions are virtual, i can implement them with Polycode functions.
And yes... this wouldn't change my program a lot. I will do that.

Oh... and another thing.... i would like to to say, that I am (and may others too) very impressed with your work with open source projects. Tutorials, videos, RUBE, answers on forums... are really really great. I really appreciate this. Keep on the good work. :)

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: weird simulation

Post by iforce2d » Mon Feb 23, 2015 12:33 pm

Thanks!
Actually.... I downloaded and built PolyCode to check it out, and it seems like it has very tight control over the Box2D engine making it hard to do any customization... how did you manage to load a scene from .json at all I wonder? Are you using any of the PolyCode physics stuff (eg. PhysicsScene2D) or are you just using PolyCode only for rendering and sound etc?

klemen
Posts: 11
Joined: Fri Feb 20, 2015 8:27 am

Re: weird simulation

Post by klemen » Mon Feb 23, 2015 2:05 pm

I dont use physics feature of Polycode.
Its based on Box2D 2.1.0, so i use it only for graphics and sound. It doesnt have support for joints, that I need.

If you look the source code of Polycode, you will see, that code is very well structured and written. I think adding support for other joints isn't that hard. Even writing loader for json files shouldn't be that hard. I will try to contribute to this project in the future.

The thing I like about Polycode is, that it is multi-platform - only Polycode core initialisation is different for Windows, OSX and Linux. Everything else is platform independent. And not to mention, that it takes care of other things, that make OpenGL programming difficult - allocating vertex buffers on GPU....

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: weird simulation

Post by iforce2d » Mon Feb 23, 2015 4:22 pm

In that case, you will have good control over the Box2D part of it, and you should be able to set up a debug draw class pretty much as I've described. You will need to be able to draw lines... couldn't find any examples of drawing lines but it seems to do polygons easily.

Post Reply