Qt Quick + OpenGL + Bullet initial test, qtbullet

Source: Internet
Author: User

Qt Quick + OpenGL + Bullet initial test, qtbullet
Qt Quick + OpenGL + Bullet initial test

Currently, the Quick module of Qt has been outstanding, and an interface can be reserved for rendering OpenGL scenarios. Generally, it can meet most programming needs. This time, I tried to combine some technologies to do something interesting-integrate Bullet and perform tests.

Jiang caiyang's original article, first published at: http://blog.csdn.net/gamesdev/article/details/44284317. Welcome to the discussion.

The selection of Bullet is also very exquisite. Currently, Bullet version 2.82 is not updated yet, while Bullet 3.xis rarely used. If we look at the new code base of Bullet-github, we will find that in "Requirementsfor Bullet 2", the author says that almost any compiler can compile its code, in the introduction of "Requirements for Bullet 3", it only means that it can run in a high-end video card. The low-end video card or mobile device may not be able to run with Bullet 3. In this case, I chose Bullet 2.82 to create this example.

To complete this example, I have referred to some examples from our predecessors. For example, Qt wrote a great Bullet + Qt example called BulletDice during the Nokia period. Its github address is here. This is relatively simple and easy to use. In addition, the Bullet has a Manual, which makes it much easier to combine. It took a week to finally imitate and make the effect:

At first, the cube is located on the top of the plane. Then, based on the gravity, the cube falls slowly at a free falling speed. It finally fell onto the ground. It indicates what it looks like when it falls down:

How is it? It's easy. This is my preliminary understanding of Bullet. During the compilation of this example, we encountered many difficulties and many points worth further consideration. So I will take a note here for future reference.

First, to verify the availability of the physical engine, I wrote a function in the class called debugShow (), which outputs the position of the cube each time a simulation sample is performed. On the basis of the validation, we began to study how to combine data with rendering. The following code snippets describe some knowledge about Bullet:

 

Void DynamicsWorldPrivate: initializePhysics (void) {// initialize the physical engine m_broadPhase = new btDbvtBroadphase; m_conf = new btdefacolcollisionconfiguration; m_dispatcher = new handler (m_conf); m_solver = new handler; // application gravity m_world = new btDiscreteDynamicsWorld (m_dispatcher, m_broadPhase, m_solver, m_conf); m_world-> setGravity (btVector3 (0,-9.81, 0 )); // create a plane ry createPlane (); // create a cube ry createCube ();} void DynamicsWorldPrivate: releasePhysics (void) {delete m_world; m_world = Q_NULLPTR; delete m_solver; m_solver = delimiter; delete m_conf; m_conf = delimiter; delete m_dispatcher; m_dispatcher = delimiter; delete m_broadPhase; m_broadPhase = delimiter; 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 from the relative origin of the plane to btTransform originTransform; originTransform. transform (m_planeModelMatrix-> data (); btdefamomotionstate * motionState = new btdefamomotionstate (originTransform, // start the transformation btTransform: getIdentity (); // center translation btRigidBody :: btRigidBodyConstructionInfo planeInfo (0, // quality motionState, // motion state planeShape, // collision shape btVector3 (0, 0, 0 )); // local inertia m_planeBody = new inline (planeInfo); m_world-> addRigidBody (m_planeBody); inline (planeShape); rotate (m_planeBody);} void DynamicsWorldPrivate: createCube (void) {qreal semi = m_cubeLength/2.0; btBoxShape * cubeShape = new btBoxShape (btVector3 (semi, semi, semi); // the size of half a cube btTransform originTransform; originTransform. convert (m_cubeModelMatrix-> data (); btdefamomotionstate * motionState = new btdefamomotionstate (originTransform, // start the transformation btTransform: getIdentity (); // center translation btRigidBody :: btRigidBodyConstructionInfo cubeInfo (0.8f, // quality motionState, // motion status cubeShape, // collision shape btVector3 (0, 0, 0); // local inertia cubeInfo. m_friction = 0.3; // friction cubeInfo. m_restitution = 0.1; // reverse force (recovery) m_cubeBody = new btRigidBody (cubeInfo); m_world-> addRigidBody (m_cubeBody); m_shapes.append (cubeShape); weight (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: debugShow (void) {btTransform worldTransform; m_cubeBody-> getMotionState ()-> getWorldTransform (worldTransform); btVector3 position = worldTransform * defaults (0, 0, 0); qDebug ("position is: (%. 2f, %. 2f, %. 2f) ", position. x (), position. y (), position. z ());}

Later, we may consider that in this model, there is only one force-applying object, that is, the "Earth". It applies gravity to the cube and allows the cube to freely fall into the body, the trajectory sampled during motion is expressed by worldTransform in motionState in the body. In combination with the rendering engine, our approach here is that the physical engine only modifies the modelMatrix of the object, and the model's own form controls the original position, the camera parameters control viewMatrix and projectionMatrix. Only in this way can everything seem clear.

There may be a tricky problem here. Sometimes you need to manually control modelMatrix to change the model transformation information. However, modelMatrix is controlled by the physical engine, therefore, it is difficult to handle the switchover of modelMatrix under manual control and the control of the physical engine. Next time I will see if I can clarify their previous relationships with the further study.

Finally, I successfully tested it on my Android tablet. It indicates that Bullet can be fully integrated with Qt for cross-platform development.

Related Article

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.