<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Another question on Angles &amp; Vectors</title>
		<link>http://www.allegro.cc/forums/view/591703</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 05 Jun 2007 16:28:02 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Heeeeelp.    </p><p>I have two circles moving around on-screen.<br />I can handle the collision detection with no problems but due to my lack of Math skills am still struggling to get a handle on vectors etc.</p><p>If my two circles are:</p><p>Circle 1 = x1,y1,vx1,vy1,r1<br />Circle 2 = x2,y2,vx2,vy2,r2</p><p>x,y = screen position<br />vx,vy = velocity<br />r= radius</p><p>Would anyone be as kind as to write a few lines of code to enable the angle of bounce to be calculated?  I&#39;ve spent about the last week trying to get my head around vectors and dot products, but am having no luck <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gary_ramsgate)</author>
		<pubDate>Sat, 02 Jun 2007 14:21:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Time n: Circles don&#39;t intersect<br />Time n+1: Circles do intersect</p><p>The collision should have appeared somewhere between time n and time n+1. Think of time as discrete frames. If you want a ralistic collision, you have to calculate the exact time n+d of the collision, do the collision calculus and then calculate the situation for frame n+1. The simpler way is to act like if the collision took place in n+1, even though the circles intersect in n+1. In n+2 the circles have moved in their new directions. But they might still intersect in n+2, which might cause problem. You can also make it simpler by actink like the collision took place in frame n. You kind of peek into n+1 and find out that the circles intersect. Then you calculate the collision and calculate the n+1 situation with the new directions. If the time steps are small enough, you can go this simpler way. Say the radius is 10 and circle movement from frame to frame is 0.1.</p><p>Anyway, the collision calculus is something as follows:<br />Define the line or vector from one circle to the other.<br />Find the point on this line where the circles touch. This is the collision point.<br />Define the line through this point perpendicular to the first line. This is the tangent of both circles. <br />Think like both circles collide against the tangent.<br />A collision against a wall: divide the speed vector into a normal vector and a tangent vector. Tangent vector has same direction as the wall. Normal vector is perpendicular. In collision nothing happens to the tangent component, but the normal component gets negated.<br />But in you case the two circles collide against each other, not against a wall. Swap the normal vectors of the circles!</p><p>Imagine two beads, one is at rest and the other hits it. The one at rest takes completely over the speed of the other bead, while the other bead stops. They swap their speeds! If they however have different masses, the situation might be other. In any collision of two beads or two circles, you can think of them swapping the normal vectors, while the tangent vectors are unchanged.</p><p>I should probably complete this with a drawing. And <i>do</i> study vector maths! Dividing vx and vy into vx<sub>normal</sub>, vy<sub>normal</sub>, vx<sub>tangent</sub> and vy<sub>tangent</sub> is heavy pain compared with doing it with vectors. With my vector class I would do something like.
</p><div class="source-code snippet"><div class="inner"><pre>Vektor normal<span class="k2">(</span>circle1.coord, circle2.coord<span class="k2">)</span><span class="k2">;</span>
Vektor c1_vt, c1_vn, c2_vt, c2_vn<span class="k2">;</span>
circle1.speed.components<span class="k2">(</span>normal, <span class="k3">&amp;</span>c1_vt, <span class="k3">&amp;</span>c1_vn<span class="k2">)</span><span class="k2">;</span> <span class="c">// divide speed into two vectors, one </span>
                                                  <span class="c">//in normal direction, other perpendicular to normal</span>
circle2.speed.components<span class="k2">(</span>normal, <span class="k3">&amp;</span>c2_vt, <span class="k3">&amp;</span>c2_vn<span class="k2">)</span><span class="k2">;</span> <span class="c">// ditto</span>
circle1.speed <span class="k3">=</span> c1_vt <span class="k3">+</span> c2_vn<span class="k2">;</span>
circle2.speed <span class="k3">=</span> c2_vt <span class="k3">+</span> c1_vn<span class="k2">;</span>
</pre></div></div><p>

