Hi, I chose a physics engine recently. It turned out that I did not find it better than box2d.

I struggle with this engine for a long time. And while nothing comes out ... Even the manual world could not create a console world. Some authors have a discrepancy between theory and practice. Can anyone share a project where the world is just being created for example, or is something being created at all? I want to write a simple igruhu, for example, like this: Splasher Game

Thank)


Many thanks for the code! But it does not work ....

#include<Box2D\Box2D.h> #include<stdlib.h> void main(){ const b2Vec2 gravity(0.0f, -10.0f); b2World world(gravity); } 

Crashes:

  1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall b2World::~b2World(void)" (??1b2World@@QAE@XZ) referenced in function _main error LNK2019: unresolved external symbol "public: __thiscall b2World::b2World(struct b2Vec2 const &)" (??0b2World@@QAE@ABUb2Vec2@@@Z) referenced in function _main 

Of course, I understand that before the question I have to google .... I have already googled such errors, but I did not recognize anything useful .... The release link does not work ...



    1 answer 1

    Here are a couple of good links:

    What language do you write in? I used the c # engine. I bring pieces of my code. :)

    Create the world

     world_ = new World(border_, gravity_, true);// world 

    Create an object of some kind

     _polygon = new Rectangle(); myBrush = Brushes.Red; /// пол _polygon = new Rectangle(); _polygon.Fill = myBrush; _polygon.Width = width; _polygon.Height = 10; Canvas.SetLeft(_polygon, 0); Canvas.SetTop(_polygon, heigth - 10); canvas1.Children.Add(_polygon); Body polBody_; BodyDef polDef = new BodyDef(); polDef.Position.Set((float)width / 2, (float)(heigth - 10) + 5); polDef.UserData = _polygon; PolygonDef shapepolDef = new PolygonDef(); shapepolDef.SetAsBox((float)((_polygon.Width) / 2), (float)((_polygon.Height) / 2)); shapepolDef.Density = (float)0.0; // плотность shapepolDef.Friction = (float)1.0; // коэффициент трения shapepolDef.Restitution = (float)0.1;//упругость polBody_ = world_.CreateBody(polDef); polBody_.CreateShape(shapepolDef); polBody_.SetMassFromShapes(); 

    Then create a timer

     public void time() { timer.Tick += new EventHandler(delegate(object s, EventArgs a) { updateWorld(); }); } 

    And in the method draw each shape

     private void updateWorld() { for (Body list_ = world_.GetBodyList(); list_ != null; list_ = list_.GetNext()) { if (list_.IsStatic()) { continue; } if (list_.GetUserData() == listRectangle[countR]) { vec = list_.GetPosition(); float angle = (float) (list_.GetAngle() * (180 / System.Math.PI)); RotateTransform rot = new RotateTransform(); angle = angle - (float) System.Math.Truncate(angle / 360) * 360; rot.Angle = angle; rot.CenterX = 0; rot.CenterY = 0; listRectangle[countR].RenderTransform = rot; double Xn = 0, Yn = 0; Xn = (-dlinaRectangle[countR]) * System.Math.Cos(list_.GetAngle()) - (-chirinaRectangle[countR]) * System.Math.Sin(list_.GetAngle()) + vec.X; Yn = (-dlinaRectangle[countR]) * System.Math.Sin(list_.GetAngle()) + (-chirinaRectangle[countR]) * System.Math.Cos(-list_.GetAngle()) + vec.Y; Canvas.SetLeft(listRectangle[countR], Xn); Canvas.SetTop(listRectangle[countR], Yn); countR--; } } 
    • I am in C / C ++ ... I have seen enough of the lessons so much ..... I’ve seen these too ... I really need a project with C ++ and a manual for version 2.2.1 - Alerr
    • @sergileon, To format the code, select it with the mouse and click on the button 101010 of the editor. - Nicolas Chabanovsky
    • Well, already underlines ... gravity needed to be created the same? The studio is not satisfied with the first line .. (world_ = new World (border, gravity, true); // world I removed underscores if anything ... Does this code really work? I connected the boxing library, I recorded the first line of your code in mine and ... error C2065: 'world': undeclared identifier error C2061: syntax error: identifier 'World' What's wrong ???? ??????? - Alerr
    • one
      @Alerr I would start with C++ Primer , not Box2D . - Costantino Rupert
    • one
      @Alerr - You were given C# code as an example, of course, it will not compile in C++ . - Sketched for you a completely trivial example. - In the release of Box2D_v2.2.1.zip there is a Documentation\API, folder in which you can find the actual Doxygen documentation for the current version of Box2D. - Costantino Rupert