Qt Quick + OpenGL + Bullet initial test

Source: Internet
Author: User

Qt Quick + OpenGL + Bullet initial test

QT's quick modules are already performing very well and can be reserved for the interface to render OpenGL scenes. In general, most programming needs can be met. This time, try to use a combination of techniques to do something interesting-integrating bullet and testing.

Saiyang original article, starting address: http://blog.csdn.net/gamesdev/article/details/44284317. Welcome to come to discuss the peer.

The choice of bullet, in fact, is also a particular attention. Currently bullet 2.82 version, not updated, and bullet 3.x, it is also a rare application. If we went to see Bullet's new codebase--github, we would find that in "Requirementsfor Bullet 2", the author said that almost any compiler could compile its code, and in the introduction of "Requirements for Bullet 3", Just say to be able to run in the high-end graphics card, low-end graphics or mobile devices may be bullet 3. So in this case, I chose bullet 2.82来 to make this example.

To complete this example, I refer to some examples of predecessors, such as QT in the Nokia period, wrote a great bullet + qt example, called Bulletdice. Its github address is here. This edge is relatively simple, easy to get started, plus bullet has a manual, the two combined to see, convenient a lot. It took a week to finally imitate and produce the effect:

At first, the cube is in the upper position of the plane, and then, according to Gravity, the velocity of free falling falls slowly. Eventually landed on the ground. Indicates the appearance of falling:

What do you think? It's simple. Here is also my preliminary understanding of bullet. In the process of writing this example, many difficulties have been encountered, and there are many areas worth scrutiny. So I'm going to make a note here and make a reference for the future.

First, in order to verify that the physics engine is available, I wrote a function called Debugshow () in the class, each time the simulation is sampled, the position of the output cube. On the basis of verifying that there is no problem, we begin to study how to combine data with rendering. The following code slice describes some of the knowledge associated with bullet:

void Dynamicsworldprivate::initializephysics (void) {//Initialize physical engine m_broadphase = new Btdbvtbroadphase;    m_conf = new Btdefaultcollisionconfiguration;    M_dispatcher = new Btcollisiondispatcher (m_conf);    M_solver = new Btsequentialimpulseconstraintsolver;                                           Apply Gravity M_world = new Btdiscretedynamicsworld (M_dispatcher, M_broadphase,    M_solver, m_conf);    M_world->setgravity (BtVector3 (0,-9.81, 0));    Create a planar rigid body createplane (); Create a cube rigid body createcube ();}    void Dynamicsworldprivate::releasephysics (void) {delete m_world; m_world = q_nullptr; Delete M_solver;    M_solver = q_nullptr; Delete m_conf;    m_conf = q_nullptr; Delete M_dispatcher;    M_dispatcher = q_nullptr; Delete m_broadphase;    M_broadphase = q_nullptr;    Qdeleteall (m_shapes);    Qdeleteall (m_bodies);    M_shapes.clear (); M_bodies.clear ();} void DYNAMICSWOrldprivate::createplane (void) {btstaticplaneshape* planeshape = new Btstaticplaneshape (BtVector3 (0,                      1, 0),//Plane normal 0.0);    The distance of the relative origin of the plane bttransform Origintransform;    Origintransform.setfromopenglmatrix (M_planemodelmatrix->data ());                btdefaultmotionstate* motionstate = new Btdefaultmotionstate (origintransform,//Start transform  Bttransform::getidentity ());                Center Shift Amount Btrigidbody::btrigidbodyconstructioninfo planeinfo (0,//Mass  Motionstate,//Motion state planeshape,//Collision shape BtVector3 (0,     0, 0));    Local inertia m_planebody = new Btrigidbody (planeinfo);    M_world->addrigidbody (M_planebody);    M_shapes.append (Planeshape); M_bodies.append (m_planebody);}    void Dynamicsworldprivate::createcube (void) {qreal semi = m_cubelength/2.0; Btboxshape* Cubeshape = new Btboxshape (BtVector3 (semi, Semi, semi));//half cube size Bttransform origintransform;    Origintransform.setfromopenglmatrix (M_cubemodelmatrix->data ());                btdefaultmotionstate* motionstate = new Btdefaultmotionstate (origintransform,//Start transform  Bttransform::getidentity ());                Center Shift Amount Btrigidbody::btrigidbodyconstructioninfo Cubeinfo (0.8f,//Mass Motionstate,//Motion state cubeshape,//Collision shape BtVector3 (0,     0, 0));              Local inertia cubeinfo.m_friction = 0.3;           Friction cubeinfo.m_restitution = 0.1;    Rebound Force (resilience) M_cubebody = new Btrigidbody (cubeinfo);    M_world->addrigidbody (M_cubebody);    M_shapes.append (Cubeshape); M_bodies.append (m_cubebody);}    void Dynamicsworldprivate::simulate (void) {Qtime curtime = Qtime::currenttime (); int steptime = M_lastTime.msecsto (Curtime);    M_lasttime = Curtime; M_world->stepsimulation (Btscalar (steptime) * 0.001);    void Dynamicsworldprivate::d ebugshow (void) {Bttransform worldtransform;    M_cubebody->getmotionstate ()->getworldtransform (worldtransform);    BtVector3 position = worldtransform * BTVECTOR3 (0, 0, 0); Qdebug ("position is: (%.2f,%.2f,%.2f)", position.x (), POSITION.Y (), position.z ());}

Subsequently, it may be considered that in this model, there is only one force object, that is, "Earth", it applied gravity to the cube, let the cube do free fall, the trajectory of the movement of the sample through the body motionstate in the Worldtransform to express. If combined with the rendering engine, my approach is that the physics engine will eventually only modify the object's Modelmatrix, the model's own shape controls the primitive position, the camera's parameters control the Viewmatrix and ProjectionMatrix. Only in this way, everything seems to be a clear concept.

There may be a tricky problem here, and sometimes you need to manually control the Modelmatrix to change the model's transformation information, but Modelmatrix is also controlled by the physics engine, so for Modelmatrix to be controlled by the manual control and the physical engine, is more difficult to deal with, next time I look at the study in depth, can not clarify their previous relationship.

Finally, I also tested success on my Android tablet. Note that bullet is fully capable of combining QT to cross-platform.

Qt Quick + OpenGL + Bullet initial test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.