ODE仿真引擎使用(十)

  这个例程简要介绍速度,角速度,加速度和角加速度。
1.Velocity and angular velocity
   dBodySetLinearVel()设定物体的线速度,dBodySetAngularVel()设定物体的角速度。速度是重心的速度,角速度是围绕重心的。可以在初始状态下设置速度和角速度。不建议在每个模拟步骤中设置这些值,它滥用了你的物理模型。
If you want to control the speed of an object. Please use a joint motor.
void dBodySetLinearVel (dBodyID body, dReal x, dReal y, dReal z);
Set a linear velocity (x, y, z) [m/s] of a body.
void dBodySetAngularVel (dBodyID body, dReal x, dReal y, dReal z);
Set an angular velocity (x, y, z) [rad/s].
const dReal * dBodyGetLinearVel (dBodyID body);
Get a linear velocity (x, y, z) [m/s] of a body. The return value is a pointer to an array.
const dReal * dBodyGetAngularVel (dBodyID body);
Get an angular velocity (x, y, z) [rad/s] of a body. The return value is a pointer to an array.
2. Acceleration and angular acceleration
  不幸的,没有APIs去获得加速度和角加速度,如果你想获得它们,将速度的变化除以步长,时间步长是dWorldStep(dWorldID,dReal step size)的第二个参数。步长是用于数值积分的时间步长。值得注意的是,dBodyGetLinearVel()dBodyGetAngularVel()返回一个指针,由三个元素(x,y,z)组成的数组。
3. 程序
  在中程序的基础上,加入展示物体的速度和加速度。

 1 static void simLoop (int pause)
 2 {
 3   static long steps = 0;
 4   const dReal stepsize = 0.01;  
 5   const dReal *linear_vel, *angular_vel;
 6   static dReal linear_vel_old[3], angular_vel_old[3];
 7   dReal linear_accel[3], angular_accel[3];
 8 
 9   if (!pause)     {
10     dSpaceCollide(space,0,&nearCallback);
11     dWorldStep(world,stepsize);
12     dJointGroupEmpty(contactgroup);
13   }      
14 
15   // Linear velocity, Angular Velocity
16   linear_vel  = dBodyGetLinearVel(ball.body);
17   angular_vel = dBodyGetAngularVel(pillar.body);
18   printf("\n%d steps \n", steps++);
19   printf("Linear Velocity: x=%.3f y=%.3f z=%.3f \n",
20   linear_vel[0],linear_vel[1],linear_vel[2]);
21   printf("Angular Velocity: x=%.3f y=%.3f z=%.3f \n",       angular_vel[0],angular_vel[1],angular_vel[2]);
22 
23   // Linear acceleration, Angular acceleration
24   for (int i=0; i < 3; i++) {
25     linear_accel[i] = (linear_vel[i]-linear_vel_old[i])  /stepsize;
26     angular_accel[i] = (angular_vel[i]-angular_vel_old[i])/stepsize;
27   }
28 
29   printf("Linear Acceleration: x=%.3f y=%.3f z=%.3f \n",       linear_accel[0],linear_accel[1],linear_accel[2]);
30   printf("Angular Acceleration: x=%.3f y=%.3f z=%.3f \n",       angular_accel[0],angular_accel[1],angular_accel[2]);
31 
32   for (int j=0; j < 3; j++) {
33     linear_vel_old[j]  = linear_vel[j];
34     angular_vel_old[j] = angular_vel[j];
35   }
36   drawObject(ball.geom,1.3,0,0);
37   drawObject(pillar.geom,0,0,1.3);
38 }

猜你喜欢

转载自www.cnblogs.com/zhaochenliang/p/12287361.html