[edit]<br />Here we go...<br /><span class="remote-thumbnail"><span class="json">{"name":"592270","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/4\/34b355a2b9684f120b94ac6582e33e16.png","w":392,"h":510,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/4\/34b355a2b9684f120b94ac6582e33e16"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/3/4/34b355a2b9684f120b94ac6582e33e16-240.jpg" alt="592270" width="240" height="312" /></span><br />Two circles colliding. Black vectors show their velocities.<br />http://www.allegro.cc/files/attachment/592271<br />Find out the velocity vectors&#39; tangent components (blue vectors) and normal components (red vectors)<br />http://www.allegro.cc/files/attachment/592272<br />Exchange the red vectors and add the red and blue vectors and you have your new velocities (black vectors).</p><p>Someone take over from this point and explain how the collision makes the objects spin and how the spinning affects further collisions.</p><p>Now why did you make me explain all this? Now I have to make a simple simulation application to find out this all is not just bulls hit. <img src="http://www.allegro.cc/forums/smileys/angry.gif" alt="&gt;:(" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Johan Halmén)</author>
		<pubDate>Sun, 03 Jun 2007 14:17:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Not a reply but an article itself. Bookmarked!. Johan, it would be nice if you continue with this stuff, this thread has become really interesting now.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Sun, 03 Jun 2007 22:31:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t intend to. That&#39;s all there is. The spinning thing could be interesting, but there are too many things that are involved in it. Are the objects like circles like in air hockey? Or are they balls like in pools? Are they slippery against the surface or do they stick like rubber? Not to mention differing masses.</p><p>[edit]<br />Well, this I did. The files are attached. I did it with OSX and Xcode, but it probably runs well on any machine. There&#39;s even a dialog in the code for some settings, but I didn&#39;t implement it yet. Probably won&#39;t. Three coloured circles move inside a box and collide against the box and each other. The objects actually never collide. They bounce in frame n if frame n+1 would make them overlap. And after they have bounced, they will move in the new direction no matter if the movement makes them overlap another object. If this occures, I have no idea what happens. They might get stuck inside each other. And each time frame gets rendered. In a game the time frame should be as short as possible while the rendering rate could be some 50 - 100 fps.</p><p><span class="remote-thumbnail"><span class="json">{"name":"592291","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/8\/689a8a93efe3d3b96959c8a908edd4d4.png","w":809,"h":629,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/8\/689a8a93efe3d3b96959c8a908edd4d4"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/6/8/689a8a93efe3d3b96959c8a908edd4d4-240.jpg" alt="592291" width="240" height="186" /></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Johan Halmén)</author>
		<pubDate>Sun, 03 Jun 2007 22:54:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I found some code on the net, which I&#39;ve modified slightly to suit my program.<br />It seems to work, with the balls bouncing off each other correctly.  I&#39;m not sure how it works, but it does :-)</p><p>sprite[??].x, sprite[??].y - sprite co-ordinates<br />sprite[??].dx, sprite[??].dy - sprite velocity</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="c">//This is the main physics function for the balls.  </span></td></tr><tr><td class="number">2</td><td><span class="c">//It contains very basic but effective collision response code </span></td></tr><tr><td class="number">3</td><td><span class="c">//the following code was modified by code written by Joseph 'Phish' Humfrey</span></td></tr><tr><td class="number">4</td><td>  </td></tr><tr><td class="number">5</td><td><span class="k1">double</span> c_mass<span class="k3">=</span><span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td><span class="k1">double</span> c2_mass<span class="k3">=</span><span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td><span class="k1">double</span> collNormalAngle<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_50.html" target="_blank">atan2</a><span class="k2">(</span>sprite<span class="k2">[</span>n1<span class="k2">]</span>.y-sprite<span class="k2">[</span>n<span class="k2">]</span>.y,sprite<span class="k2">[</span>n1<span class="k2">]</span>.x-sprite<span class="k2">[</span>n<span class="k2">]</span>.x<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>&#160;</td></tr><tr><td class="number">9</td><td><span class="c">// n=vector connecting the centre of the balls</span></td></tr><tr><td class="number">10</td><td><span class="c">// we are finding the components of the normalised vector n</span></td></tr><tr><td class="number">11</td><td><span class="k1">double</span> nX<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>collNormalAngle<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td><span class="k1">double</span> nY<span class="k3">=</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>collNormalAngle<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>&#160;</td></tr><tr><td class="number">14</td><td><span class="c">// Now find the length of the components of each movement vectors</span></td></tr><tr><td class="number">15</td><td><span class="c">// along n, by using a dot product</span></td></tr><tr><td class="number">16</td><td><span class="k1">double</span> a1<span class="k3">=</span>sprite<span class="k2">[</span>n<span class="k2">]</span>.dx<span class="k3">*</span>nX <span class="k3">+</span> sprite<span class="k2">[</span>n<span class="k2">]</span>.dy<span class="k3">*</span>nY<span class="k2">;</span></td></tr><tr><td class="number">17</td><td><span class="k1">double</span> a2<span class="k3">=</span>sprite<span class="k2">[</span>n1<span class="k2">]</span>.dx<span class="k3">*</span>nX <span class="k3">+</span> sprite<span class="k2">[</span>n1<span class="k2">]</span>.dy<span class="k3">*</span>nY<span class="k2">;</span></td></tr><tr><td class="number">18</td><td><span class="k1">double</span> optimisedP <span class="k3">=</span> <span class="k2">(</span><span class="n">2</span>.<span class="n">0</span> <span class="k3">*</span> <span class="k2">(</span>a1-a2<span class="k2">)</span><span class="k2">)</span> <span class="k3">/</span> <span class="k2">(</span>c_mass <span class="k3">+</span>c2_mass<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>&#160;</td></tr><tr><td class="number">20</td><td><span class="c">// Now find the resultant vectors</span></td></tr><tr><td class="number">21</td><td>sprite<span class="k2">[</span>n<span class="k2">]</span>.dx<span class="k3">=</span>sprite<span class="k2">[</span>n<span class="k2">]</span>.dx <span class="k3">-</span> <span class="k2">(</span>optimisedP <span class="k3">*</span> c2_mass <span class="k3">*</span> nX<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">22</td><td>sprite<span class="k2">[</span>n<span class="k2">]</span>.dy<span class="k3">=</span>sprite<span class="k2">[</span>n<span class="k2">]</span>.dy <span class="k3">-</span> <span class="k2">(</span>optimisedP <span class="k3">*</span> c2_mass <span class="k3">*</span> nY<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td>sprite<span class="k2">[</span>n1<span class="k2">]</span>.dx<span class="k3">=</span>sprite<span class="k2">[</span>n1<span class="k2">]</span>.dx <span class="k3">+</span> <span class="k2">(</span>optimisedP <span class="k3">*</span> c_mass <span class="k3">*</span> nX<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>sprite<span class="k2">[</span>n1<span class="k2">]</span>.dy<span class="k3">=</span>sprite<span class="k2">[</span>n1<span class="k2">]</span>.dy <span class="k3">+</span> <span class="k2">(</span>optimisedP <span class="k3">*</span> c_mass <span class="k3">*</span> nY<span class="k2">)</span><span class="k2">;</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gary_ramsgate)</author>
		<pubDate>Tue, 05 Jun 2007 16:28:02 +0000</pubDate>
	</item>
</rss>
