⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tutorial_40.htm

📁 如果你相信它就好好学学吧,同样这里也只是个入门
💻 HTM
📖 第 1 页 / 共 2 页
字号:
		float airFrictionConstant,				
		float groundRepulsionConstant,				
		float groundFrictionConstant,				
		float groundAbsorptionConstant,				
		float groundHeight						
		) : Simulation(numOfMasses, m)			
	{
		this->gravitation = gravitation;
		
		this->airFrictionConstant = airFrictionConstant;

		this->groundFrictionConstant = groundFrictionConstant;
		this->groundRepulsionConstant = groundRepulsionConstant;
		this->groundAbsorptionConstant = groundAbsorptionConstant;
		this->groundHeight = groundHeight;

		for (int a = 0; a &lt; numOfMasses; ++a)				<font color="#ffffaa">// 设置质点位置</font>
		{
			masses[a]-&gt;pos.x = a * springLength;			
			masses[a]-&gt;pos.y = 0;					
			masses[a]-&gt;pos.z = 0;					
		}

		springs = new Spring*[numOfMasses - 1];			
		
		for (a = 0; a &lt; numOfMasses - 1; ++a)				<font color="#ffffaa">//创建各个质点之间的模拟弹簧</font>
		{
			springs[a] = new Spring(masses[a], masses[a + 1], 
				springConstant, springLength, springFrictionConstant);
		}
	}
</pre>
</font> 
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
  <tr>
    <td><img src="Tutorial_40_files/tl.png" height="28" width="28"></td>
    <td width="100%"><img src="Tutorial_40_files/tc.png" height="28" width="100%"></td>
    <td><img src="Tutorial_40_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_40_files/l.png"><img src="Tutorial_40_files/l.png"></td>
      <td valign="top" width="100%">计算施加给各个质点的力</td>
    <td background="Tutorial_40_files/r.png"><img src="Tutorial_40_files/r.png"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
  <tr>
    <td><img src="Tutorial_40_files/bl.png" height="28" width="28"></td>
    <td width="100%"><img src="Tutorial_40_files/bc.png" height="28" width="100%"></td>
    <td><img src="Tutorial_40_files/br.png" height="28" width="28"></td></tr></tbody></table>
<font color="#aaffaa" size="3"> 
</font><pre><font color="#aaffaa" size="3">	void solve()								<font color="#ffffaa">// 计算施加给各个质点的力</font>
	{
		for (int a = 0; a &lt; numOfMasses - 1; ++a)			<font color="#ffffaa">// 弹簧施加给各个物体的力</font>
		{
			springs[a]-&gt;solve();					
		}

		for (a = 0; a &lt; numOfMasses; ++a)				<font color="#aaffaa" size="3"><font color="#ffffaa">// 计算各个物体受到的其它的力</font></font>
		{
			masses[a]-&gt;applyForce(gravitation * masses[a]-&gt;m);	<font color="#ffffaa">// 万有引力</font>
			<font color="#ffffaa">// 空气的摩擦力</font>
			masses[a]-&gt;applyForce(-masses[a]-&gt;vel * airFrictionConstant);

			if (masses[a]-&gt;pos.y &lt; groundHeight)			<font color="#ffffaa">// 计算地面对质点的作用</font>
			{
				Vector3D v;					

				v = masses[a]-&gt;vel;				<font color="#ffffaa">// </font><font color="#aaffaa" size="3"><font color="#ffffaa">返回速度</font></font>
				v.y = 0;					<font color="#ffffaa">// y方向的速度为0</font>

<font color="#ffffaa">		// 计算地面给质点的力</font>
				masses[a]-&gt;applyForce(-v * groundFrictionConstant);

				v = masses[a]-&gt;vel;				
				v.x = 0;					
				v.z = 0;					

				if (v.y &lt; 0)					<font color="#ffffaa">// 计算地面的缓冲力</font>

					masses[a]-&gt;applyForce(-v * groundAbsorptionConstant);
				
				<font color="#ffffaa">// 计算地面的反作用力</font>
				Vector3D force = Vector3D(0, groundRepulsionConstant, 0) * 
					(groundHeight - masses[a]-&gt;pos.y);

				masses[a]-&gt;applyForce(force);			<font color="#ffffaa">// 施加地面对质点的力</font>
			}
		}
	}
</font></pre>
 
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
  <tr>
    <td><img src="Tutorial_40_files/tl.png" height="28" width="28"></td>
    <td width="100%"><img src="Tutorial_40_files/tc.png" height="28" width="100%"></td>
    <td><img src="Tutorial_40_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_40_files/l.png"><img src="Tutorial_40_files/l.png"></td>
      <td valign="top" width="100%">下面的代码完成整个模拟过程</td>
    <td background="Tutorial_40_files/r.png"><img src="Tutorial_40_files/r.png"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
  <tr>
    <td><img src="Tutorial_40_files/bl.png" height="28" width="28"></td>
    <td width="100%"><img src="Tutorial_40_files/bc.png" height="28" width="100%"></td>
    <td><img src="Tutorial_40_files/br.png" height="28" width="28"></td></tr></tbody></table>
