📄 tutorial_39.htm
字号:
{
init(); <font color="#ffffaa">// 设置力为0</font>
solve(); <font color="#ffffaa"> // 应用力</font>
simulate(dt); <font color="#ffffaa">// 模拟</font>
}
};
</font></pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/tl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/tc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/tr.png" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_39_files/l.png"><img src="Tutorial_39_files/l.png"></td>
<td valign="top" width="100%">现在我们已经有了一个简单的物理模拟引擎了,它包含有物体和模拟两个类,下面我们基于它们创建三个具体的模拟对象:
<br> <br> <font color="#ffffaa">1. 具有恒定速度的物体</font><br> <font color="#ffffaa">2. 具有恒定加速度的物体</font><br> <font color="#ffffaa">3. 具有与距离成反比的力的物体</font><br> <br> <u>在程序中控制一个模拟对象:</u><br>
<br>
在我们写一个具体的模拟类之前,让我们看看如何在程序中模拟一个对象,在这个教程里,模拟引擎和操作模拟的程序在两个文件里,在程序中我们使用如下的函数,操作模拟:<br>
<font face="Tahoma,Verdana,sans-serif" size="-1"><br>
</font></td>
<td background="Tutorial_39_files/r.png"><img src="Tutorial_39_files/r.png"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/bl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/bc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/br.png" height="28" width="28"></td></tr></tbody></table><font color="#aaffaa" size="3"><pre>void Update (DWORD milliseconds) <font color="#ffffaa">// 执行模拟</font>
</pre></font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/tl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/tc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/tr.png" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_39_files/l.png"><img src="Tutorial_39_files/l.png"></td>
<td valign="top" width="100%">这个函数在每一帧的开始更新,参数为相隔的时间。</td>
<td background="Tutorial_39_files/r.png"><img src="Tutorial_39_files/r.png"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/bl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/bc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/br.png" height="28" width="28"></td></tr></tbody></table><font color="#aaffaa" size="3">
</font><pre><font color="#aaffaa" size="3">void Update (DWORD milliseconds)
{
...
...
...
float dt = milliseconds / 1000.0f; <font color="#ffffaa">// 转化为秒</font>
dt /= slowMotionRatio; <font color="#ffffaa">// 除以模拟系数</font>
timeElapsed += dt; <font color="#ffffaa">// 更新流失的时间</font>
...
</font></pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/tl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/tc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/tr.png" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_39_files/l.png"><img src="Tutorial_39_files/l.png"></td>
<td valign="top" width="100%">在下面的代码中,我们定义一个处理间隔,没隔这么长时间,让物理引擎模拟一次。</td>
<td background="Tutorial_39_files/r.png"><img src="Tutorial_39_files/r.png"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/bl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/bc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/br.png" height="28" width="28"></td></tr></tbody></table><font color="#aaffaa" size="3">
</font><pre><font color="#aaffaa" size="3"> ...
float maxPossible_dt = 0.1f; <font color="#ffffaa">// 设置模拟间隔</font>
int numOfIterations = (int)(dt / maxPossible_dt) + 1; <font color="#ffffaa">//计算在流失的时间里模拟的次数</font>
if (numOfIterations != 0)
dt = dt / numOfIterations;
for (int a = 0; a < numOfIterations; ++a) <font color="#ffffaa">// 模拟它们</font>
{
constantVelocity->operate(dt);
motionUnderGravitation->operate(dt);
massConnectedWithSpring->operate(dt);
}
}
</font></pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/tl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/tc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/tr.png" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_39_files/l.png"><img src="Tutorial_39_files/l.png"></td>
<td valign="top" width="100%">下面让我们来写着两个具体的模拟类: <br> <br>
1. 具有恒定速度的物体<br> <font color="#aaffaa">* class ConstantVelocity : public Simulation</font> --->
<font color="#ffffaa">模拟一个匀速运动的物体</font><font face="Tahoma,Verdana,sans-serif" size="-1"><br>
<br>
</font></td>
<td background="Tutorial_39_files/r.png"><img src="Tutorial_39_files/r.png"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/bl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/bc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/br.png" height="28" width="28"></td></tr></tbody></table><font color="#aaffaa" size="3">
<pre>class ConstantVelocity : public Simulation
{
public:
ConstantVelocity() : Simulation(1, 1.0f) <font color="#ffffaa"> </font>
{
masses[0]->pos = Vector3D(0.0f, 0.0f, 0.0f); <font color="#ffffaa">// 初始位置为0</font>
masses[0]->vel = Vector3D(1.0f, 0.0f, 0.0f); <font color="#ffffaa">// 向右运动</font>
}
};
</pre>
</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/tl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/tc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/tr.png" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_39_files/l.png"><img src="Tutorial_39_files/l.png"></td>
<td valign="top" width="100%">下面我们来创建一个具有恒定加速的物体:<font face="Tahoma,Verdana,sans-serif" size="-1"><br>
<br>
</font></td>
<td background="Tutorial_39_files/r.png"><img src="Tutorial_39_files/r.png"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/bl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/bc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/br.png" height="28" width="28"></td></tr></tbody></table>
<font color="#aaffaa" size="3">
</font><pre><font color="#aaffaa" size="3">class MotionUnderGravitation : public Simulation
{
Vector3D gravitation; <font color="#ffffaa">// 加速度</font>
MotionUnderGravitation(Vector3D gravitation) : Simulation(1, 1.0f) <font color="#ffffaa"> // 构造函数</font>
{
this->gravitation = gravitation; <font color="#ffffaa"> // 设置加速度</font>
masses[0]->pos = Vector3D(-10.0f, 0.0f, 0.0f); <font color="#ffffaa">// 设置位置为左边-10处</font>
masses[0]->vel = Vector3D(10.0f, 15.0f, 0.0f); <font color="#ffffaa">// 设置速度为右上</font>
}
...
</font></pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/tl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/tc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/tr.png" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_39_files/l.png"><img src="Tutorial_39_files/l.png"></td>
<td valign="top" width="100%">下面的函数设置施加给物体的力</td>
<td background="Tutorial_39_files/r.png"><img src="Tutorial_39_files/r.png"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/bl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/bc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/br.png" height="28" width="28"></td></tr></tbody></table>
<font color="#aaffaa" size="3">
<pre> virtual void solve() <font color="#ffffaa">// 设置当前的力</font>
{
for (int a = 0; a < numOfMasses; ++a)
masses[a]->applyForce(gravitation * masses[a]->m);
}
</pre>
</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/tl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/tc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/tr.png" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_39_files/l.png"><img src="Tutorial_39_files/l.png"></td>
<td valign="top" width="100%">下面的类创建一个受到与距离成正比的力的物体:</td>
<td background="Tutorial_39_files/r.png"><img src="Tutorial_39_files/r.png"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/bl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/bc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/br.png" height="28" width="28"></td></tr></tbody></table>
<font color="#aaffaa" size="3">
<pre>class MassConnectedWithSpring : public Simulation
{
public:
float springConstant; <font color="#ffffaa">// 弹性系数</font>
Vector3D connectionPos; <font color="#ffffaa">// 连接方向</font>
MassConnectedWithSpring(float springConstant) : Simulation(1, 1.0f) <font color="#ffffaa">// 构造函数</font>
{
this->springConstant = springConstant;
connectionPos = Vector3D(0.0f, -5.0f, 0.0f);
masses[0]->pos = connectionPos + Vector3D(10.0f, 0.0f, 0.0f);
masses[0]->vel = Vector3D(0.0f, 0.0f, 0.0f);
}
...
</pre>
</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/tl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/tc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/tr.png" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_39_files/l.png"><img src="Tutorial_39_files/l.png"></td>
<td valign="top" width="100%">下面的函数设置当前物体所受到的力:</td>
<td background="Tutorial_39_files/r.png"><img src="Tutorial_39_files/r.png"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/bl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/bc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/br.png" height="28" width="28"></td></tr></tbody></table>
<font color="#aaffaa" size="3">
<pre>virtual void solve() <font color="#ffffaa">// 设置当前的力</font>
{
for (int a = 0; a < numOfMasses; ++a)
{
Vector3D springVector = masses[a]->pos - connectionPos;
masses[a]->applyForce(-springVector * springConstant);
}
}
</pre>
</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td><img src="Tutorial_39_files/tl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/tc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/tr.png" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td background="Tutorial_39_files/l.png"><img src="Tutorial_39_files/l.png"></td>
<td valign="top" width="100%"><p>好了上面就是一个简单的物理模拟,希望你能喜欢:)</p>
<table border="1" width="100%">
<tbody><tr>
<td width="27%"><img src="Tutorial_39_files/logo%25203.jpg" align="middle" height="200" width="209"></td>
<td width="73%">版权与使用声明:<br>
我是个对学习和生活充满激情的普通男孩,在网络上我以DancingWind为昵称,我的联系方式是zhouwei02@mails.tsinghua.edu.cn,如果你有任何问题,都可以联系我。
<p>引子<br>
网络是一个共享的资源,但我在自己的学习生涯中浪费大量的时间去搜索可用的资料,在现实生活中花费了大量的金钱和时间在书店中寻找资料,于是我给自己起了
个昵称DancingWind,其意义是想风一样从各个知识的站点中吸取成长的养料。在飘荡了多年之后,我决定把自己收集的资料整理为一个统一的资源库。</p>
<p>版权声明<br>
所有DancingWind发表的内容,大多都来自共享的资源,所以我没有资格把它们据为己有,或声称自己为这些资源作出了一点贡献。故任何人都可以复
制,修改,重新发表,甚至以自己的名义发表,我都不会追究,但你在做以上事情的时候必须保证内容的完整性,给后来的人一个完整的教程。最后,任何人不能以
这些资料的任何部分,谋取任何形式的报酬。</p>
<p>发展计划<br>
在国外,很多资料都是很多人花费几年的时间慢慢积累起来的。如果任何人有兴趣与别人共享你的知识,我很欢迎你与我联系,但你必须同意我上面的声明。</p>
<p>感谢<br>
感谢我的母亲一直以来对我的支持和在生活上的照顾。<br>
感谢我深爱的女友田芹,一直以来默默的在精神上和生活中对我的支持,她甚至把买衣服的钱都用来给我买书了,她真的是我见过的最好的女孩,希望我能带给她幸福。</p>
<p>资源下载: <br>
文档 <a href="http://www.owlei.com/DancingWind/Res/mht/NeHe%20OpenGL%20Chinese%20Course%2039.mht">网页格式</a>
<a href="http://www.owlei.com/DancingWind/Res/pdf/OpenGL_Nehe_Course_Tutorial_39.pdf">PDF格式</a><br>
源码 <a href="http://www.owlei.com/DancingWind/Res/Src/39_Physics.rar">RAR格式</a></p></td>
</tr>
</tbody></table><table border="0" width="100%">
<tbody><tr><td align="left" width="50%"><b><font size="-1"> <a href="http://www.owlei.com/DancingWind/Course/Tutorial_38.htm"><
第38课 </a></font></b></td>
<td align="right" width="50%"><b><font size="-1"> <a href="http://www.owlei.com/DancingWind/Course/Tutorial_40.htm">第40课
></a></font></b></td>
</tr></tbody></table>
</td><td background="Tutorial_39_files/r.png"><img src="Tutorial_39_files/r.png"></td></tr>
</tbody></table><table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody><tr><td><img src="Tutorial_39_files/bl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_39_files/bc.png" height="28" width="100%"></td>
<td><img src="Tutorial_39_files/br.png" height="28" width="28"></td></tr>
</tbody></table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -