當前位置:
首頁 >
如何使用vs来运行box2d中Testbed的案例
發布時間:2023/11/28
30
豆豆
生活随笔
收集整理的這篇文章主要介紹了
如何使用vs来运行box2d中Testbed的案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
0 環境
vs: vs2012
box2d: Box2D_v2.1.2
1 得到box2d編譯版本
可以參考:
https://blog.csdn.net/sty945/article/details/83823976
現在我們得到一個可以用vs2012打開的box2d版本,用vs2012打開如下圖所示:
2 運行Testbed的項目
將testbed項目設置成啟動項目,然后F7編譯, Ctrl+F5運行就可以得到如下界面:
我們可以做一些演示練習
3 基于Testbed項目編寫程序
推薦一個box2d教程:
http://ohcoder.com/blog/categories/box2d-tutorials/
我們如何編寫自己的程序呢?
在vs2012中直接添加文件是不對的,添加文件只會添加到build目錄下,必須要和原始文件在同一目錄下才可以,以下是我采用的方法:
3.1 采用vscode打開源代碼
路徑是…\Box2D_v2.1.2\Box2D文件夾,會得到如下的界面:
3.2 打開testbed文件夾
3.3 創建測試
Testbed/Tests文件夾下面,添加自己創建的.h頭文件,如創建一個FooTest.h,寫入如下內容:
#ifndef FOO_TEST_H
#define FOO_TEST_H#define DEGTORAD 0.0174532925199432957f
#define RADTODEG 57.295779513082320876fclass FooTest: public Test
{public:b2Body* dynamicBody;FooTest(){b2BodyDef myBodyDef;myBodyDef.type = b2_dynamicBody;//start 10 units further to the right, 20 units highermyBodyDef.position.Set(0, 20);myBodyDef.angle = 0;// b2Body* dynamicBody = m_world->CreateBody(&myBodyDef);dynamicBody = m_world->CreateBody(&myBodyDef);b2PolygonShape boxShape;boxShape.SetAsBox(2, 1);b2FixtureDef boxFixtureDef;boxFixtureDef.shape = &boxShape;boxFixtureDef.density = 1;dynamicBody->CreateFixture(&boxFixtureDef);//change the starting position and angledynamicBody->SetTransform(b2Vec2(10, 20), 1);// set the linear velocity and angular velocity of the bodydynamicBody->SetLinearVelocity(b2Vec2(-5, 5));dynamicBody->SetAngularVelocity(-90 * DEGTORAD);//static BodiesmyBodyDef.type = b2_staticBody;myBodyDef.position.Set(0, 10);b2Body* staticBody = m_world->CreateBody(&myBodyDef);staticBody->CreateFixture(&boxFixtureDef);//kinematic bodymyBodyDef.type = b2_kinematicBody;myBodyDef.position.Set(-18, 11);b2Body* kinematicBody = m_world->CreateBody(&myBodyDef);kinematicBody->CreateFixture(&boxFixtureDef);kinematicBody->SetLinearVelocity(b2Vec2(1, 0));kinematicBody->SetAngularVelocity(360 * DEGTORAD);};void Step(Settings* setttings){Test::Step(setttings);m_debugDraw.DrawString(5, m_textLine, "now we have a foo test");m_textLine += 15;b2Vec2 pos = dynamicBody->GetPosition();float angle = dynamicBody->GetAngle();b2Vec2 vel = dynamicBody->GetLinearVelocity();float angularvel = dynamicBody->GetAngularVelocity();m_debugDraw.DrawString(5, m_textLine, "Position:%.3f,%.3f Angle:%.3f", pos.x, pos.y, angle*RADTODEG);m_textLine += 15;m_debugDraw.DrawString(5, m_textLine, "Veloctiy:%.3f, %.3f Angular velocity:%.3f", vel.x, vel.y, angularvel * RADTODEG);m_textLine += 15;for (b2Body* b = m_world->GetBodyList(); b; b = b->GetNext()){b2Vec2 pos1 = b->GetPosition();m_debugDraw.DrawString(5, m_textLine, "Position:%.3f,%.3f", pos1.x, pos1.y);m_textLine += 15;}}static Test* Create(){return new FooTest;}
};#endif
然后在同一目錄下的TestEntries.cpp文件添加如下兩行代碼:
#include "FooTest.h"{"Foo test", FooTest::Create},
3.4 運行測試
在vs2012中編譯運行,會出現如下界面:
第一個演示的就是我們剛才編寫的程序。
基于此就可以根據教程:
http://ohcoder.com/blog/categories/box2d-tutorials/
基于學習Box2D了
總結
以上是生活随笔為你收集整理的如何使用vs来运行box2d中Testbed的案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: grep 的特殊使用
- 下一篇: awk 入门教程