mirror of
https://github.com/Zeal-Operating-System/ZealOS.git
synced 2024-12-28 08:16:31 +00:00
194 lines
17 KiB
HTML
Executable file
194 lines
17 KiB
HTML
Executable file
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
|
|
<meta name="generator" content="ZealOS V1.01">
|
|
<style type="text/css">
|
|
body {background-color:#fef1f0;}
|
|
.cF0{color:#000000;background-color:#fef1f0;}
|
|
.cF1{color:#0148a4;background-color:#fef1f0;}
|
|
.cF2{color:#3b7901;background-color:#fef1f0;}
|
|
.cF3{color:#057c7e;background-color:#fef1f0;}
|
|
.cF4{color:#bb2020;background-color:#fef1f0;}
|
|
.cF5{color:#9e42ae;background-color:#fef1f0;}
|
|
.cF6{color:#b57901;background-color:#fef1f0;}
|
|
.cF7{color:#b2b6af;background-color:#fef1f0;}
|
|
.cF8{color:#555753;background-color:#fef1f0;}
|
|
.cF9{color:#678fbb;background-color:#fef1f0;}
|
|
.cFA{color:#82bc49;background-color:#fef1f0;}
|
|
.cFB{color:#0097a2;background-color:#fef1f0;}
|
|
.cFC{color:#e26a6a;background-color:#fef1f0;}
|
|
.cFD{color:#c671bc;background-color:#fef1f0;}
|
|
.cFE{color:#c7ab00;background-color:#fef1f0;}
|
|
.cFF{color:#fef1f0;background-color:#fef1f0;}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<pre style="font-family:monospace;font-size:12pt">
|
|
<a name="l1"></a><span class=cF2>/*Uses </span><a href="https://zeal-operating-system.github.io/ZealOS/Demo/Lectures/FixedPoint.CC.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="https://zeal-operating-system.github.io/ZealOS/Demo/Lectures/FixedPoint.CC.html#l1"><span class=cF4>::/Demo/Lectures/FixedPoint.CC</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 </span><span class=cFE>64</span><span class=cF0>
|
|
<a name="l10"></a>#</span><span class=cF1>define</span><span class=cF0> BALL_RADIUS </span><span class=cFE>5</span><span class=cF0>
|
|
<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], ball_velocity_x[BALLS_NUM], ball_velocity_y[BALLS_NUM];
|
|
<a name="l13"></a>
|
|
<a name="l14"></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="l15"></a>{
|
|
<a name="l16"></a> </span><span class=cF9>I64</span><span class=cF0> i;
|
|
<a name="l17"></a>
|
|
<a name="l18"></a> dc->color = </span><span class=cF3>RED</span><span class=cF0>;
|
|
<a name="l19"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i < BALLS_NUM; i++)
|
|
<a name="l20"></a> </span><span class=cF5>GrCircle</span><span class=cF0>(dc, ball_x[i].i32[</span><span class=cFE>1</span><span class=cF0>], ball_y[i].i32[</span><span class=cFE>1</span><span class=cF0>], 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>->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 = (</span><span class=cFE>2</span><span class=cF0> * BALL_RADIUS) << </span><span class=cFE>32</span><span class=cF0>,
|
|
<a name="l52"></a> diadia = </span><span class=cF5>SqrI64</span><span class=cF0>(</span><span class=cFE>2</span><span class=cF0> *BALL_RADIUS) << </span><span class=cFE>32</span><span class=cF0>,
|
|
<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>
|
|
<a name="l56"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>)
|
|
<a name="l57"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l58"></a> h = task->pix_width;
|
|
<a name="l59"></a> v = task->pix_height;
|
|
<a name="l60"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i < BALLS_NUM; i++)
|
|
<a name="l61"></a> {
|
|
<a name="l62"></a> ball_x[i] += ball_velocity_x[i];
|
|
<a name="l63"></a> ball_y[i] += ball_velocity_y[i];
|
|
<a name="l64"></a> </span><span class=cF1>if</span><span class=cF0> (ball_x[i] < BALL_RADIUS << </span><span class=cFE>32</span><span class=cF0>)
|
|
<a name="l65"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l66"></a> ball_velocity_x[i] *= -</span><span class=cFE>1</span><span class=cF0>;
|
|
<a name="l67"></a> ball_x[i] = BALL_RADIUS << </span><span class=cFE>32</span><span class=cF0>;
|
|
<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_x[i] >= </span><span class=cF7>(</span><span class=cF0>h - BALL_RADIUS</span><span class=cF7>)</span><span class=cF0> << </span><span class=cFE>32</span><span class=cF0>)
|
|
<a name="l70"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l71"></a> ball_velocity_x[i] *= -</span><span class=cFE>1</span><span class=cF0>;
|
|
<a name="l72"></a> ball_x[i] = (h - BALL_RADIUS) << </span><span class=cFE>32</span><span class=cF0>;
|
|
<a name="l73"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l74"></a> </span><span class=cF1>if</span><span class=cF0> (ball_y[i] < BALL_RADIUS << </span><span class=cFE>32</span><span class=cF0>)
|
|
<a name="l75"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l76"></a> ball_velocity_y[i] *= -</span><span class=cFE>1</span><span class=cF0>;
|
|
<a name="l77"></a> ball_y[i] = BALL_RADIUS << </span><span class=cFE>32</span><span class=cF0>;
|
|
<a name="l78"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l79"></a> </span><span class=cF1>if</span><span class=cF0> (ball_y[i] >= </span><span class=cF7>(</span><span class=cF0>v - BALL_RADIUS</span><span class=cF7>)</span><span class=cF0> << </span><span class=cFE>32</span><span class=cF0>)
|
|
<a name="l80"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l81"></a> ball_velocity_y[i] *= -</span><span class=cFE>1</span><span class=cF0>;
|
|
<a name="l82"></a> ball_y[i] = (v - BALL_RADIUS) << </span><span class=cFE>32</span><span class=cF0>;
|
|
<a name="l83"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l84"></a> }
|
|
<a name="l85"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i < BALLS_NUM; i++)
|
|
<a name="l86"></a> {
|
|
<a name="l87"></a> </span><span class=cF1>for</span><span class=cF0> (j = i + </span><span class=cFE>1</span><span class=cF0>; j < BALLS_NUM; j++)
|
|
<a name="l88"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l89"></a> delta_x = ball_x[i] - ball_x[j];
|
|
<a name="l90"></a> delta_y = ball_y[i] - ball_y[j];
|
|
<a name="l91"></a>
|
|
<a name="l92"></a> </span><span class=cF2>//We shift 16 because multiplying</span><span class=cF0>
|
|
<a name="l93"></a> </span><span class=cF2>//two 32 shifted would yield 64 shifted</span><span class=cF0>
|
|
<a name="l94"></a> </span><span class=cF2>//and we want a 32 shifted res.</span><span class=cF0>
|
|
<a name="l95"></a> distdist = </span><span class=cF5>SqrI64</span><span class=cF0>(delta_x >> </span><span class=cFE>16</span><span class=cF0>) + </span><span class=cF5>SqrI64</span><span class=cF0>(delta_y >> </span><span class=cFE>16</span><span class=cF0>);
|
|
<a name="l96"></a>
|
|
<a name="l97"></a> </span><span class=cF2>//We work with square instead of sqrt</span><span class=cF0>
|
|
<a name="l98"></a> </span><span class=cF2>//to avoid unnecessarily calculating</span><span class=cF0>
|
|
<a name="l99"></a> </span><span class=cF2>//square heads (They are slow.)</span><span class=cF0>
|
|
<a name="l100"></a> </span><span class=cF1>if</span><span class=cF0> (distdist && distdist <= diadia)
|
|
<a name="l101"></a> {
|
|
<a name="l102"></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="l103"></a> delta_x /= dist; </span><span class=cF2>//shifted 16</span><span class=cF0>
|
|
<a name="l104"></a> delta_y /= dist;
|
|
<a name="l105"></a>
|
|
<a name="l106"></a> v_t1 = (ball_velocity_x[i] >> </span><span class=cFE>16</span><span class=cF0> * delta_y - ball_velocity_y[i] >> </span><span class=cFE>16</span><span class=cF0> * delta_x) >> </span><span class=cFE>16</span><span class=cF0>;
|
|
<a name="l107"></a> v_n1 = (ball_velocity_x[i] >> </span><span class=cFE>16</span><span class=cF0> * delta_x + ball_velocity_y[i] >> </span><span class=cFE>16</span><span class=cF0> * delta_y) >> </span><span class=cFE>16</span><span class=cF0>;
|
|
<a name="l108"></a> v_t2 = (ball_velocity_x[j] >> </span><span class=cFE>16</span><span class=cF0> * delta_y - ball_velocity_y[j] >> </span><span class=cFE>16</span><span class=cF0> * delta_x) >> </span><span class=cFE>16</span><span class=cF0>;
|
|
<a name="l109"></a> v_n2 = (ball_velocity_x[j] >> </span><span class=cFE>16</span><span class=cF0> * delta_x + ball_velocity_y[j] >> </span><span class=cFE>16</span><span class=cF0> * delta_y) >> </span><span class=cFE>16</span><span class=cF0>;
|
|
<a name="l110"></a>
|
|
<a name="l111"></a> </span><span class=cF1>if</span><span class=cF0> (ball_velocity_x[i] >> </span><span class=cFE>16</span><span class=cF0> * ball_velocity_x[j] >> </span><span class=cFE>16</span><span class=cF0> +
|
|
<a name="l112"></a> ball_velocity_y[i] >> </span><span class=cFE>16</span><span class=cF0> * ball_velocity_y[j] >> </span><span class=cFE>16</span><span class=cF0> <= </span><span class=cFE>0</span><span class=cF0>)
|
|
<a name="l113"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l114"></a> ball_velocity_x[i] = v_t1 * delta_y - v_n1 * delta_x;
|
|
<a name="l115"></a> ball_velocity_y[i] = -v_t1 * delta_x - v_n1 * delta_y;
|
|
<a name="l116"></a> ball_velocity_x[j] = v_t2 * delta_y - v_n2 * delta_x;
|
|
<a name="l117"></a> ball_velocity_y[j] = -v_t2 * delta_x - v_n2 * delta_y;
|
|
<a name="l118"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l119"></a> </span><span class=cF1>else</span><span class=cF0>
|
|
<a name="l120"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l121"></a> ball_velocity_x[i] = v_t1 * delta_y + v_n2 * delta_x;
|
|
<a name="l122"></a> ball_velocity_y[i] = -v_t1 * delta_x + v_n2 * delta_y;
|
|
<a name="l123"></a> ball_velocity_x[j] = v_t2 * delta_y + v_n1 * delta_x;
|
|
<a name="l124"></a> ball_velocity_y[j] = -v_t2 * delta_x + v_n1 * delta_y;
|
|
<a name="l125"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l126"></a>
|
|
<a name="l127"></a> </span><span class=cF2>//Correct for overlap</span><span class=cF0>
|
|
<a name="l128"></a> dist = </span><span class=cFE>0x10000</span><span class=cF0> + (dia / </span><span class=cFE>0x10000</span><span class=cF0> - dist) / </span><span class=cFE>2</span><span class=cF0>;
|
|
<a name="l129"></a> ball_x[i] += dist * delta_x;
|
|
<a name="l130"></a> ball_y[i] += dist * delta_y;
|
|
<a name="l131"></a> ball_x[j] -= dist * delta_x;
|
|
<a name="l132"></a> ball_y[j] -= dist * delta_y;
|
|
<a name="l133"></a> }
|
|
<a name="l134"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l135"></a> }
|
|
<a name="l136"></a> </span><span class=cF5>Sleep</span><span class=cF0>(</span><span class=cFE>1</span><span class=cF0>);
|
|
<a name="l137"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l138"></a>}
|
|
<a name="l139"></a>
|
|
<a name="l140"></a></span><span class=cF1>U0</span><span class=cF0> Init()
|
|
<a name="l141"></a>{
|
|
<a name="l142"></a> </span><span class=cF9>I64</span><span class=cF0> i;
|
|
<a name="l143"></a>
|
|
<a name="l144"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i < BALLS_NUM; i++)
|
|
<a name="l145"></a> </span><span class=cF7>{</span><span class=cF0>
|
|
<a name="l146"></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>->pix_width - BALL_RADIUS * </span><span class=cFE>2</span><span class=cF7>)</span><span class=cF0> + BALL_RADIUS) << </span><span class=cFE>32</span><span class=cF0>;
|
|
<a name="l147"></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>->pix_height - BALL_RADIUS * </span><span class=cFE>2</span><span class=cF7>)</span><span class=cF0> + BALL_RADIUS) << </span><span class=cFE>32</span><span class=cF0>;
|
|
<a name="l148"></a> ball_velocity_x[i] = </span><span class=cF5>RandI32</span><span class=cF0> / </span><span class=cFE>4</span><span class=cF0>;
|
|
<a name="l149"></a> ball_velocity_y[i] = </span><span class=cF5>RandI32</span><span class=cF0> / </span><span class=cFE>4</span><span class=cF0>;
|
|
<a name="l150"></a> </span><span class=cF7>}</span><span class=cF0>
|
|
<a name="l151"></a>}
|
|
<a name="l152"></a>
|
|
<a name="l153"></a></span><span class=cF1>U0</span><span class=cF0> Collision()
|
|
<a name="l154"></a>{
|
|
<a name="l155"></a> </span><span class=cF5>SettingsPush</span><span class=cF0>; </span><span class=cF2>//See </span><a href="https://zeal-operating-system.github.io/ZealOS/System/TaskSettings.CC.html#l3"><span class=cF4>SettingsPush</span></a><span class=cF0>
|
|
<a name="l156"></a> Init;
|
|
<a name="l157"></a> </span><span class=cF5>Fs</span><span class=cF0>->animate_task = </span><span class=cF5>Spawn</span><span class=cF0>(&AnimateTask, </span><span class=cF3>NULL</span><span class=cF0>, </span><span class=cF6>"Animate"</span><span class=cF0>,, </span><span class=cF5>Fs</span><span class=cF0>);
|
|
<a name="l158"></a> </span><span class=cF5>DocCursor</span><span class=cF0>;
|
|
<a name="l159"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
|
|
<a name="l160"></a> </span><span class=cF5>Fs</span><span class=cF0>->draw_it = &</span><span class=cF5>DrawIt</span><span class=cF0>;
|
|
<a name="l161"></a> </span><span class=cF5>CharGet</span><span class=cF0>;
|
|
<a name="l162"></a> </span><span class=cF5>SettingsPop</span><span class=cF0>;
|
|
<a name="l163"></a>}
|
|
<a name="l164"></a>
|
|
<a name="l165"></a>Collision;
|
|
</span></pre></body>
|
|
</html>
|