<font color="#aaffaa" size="3"> 
<pre>	void simulate(float dt)						<font color="#ffffaa">// 模拟一次</font>
	{
		Simulation::simulate(dt);					<font color="#ffffaa">// 调用基类的模拟函数</font>

		ropeConnectionPos += ropeConnectionVel * dt;			<font color="#ffffaa">// 计算绳子的连接点</font>

		if (ropeConnectionPos.y &lt; groundHeight)				
		{
			ropeConnectionPos.y = groundHeight;
			ropeConnectionVel.y = 0;
		}

		masses[0]-&gt;pos = ropeConnectionPos;				<font color="#ffffaa">// 更新绳子的连接点和速度</font>
		masses[0]-&gt;vel = ropeConnectionVel;				
	}
</pre>
</font> <font color="#aaffaa" size="3">
<pre>	void setRopeConnectionVel(Vector3D ropeConnectionVel)			
	{
		this-&gt;ropeConnectionVel = ropeConnectionVel;
	}
</pre>
</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
  <tr>
    <td><img src="Tutorial_40_files/tl.png" height="28" width="28"></td>
    <td width="100%"><img src="Tutorial_40_files/tc.png" height="28" width="100%"></td>
    <td><img src="Tutorial_40_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_40_files/l.png"><img src="Tutorial_40_files/l.png"></td>
      <td valign="top" width="100%">有了上面的类,我们可以很方便的模拟绳子,代码如下:</td>
    <td background="Tutorial_40_files/r.png"><img src="Tutorial_40_files/r.png"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
  <tr>
    <td><img src="Tutorial_40_files/bl.png" height="28" width="28"></td>
    <td width="100%"><img src="Tutorial_40_files/bc.png" height="28" width="100%"></td>
    <td><img src="Tutorial_40_files/br.png" height="28" width="28"></td></tr></tbody></table>
<font color="#aaffaa" size="3"> 
<pre>RopeSimulation* ropeSimulation =
	new RopeSimulation(
		80,								<font color="#ffffaa">// 80 质点</font>
		0.05f,								<font color="#ffffaa">// 每个质点50g</font>
		10000.0f,								<font color="#ffffaa">// 弹性系数</font>
		0.05f,								<font color="#ffffaa">// 质点之间的距离</font>
		0.2f,								<font color="#ffffaa">// 弹簧的内摩擦力</font>
		Vector3D(0, -9.81f, 0),						<font color="#ffffaa">// 万有引力</font>
		0.02f,								<font color="#ffffaa">// 空气摩擦力</font>
		100.0f,								<font color="#ffffaa">// 地面反作用系数</font>
		0.2f,								<font color="#ffffaa">// 地面摩擦系数</font>
		2.0f,								<font color="#ffffaa">// 地面缓冲系数</font>
		-1.5f);								<font color="#ffffaa">// 地面高度</font>
</pre>
</font> 
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
  <tr>
    <td><img src="Tutorial_40_files/tl.png" height="28" width="28"></td>
    <td width="100%"><img src="Tutorial_40_files/tc.png" height="28" width="100%"></td>
    <td><img src="Tutorial_40_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_40_files/l.png"><img src="Tutorial_40_files/l.png"></td>
      <td valign="top" width="100%">下面的代码在程序中执行绳子的模拟</td>
    <td background="Tutorial_40_files/r.png"><img src="Tutorial_40_files/r.png"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
  <tr>
    <td><img src="Tutorial_40_files/bl.png" height="28" width="28"></td>
    <td width="100%"><img src="Tutorial_40_files/bc.png" height="28" width="100%"></td>
    <td><img src="Tutorial_40_files/br.png" height="28" width="28"></td></tr></tbody></table><font color="#aaffaa" size="3">
<pre>float dt = milliseconds / 1000.0f;						<font color="#ffffaa">// 经过的秒数</font>

float maxPossible_dt = 0.002f;						<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 &lt; numOfIterations; ++a)					<font color="#ffffaa">// 执行模拟</font>
	ropeSimulation-&gt;operate(dt);
</pre>
</font>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
  <tr>
    <td><img src="Tutorial_40_files/tl.png" height="28" width="28"></td>
    <td width="100%"><img src="Tutorial_40_files/tc.png" height="28" width="100%"></td>
    <td><img src="Tutorial_40_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_40_files/l.png"><img src="Tutorial_40_files/l.png"></td>
      <td valign="top" width="100%"><p> </p>
        <p>我相信这一个教会了你很多,从最开始的模型的建立,到完成最后的代码。有了这个基础,相信你会创造出很多更有意思的代码!</p>
<table border="1" width="100%">
  <tbody><tr>
    <td width="27%"><img src="Tutorial_40_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%2040.mht">网页格式</a> 
                <a href="http://www.owlei.com/DancingWind/Res/pdf/OpenGL_Nehe_Course_Tutorial_40.pdf">PDF格式</a><br>
                源码 <a href="http://www.owlei.com/DancingWind/Res/Src/40_Robe.rar">RAR格式</a></p></td>
  </tr>
</tbody></table>
        <font face="Tahoma,Verdana,sans-serif" size="-1"><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_39.htm">&lt; 
              第39课 </a></font></b></td>
            <td align="right" width="50%"><b><font size="-1"> <a href="http://www.owlei.com/DancingWind/Course/Tutorial_41.htm">第41课 
              &gt;</a></font></b></td>
</tr></tbody></table>
</font></td><td background="Tutorial_40_files/r.png"><img src="Tutorial_40_files/r.png"></td></tr>
</tbody></table><table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody><tr><td><img src="Tutorial_40_files/bl.png" height="28" width="28"></td>
<td width="100%"><img src="Tutorial_40_files/bc.png" height="28" width="100%"></td>
<td><img src="Tutorial_40_files/br.png" height="28" width="28"></td></tr>
</tbody></table>

</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -