templeos-info/public/Wb/Demo/Games/Collision.HC.HTML

186 lines
13 KiB
Plaintext
Raw Normal View History

2024-03-24 21:24:44 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF2>/*Uses </span><a href="/Wb/Demo/Lectures/FixedPoint.HC.HTML#l1"><span class=cF4>fixed-point</span></a><span class=cF2> arithmetic</span><span class=cF0>
<a name="l2"></a></span><span class=cF2>because it used to be faster than floating</span><span class=cF0>
<a name="l3"></a></span><span class=cF2>point.</span><span class=cF0> </span><span class=cF2>See </span><a href="/Wb/Demo/Lectures/FixedPoint.HC.HTML#l1"><span class=cF4>::/Demo/Lectures/FixedPoint.HC</span></a><span class=cF2>.</span><span class=cF0>
<a name="l4"></a>
<a name="l5"></a></span><span class=cF2>The decimal place is between</span><span class=cF0>
<a name="l6"></a></span><span class=cF2>bits 31 and 32.</span><span class=cF0>
<a name="l7"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l8"></a>
<a name="l9"></a>#</span><span class=cF1>define</span><span class=cF0> BALLS_NUM 64
<a name="l10"></a>#</span><span class=cF1>define</span><span class=cF0> BALL_RADIUS 5
<a name="l11"></a>
<a name="l12"></a></span><span class=cF9>I64</span><span class=cF0> ball_x[BALLS_NUM],ball_y[BALLS_NUM],
<a name="l13"></a> ball_velocity_x[BALLS_NUM],ball_velocity_y[BALLS_NUM];
<a name="l14"></a>
<a name="l15"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DrawIt</span><span class=cF0>(</span><span class=cF9>CTask</span><span class=cF0> *,</span><span class=cF9>CDC</span><span class=cF0> *dc)
<a name="l16"></a>{
<a name="l17"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l18"></a> dc-&gt;color=</span><span class=cF3>RED</span><span class=cF0>;
<a name="l19"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;BALLS_NUM;i++)
<a name="l20"></a> </span><span class=cF5>GrCircle</span><span class=cF0>(dc,ball_x[i].i32[1],ball_y[i].i32[1],BALL_RADIUS);
<a name="l21"></a>}
<a name="l22"></a>
<a name="l23"></a></span><span class=cF2>/****</span><span class=cF0>
<a name="l24"></a>
<a name="l25"></a></span><span class=cFA>
<a name="l26"></a>/* Graphics Not Rendered in HTML */</span><span class=cF0>
<a name="l27"></a>
<a name="l28"></a>
<a name="l29"></a>
<a name="l30"></a>
<a name="l31"></a></span><span class=cF2>Initial and final velocity vects</span><span class=cF0>
<a name="l32"></a></span><span class=cF2>with normal and tangential components.</span><span class=cF0>
<a name="l33"></a></span><span class=cF2>All masses are ident, so they</span><span class=cF0>
<a name="l34"></a></span><span class=cF2>have been dropped from the equations.</span><span class=cF0>
<a name="l35"></a>
<a name="l36"></a></span><span class=cF2>Conservation of Momentum:</span><span class=cF0>
<a name="l37"></a>
<a name="l38"></a></span><span class=cF2>V1it+V2it=V1ft+V2ft</span><span class=cF0>
<a name="l39"></a>
<a name="l40"></a></span><span class=cF2>V1in+V2in=V1fn+V2fn</span><span class=cF0>
<a name="l41"></a>
<a name="l42"></a></span><span class=cF2>Conservation of Energy:</span><span class=cF0>
<a name="l43"></a>
<a name="l44"></a></span><span class=cF2>|V1i|2+|V2i|2=|V1f|2+|V2f|2</span><span class=cF0>
<a name="l45"></a></span><span class=cF2>****/</span><span class=cF0>
<a name="l46"></a>
<a name="l47"></a></span><span class=cF1>U0</span><span class=cF0> AnimateTask(</span><span class=cF9>I64</span><span class=cF0>)
<a name="l48"></a>{
<a name="l49"></a> </span><span class=cF9>CTask</span><span class=cF0> *task=</span><span class=cF5>Fs</span><span class=cF0>-&gt;parent_task;
<a name="l50"></a> </span><span class=cF9>I64</span><span class=cF0> i,j,h,v,distdist,
<a name="l51"></a> dia=(2*BALL_RADIUS)&lt;&lt;32,
<a name="l52"></a> diadia=</span><span class=cF5>SqrI64</span><span class=cF0>(2*BALL_RADIUS)&lt;&lt;32,
<a name="l53"></a> delta_x,delta_y,v_t1,v_n1,v_t2,v_n2;
<a name="l54"></a> </span><span class=cF1>F64</span><span class=cF0> dist;
<a name="l55"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l56"></a> h=task-&gt;pix_width;
<a name="l57"></a> v=task-&gt;pix_height;
<a name="l58"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;BALLS_NUM;i++) {
<a name="l59"></a> ball_x[i]+=ball_velocity_x[i];
<a name="l60"></a> ball_y[i]+=ball_velocity_y[i];
<a name="l61"></a> </span><span class=cF1>if</span><span class=cF0> (ball_x[i]&lt;BALL_RADIUS&lt;&lt;32) </span><span class=cF7>{</span><span class=cF0>
<a name="l62"></a> ball_velocity_x[i]*=-1;
<a name="l63"></a> ball_x[i]=BALL_RADIUS&lt;&lt;32;
<a name="l64"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l65"></a> </span><span class=cF1>if</span><span class=cF0> (ball_x[i]&gt;=</span><span class=cF7>(</span><span class=cF0>h-BALL_RADIUS</span><span class=cF7>)</span><span class=cF0>&lt;&lt;32) </span><span class=cF7>{</span><span class=cF0>
<a name="l66"></a> ball_velocity_x[i]*=-1;
<a name="l67"></a> ball_x[i]=(h-BALL_RADIUS)&lt;&lt;32;
<a name="l68"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l69"></a> </span><span class=cF1>if</span><span class=cF0> (ball_y[i]&lt;BALL_RADIUS&lt;&lt;32) </span><span class=cF7>{</span><span class=cF0>
<a name="l70"></a> ball_velocity_y[i]*=-1;
<a name="l71"></a> ball_y[i]=BALL_RADIUS&lt;&lt;32;
<a name="l72"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l73"></a> </span><span class=cF1>if</span><span class=cF0> (ball_y[i]&gt;=</span><span class=cF7>(</span><span class=cF0>v-BALL_RADIUS</span><span class=cF7>)</span><span class=cF0>&lt;&lt;32) </span><span class=cF7>{</span><span class=cF0>
<a name="l74"></a> ball_velocity_y[i]*=-1;
<a name="l75"></a> ball_y[i]=(v-BALL_RADIUS)&lt;&lt;32;
<a name="l76"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l77"></a> }
<a name="l78"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;BALLS_NUM;i++) {
<a name="l79"></a> </span><span class=cF1>for</span><span class=cF0> (j=i+1;j&lt;BALLS_NUM;j++) </span><span class=cF7>{</span><span class=cF0>
<a name="l80"></a> delta_x=ball_x[i]-ball_x[j];
<a name="l81"></a> delta_y=ball_y[i]-ball_y[j];
<a name="l82"></a>
<a name="l83"></a> </span><span class=cF2>//We shift 16 because multiplying</span><span class=cF0>
<a name="l84"></a> </span><span class=cF2>//two 32 shifted would yield 64 shifted</span><span class=cF0>
<a name="l85"></a> </span><span class=cF2>//and we want a 32 shifted res.</span><span class=cF0>
<a name="l86"></a> distdist=</span><span class=cF5>SqrI64</span><span class=cF0>(delta_x&gt;&gt;16)+</span><span class=cF5>SqrI64</span><span class=cF0>(delta_y&gt;&gt;16);
<a name="l87"></a>
<a name="l88"></a> </span><span class=cF2>//We work with square instead of sqrt</span><span class=cF0>
<a name="l89"></a> </span><span class=cF2>//to avoid unnecessarily calculating</span><span class=cF0>
<a name="l90"></a> </span><span class=cF2>//square heads (They are slow.)</span><span class=cF0>
<a name="l91"></a> </span><span class=cF1>if</span><span class=cF0> (distdist &amp;&amp; distdist&lt;=diadia) {
<a name="l92"></a> dist=</span><span class=cF5>Sqrt</span><span class=cF0>(distdist); </span><span class=cF2>//shifted 16 bits</span><span class=cF0>
<a name="l93"></a> delta_x/=dist; </span><span class=cF2>//shifted 16</span><span class=cF0>
<a name="l94"></a> delta_y/=dist;
<a name="l95"></a>
<a name="l96"></a> v_t1=(ball_velocity_x[i]&gt;&gt;16*delta_y-
<a name="l97"></a> ball_velocity_y[i]&gt;&gt;16*delta_x)&gt;&gt;16;
<a name="l98"></a> v_n1=(ball_velocity_x[i]&gt;&gt;16*delta_x+
<a name="l99"></a> ball_velocity_y[i]&gt;&gt;16*delta_y)&gt;&gt;16;
<a name="l100"></a> v_t2=(ball_velocity_x[j]&gt;&gt;16*delta_y-
<a name="l101"></a> ball_velocity_y[j]&gt;&gt;16*delta_x)&gt;&gt;16;
<a name="l102"></a> v_n2=(ball_velocity_x[j]&gt;&gt;16*delta_x+
<a name="l103"></a> ball_velocity_y[j]&gt;&gt;16*delta_y)&gt;&gt;16;
<a name="l104"></a>
<a name="l105"></a> </span><span class=cF1>if</span><span class=cF0> (ball_velocity_x[i]&gt;&gt;16*ball_velocity_x[j]&gt;&gt;16+
<a name="l106"></a> ball_velocity_y[i]&gt;&gt;16*ball_velocity_y[j]&gt;&gt;16&lt;=0) </span><span class=cF7>{</span><span class=cF0>
<a name="l107"></a> ball_velocity_x[i]= v_t1*delta_y-v_n1*delta_x;
<a name="l108"></a> ball_velocity_y[i]=-v_t1*delta_x-v_n1*delta_y;
<a name="l109"></a> ball_velocity_x[j]= v_t2*delta_y-v_n2*delta_x;
<a name="l110"></a> ball_velocity_y[j]=-v_t2*delta_x-v_n2*delta_y;
<a name="l111"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l112"></a> ball_velocity_x[i]= v_t1*delta_y+v_n2*delta_x;
<a name="l113"></a> ball_velocity_y[i]=-v_t1*delta_x+v_n2*delta_y;
<a name="l114"></a> ball_velocity_x[j]= v_t2*delta_y+v_n1*delta_x;
<a name="l115"></a> ball_velocity_y[j]=-v_t2*delta_x+v_n1*delta_y;
<a name="l116"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l117"></a>
<a name="l118"></a> </span><span class=cF2>//Correct for overlap</span><span class=cF0>
<a name="l119"></a> dist=0x10000+(dia/0x10000-dist)/2;
<a name="l120"></a> ball_x[i]+=dist*delta_x;
<a name="l121"></a> ball_y[i]+=dist*delta_y;
<a name="l122"></a> ball_x[j]-=dist*delta_x;
<a name="l123"></a> ball_y[j]-=dist*delta_y;
<a name="l124"></a> }
<a name="l125"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l126"></a> }
<a name="l127"></a> </span><span class=cF5>Sleep</span><span class=cF0>(1);
<a name="l128"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l129"></a>}
<a name="l130"></a>
<a name="l131"></a></span><span class=cF1>U0</span><span class=cF0> Init()
<a name="l132"></a>{
<a name="l133"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l134"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;BALLS_NUM;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l135"></a> ball_x[i]=(</span><span class=cF5>RandU16</span><span class=cF0>%</span><span class=cF7>(</span><span class=cF5>Fs</span><span class=cF0>-&gt;pix_width-BALL_RADIUS*2</span><span class=cF7>)</span><span class=cF0>+BALL_RADIUS)&lt;&lt;32;
<a name="l136"></a> ball_y[i]=(</span><span class=cF5>RandU16</span><span class=cF0>%</span><span class=cF7>(</span><span class=cF5>Fs</span><span class=cF0>-&gt;pix_height-BALL_RADIUS*2</span><span class=cF7>)</span><span class=cF0>+BALL_RADIUS)&lt;&lt;32;
<a name="l137"></a> ball_velocity_x[i]=</span><span class=cF5>RandI32</span><span class=cF0>/4;
<a name="l138"></a> ball_velocity_y[i]=</span><span class=cF5>RandI32</span><span class=cF0>/4;
<a name="l139"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l140"></a>}
<a name="l141"></a>
<a name="l142"></a></span><span class=cF1>U0</span><span class=cF0> Collision()
<a name="l143"></a>{
<a name="l144"></a> </span><span class=cF5>SettingsPush</span><span class=cF0>; </span><span class=cF2>//See </span><a href="/Wb/Adam/TaskSettings.HC.HTML#l3"><span class=cF4>SettingsPush</span></a><span class=cF0>
<a name="l145"></a> Init;
<a name="l146"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;animate_task=</span><span class=cF5>Spawn</span><span class=cF0>(&amp;AnimateTask,</span><span class=cF3>NULL</span><span class=cF0>,</span><span class=cF6>&quot;Animate&quot;</span><span class=cF0>,,</span><span class=cF5>Fs</span><span class=cF0>);
<a name="l147"></a> </span><span class=cF5>DocCursor</span><span class=cF0>;
<a name="l148"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l149"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;draw_it=&amp;</span><span class=cF5>DrawIt</span><span class=cF0>;
<a name="l150"></a> </span><span class=cF5>GetChar</span><span class=cF0>;
<a name="l151"></a> </span><span class=cF5>SettingsPop</span><span class=cF0>;
<a name="l152"></a>}
<a name="l153"></a>
<a name="l154"></a>Collision;
</span></pre></body>
</html>