Open Dynamics Engine(ODE)物理引擎教程(3)–ODE仿真框架介绍与重力仿真

11 篇文章 97 订阅 ¥9.90 ¥99.00
本文是Open Dynamics Engine(ODE)物理引擎教程的第三部分,介绍了ODE仿真框架并展示了小球在重力作用下下落的场景。文章首先回顾了上一教程的项目配置,接着详细讲解了ODE环境的初始化、仿真循环、碰撞检测等核心部分,包括设置重力、创建球体、碰撞边界以及循环求解等。通过这个实例,读者可以理解ODE的运行机制,并为后续的关节仿真和更复杂的动力学仿真奠定基础。
摘要由CSDN通过智能技术生成

上一教程

Open Dynamics Engine(ODE)物理引擎教程(2)–项目环境配置与“Hello ODEWorld”
((在这一教程中详细讲解了在VS2017中的ODE项目配置过程,并通过简单的代码,运行仿真环境))

下一教程

Open Dynamics Engine(ODE)物理引擎教程(4)–ODE建立关节仿真
(这个教程主要在各种结构体之间建立约束形成链式运动链)

最终效果

一个小球在重力的效果下下落,在触碰到地面时会弹起,但是由于能量损耗,最终会停止下来。
在这里插入图片描述

引言

配置完环境后,既可以通过生成物体、关节,定义运动进行仿真了。但初次接触ODE,还需要了解它的运行框架才能灵活应有这个开源工具。本文中,通过建立一个简单的小球下落,既可以详细了解。要注意,这些实例展示整体架构内容,具体各部分函数我会另写教程,这样会更清晰,学习的时候相互照应不会凌乱,也不会上来就讲解很多函数让人摸不着头脑,在用中学才是王道。那我们开始吧!

项目代码

先贴上全部代码,有个大概印象。

完整代码

#include <stdlib.h>
#include <iostream>
#include <ode/ode.h>
#include <drawstuff/drawstuff.h>
#include "texturepath.h"

using namespace std;

/* ODE parameters.*/

#ifdef _MSC_VER
#pragma warning(disable:4244 4305)  // for VC++, no precision loss complaints
#endif

// select correct drawing functions
#ifdef dDOUBLE
#define dsDrawBox dsDrawBoxD
#define dsDrawSphere dsDrawSphereD
#define dsDrawCylinder dsDrawCylinderD
#define dsDrawCapsule dsDrawCapsuleD
#define dsDrawConvex dsDrawConvexD
#endif

// some constants
#define SIDE (0.05f)	// side length of a box
#define MASS (1.0)	// mass of a box

/* ODE */
// dynamics and collision objects
static dWorldID world;
static dBodyID ball;
static dJointID hinge;
static dSpaceID space, space_ball;
static dJointGroupID contactgroup;
static dGeomID ground, ballgeom;
static dGeomID world_mesh;

// state set by keyboard commands

// start simulation - set viewpoint

static float xyz[3] = { 0.0f,-1.5f,0.5f };
static float hpr[3] = { 90.0000f,-00.0000f,0.0000f };
static void start()
{
	dAllocateODEDataForThread(dAllocateMaskAll);

	dsSetViewpoint(xyz, hpr);
	printf(".................\n");
}


// called when a key pressed

static void command(int cmd)
{
	switch (cmd)
	{
	case 'r'|'R':
		dBodySetPosition(ball, 0.0, 0.0, 1);
		break;
	}	
}

void nearCallback(void *, dGeomID o1, dGeomID o2)
{
	int i, n;
	dBodyID b1 = dGeomGetBody(o1);
	dBodyID b2 = dGeomGetBody(o2);
	dsizeint o2d, groundd;
	o2d = (dsizeint)o2;
	groundd = (dsizeint)ground;

	if (b1 && b2 && dAreConnectedExcluding(b1, b2, dJointTypeContact)) return;
	const int N = 10;
	dContact contact[N];
	n = dCollide(o1, o2, N, &contact[0].geom, sizeof(dContact));
	if (n > 0)
	{
		for (i = 0; i < n; i++)
		{
			contact[i].surface.mode = dContactSlip1 | dContactSlip2 |dContactSoftERP | dContactSoftCFM | dContactApprox1;
			contact[i].surface.mu = 0.5;
			contact[i].surface.slip1 = 0.02;
			contact[i].surface.slip2 = 0.02;
			contact[i].surface.soft_erp = 0.1;
			contact[i].surface.soft_cfm = 0.01;
			dJointID c = dJointCreateContact(world, contactgroup, &contact[i]);
			dJointAttach(c, dGeomGetBody(contact[i].geom.g1), dGeomGetBody(contact[i].geom.g2));
		}
	}
}

// simulation loop

static void simLoop(int pause)
{
	double simstep = 0.001;

	if (!pause) {
		dSpaceCollide(space, 0, &nearCallback);
		dWorldStep(world, simstep);

		dsSetTexture(DS_WOOD);
		dJointGroupEmpty(contactgroup);
		
		dReal radius = dGeomSphereGetRadius(ballgeom);
		dsSetColor(1, 1, 1);
		dsDrawSphere(dGeomGetPosition(ballgeom), dGeomGetRotation(ballgeom), radius);
	}
}

void ODEMain(int argc, char **argv)
{
	// setup pointers to drawstuff callback functions
	dsFunctions fn;
	fn.version = DS_VERSION;
	fn.start = &start;
	fn.step = &simLoop;
	fn.command = &command;
	fn.stop = 0;
	fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;

	// create world
	dInitODE2(0);
	world = dWorldCreate();
	space = dHashSpaceCreate(0);
	contactgroup = dJointGroupCreate(0);
	dWorldSetGravity(world, 0, 0, -9.8);
	ground = dCreatePlane(space, 0, 0, 1, 0);

	dMass m;
	dMassSetParameters(&m, 4,
		0, 0, 0,
		1, 1, 1,
		0, 0, 0);

	dQuaternion q;
	dQFromAxisAndAngle(q, 0, 1, 0, 0);

	ball = dBodyCreate(world);
	dBodySetMass(ball, &m);
	dBodySetPosition(ball, 0.0, 0.0, 1);
	dBodySetQuaternion(ball, q);
	ballgeom = dCreateSphere(0, 0.1);
	dGeomSetBody(ballgeom, ball);
	dGeomSetOffsetPosition(ballgeom, 0, 0, 0);

	space_ball = dSimpleSpaceCreate(space);
	dSpaceSetCleanup(space_ball, 0);
	dSpaceAdd(space_ball, ballgeom);

	// run simulation
	dsSimulationLoop(argc, argv, 352, 288, &fn);

	dWorldDestroy(world);
	dCloseODE();
}



int  main(int argc, char **argv) {

	cout << "Hello World!" << endl;

	ODEMain(argc, argv);

	return 0;
}

程序分析

整个程序主要分为主函数入口main()、ODE初始化ODEMain()、ODE仿真循环simloop()、ODE碰撞检测nearCallback()四个部分。接下来深入剖析一下:

main()

cout << "Hello World!" << endl;

ODEMain(argc, argv);

ODEMain()函数需要传入系统参数,是因为其内部函数dsSimulationLoop(argc, argv, 352, 288, &fn);需要生成新的窗口。

ODEMain()

在这个函数中需要创建一个调用drawstuff的回调函数,其中包括start()初始化参数、simLoop()循环函数、command()键盘控制函数。
首先我们需要初始化ODE环境:

dInitODE2(0);

创建一个新世界,并设置重力:

world = dWorldCreate();
dWorldSetGravity(world, 0, 0, -9.8);

然后通过一系列函数创建球体、设定物理量以及初始化位置姿态等等。
检测碰撞中需要为球体创建碰撞边界:

ballgeom = dCreateSphere(0, 0.1);
dGeomSetBody(ballgeom, ball);
dGeomSetOffsetPosition(ballgeom, 0, 0, 0);

更具体的函数应用请参考ODE基础教程物体创建部分(求关注,更新中)。

之后通过函数调用循环部分:

dsSimulationLoop(argc, argv, 352, 288, &fn);

当仿真被停止时,会调用相关函数摧毁建立的世界关闭仿真环境:

dWorldDestroy(world);
dCloseODE();

simloop()

这是仿真一直循环进行的核心。设定每次循环的时间周期double simstep = 0.001;,当仿真没有暂停时if (!pause) {},进行一次物理求解:

dWorldStep(world, simstep);

这是一种标准调用,非常简便,更具体的函数应用请参考ODE基础教程动力学求解部分(求关注,更新中)。之后就是得到物体的位置和姿态进行画图显示的函数了:

		dsSetTexture(DS_WOOD);
		dJointGroupEmpty(contactgroup);
		
		dReal radius = dGeomSphereGetRadius(ballgeom);
		dsSetColor(1, 1, 1);
		dsDrawSphere(dGeomGetPosition(ballgeom), dGeomGetRotation(ballgeom), radius);

更具体的函数应用请参考ODE基础教程图形显示部分(求关注,更新中)。

nearCallback()

这个函数是被碰撞检测函数回调的,它会在两个接触物体间创建一些接触点n,这些点之间建立关节形成约束,即关节组contactgroup

	const int N = 10;
	dContact contact[N];
	n = dCollide(o1, o2, N, &contact[0].geom, sizeof(dContact));
	if (n > 0)
	{
		for (i = 0; i < n; i++)
		{
			contact[i].surface.mode = dContactSlip1 | dContactSlip2 |dContactSoftERP | dContactSoftCFM | dContactApprox1;
			contact[i].surface.mu = 0.5;
			contact[i].surface.slip1 = 0.02;
			contact[i].surface.slip2 = 0.02;
			contact[i].surface.soft_erp = 0.1;
			contact[i].surface.soft_cfm = 0.01;
			dJointID c = dJointCreateContact(world, contactgroup, &contact[i]);
			dJointAttach(c, dGeomGetBody(contact[i].geom.g1), dGeomGetBody(contact[i].geom.g2));
		}
	}

更具体的函数应用请参考ODE基础教程碰撞检测部分(求关注,更新中)。

本文到此结束,恭喜你已经入门了ODE。之后我们将进行更复杂物体的动力学仿真内容。

支线教程

Open Dynamics Engine(ODE)物理引擎教程(A)–c++调用python函数实现数据可视化

ODE基础教程持续更新中。。。

有什么问题会意见请留言关注我,会及时解答的。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陶技者

为技术开源努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值