<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Warpy Code Maths Blog</title>
	<atom:link href="http://warpycode.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://warpycode.wordpress.com</link>
	<description>Nice explanations of programming maths</description>
	<lastBuildDate>Sat, 09 Jul 2011 17:48:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='warpycode.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Warpy Code Maths Blog</title>
		<link>http://warpycode.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://warpycode.wordpress.com/osd.xml" title="Warpy Code Maths Blog" />
	<atom:link rel='hub' href='http://warpycode.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Ball-to-ball collision</title>
		<link>http://warpycode.wordpress.com/2011/05/08/ball-to-ball-collision/</link>
		<comments>http://warpycode.wordpress.com/2011/05/08/ball-to-ball-collision/#comments</comments>
		<pubDate>Sun, 08 May 2011 19:05:39 +0000</pubDate>
		<dc:creator>christianperfect</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://warpycode.wordpress.com/?p=190</guid>
		<description><![CDATA[Mario writes, I wanted to ask you if among all those great blog-tutorials of yours, there is simple ball-to-ball collision resonse tutorial. Like billiard kind of 2D physics? I need it for a game Im working on that needs collision response between circular asteroids. (including mass) If not, would you please consider writing one? There [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=190&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Mario writes,</p>
<blockquote>
<div>I wanted to ask you if among all those great blog-tutorials of yours, there is simple ball-to-ball collision resonse tutorial. Like billiard kind of 2D physics? I need it for a game Im working on that needs collision response between circular asteroids. (including mass)</div>
<div>If not, would you please consider writing one?</div>
</blockquote>
<p>There probably is, but it&#8217;ll be buried somewhere on the blitzbasic.com forums. So, here&#8217;s a new one!</p>
<p>The key insight for ball-to-ball collisions is that, at the moment of impact, the distance between the two balls is the sum of their radii, and the repulsive force is applied along the line between their centres. I&#8217;m going to use a lot of vector maths here, because it makes the maths a lot clearer.</p>
<p>So, the first task to detect a collision is to get the distance between the centres of the two balls. If the distance is less than their radii added together, then the balls have collided.</p>
<p>I&#8217;ll build up what needs to happen next through a few example cases. I&#8217;m not going to really explain the mechanics in depth because there&#8217;s loads of messy algebra and I have better things to do, but this should give you the idea.</p>
<h3>Case 1: Ball 1 is stationary, ball 2 is travelling directly at ball 1, both balls have the same mass.</h3>
<p>When the balls collide, the total momentum needs to be preserved, and the forces applied to each ball need to be of the same size. I&#8217;ll also assume the collision is elastic, so the total kinetic energy of the system stays the same. The only solution is that all of ball 2&#8242;s momentum is transferred to ball 1. Ball 2 stops and ball 1 starts moving in the same direction and at the same speed as ball 2.</p>
<h3><span class="Apple-style-span" style="font-size:15px;">Case 2: Ball 1 is stationary, ball 2 is travelling directly at ball 2, different masses.</span></h3>
<p>One of Newton&#8217;s laws of motion is that force  is equal to mass times acceleration:</p>
<p><img src='http://s0.wp.com/latex.php?latex=F+%3D+ma&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F = ma' title='F = ma' class='latex' /></p>
<p>As the forces applied to each ball need to be equal, it follows that if the balls have different masses, the heavier ball accelerates less than the lighter one. In fact, the acceleration of ball 1 is proportional to</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7Bm_2%7D%7Bm_1%2Bm_2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;frac{m_2}{m_1+m_2}' title='&#92;frac{m_2}{m_1+m_2}' class='latex' /></p>
<p>This way, the momentum is conserved:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brl%7D+m_1a_1+%2B+m_2a_2+%26%3D+m_1+%5Cleft%28+%5Cfrac%7Bm_2%7D%7Bm_1%2Bm_2%7D+%5Cright%29+%2B+m_2+%5Cleft%28+%5Cfrac%7B-m_1%7D%7Bm_1%2Bm_2%7D+%5Cright%29+%5C%5C+%26%3D+%5Cfrac%7Bm_1m_2%7D%7Bm_1%2Bm_2%7D+-+%5Cfrac%7Bm_2m_1%7D%7Bm_1%2Bm_2%7D+%3D+0+%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rl} m_1a_1 + m_2a_2 &amp;= m_1 &#92;left( &#92;frac{m_2}{m_1+m_2} &#92;right) + m_2 &#92;left( &#92;frac{-m_1}{m_1+m_2} &#92;right) &#92;&#92; &amp;= &#92;frac{m_1m_2}{m_1+m_2} - &#92;frac{m_2m_1}{m_1+m_2} = 0 &#92;end{array}' title='&#92;begin{array}{rl} m_1a_1 + m_2a_2 &amp;= m_1 &#92;left( &#92;frac{m_2}{m_1+m_2} &#92;right) + m_2 &#92;left( &#92;frac{-m_1}{m_1+m_2} &#92;right) &#92;&#92; &amp;= &#92;frac{m_1m_2}{m_1+m_2} - &#92;frac{m_2m_1}{m_1+m_2} = 0 &#92;end{array}' class='latex' /></p>
<h3>Case 3: Both balls moving, ball 2 is travelling directly at ball 1, equal masses.</h3>
<p>When both balls are moving, you can make the calculation simpler by considering only the <em>relative </em>velocities of the balls. By subtracting ball 1&#8242;s velocity from ball 2&#8242;s, you can perform the calculation as if ball 1 is stationary, and the resulting force will be the same.</p>
<h3>Case 4: Ball 1 stationary, ball 2 hits ball 1 at an angle, equal masses.</h3>
<p>The force is applied along the line between the two balls, so when that line isn&#8217;t parallel to the relative velocity, the balls bounce off each other at an angle.</p>
<p>All those cases put together should be enough to solve collisions in general. Here&#8217;s some code:</p>
<pre>Global gwidth=800,gheight=600

Global balls:TList=New TList
Type ball
	Field x#,y#		'position
	Field vx#,vy#		'velocity
	Field radius#
	Field mass#

	Method New()
		balls.addlast Self
	End Method

	Function Create:ball(x#,y#,radius#,mass#)
		b:ball=New ball
		b.x=x
		b.y=y
		b.radius=radius
		b.mass=mass
		b.vx=(gwidth/2-x)*.01
		b.vy=(gheight/2-y)*.01
		Return b
	End Function

	Method update()
		x:+vx
		y:+vy

		'wrap edges of screen around
		If x&lt;0
			x:+gwidth
		EndIf
		If x&gt;gwidth
			x:-gwidth
		EndIf
		If y&lt;0
			y:+gheight
		EndIf
		If y&gt;gheight
			y:-gheight
		EndIf
	End Method

	Function collideAll()
		l:TList=balls.Copy()
		While l.Count()&gt;1
			b:ball=ball(l.removefirst())
			For b2:ball=EachIn l

				dx#=b2.x-b.x		'get relative positions of b2 with respect to b
				dy#=b2.y-b.y
				d#=Sqr(dx*dx+dy*dy)	'get square of distance between b and b2

				dd#=b.radius+b2.radius-d	'get amount balls overlap

				If dd&gt;0	'if distance between balls is less than their radii added together, then they are overlapping

					dx:/d	'by dividing the difference between the two balls' positions, we get a unit vector pointing from b to b2
					dy:/d

					dvx#=b2.vx-b.vx	'get relative velocity of b2 with respect to b
					dvy#=b2.vy-b.vy

					f#=dvx*dx+dvy*dy		'calculate force of impact - project relative velocity vector onto line from b to b2
					f:*2/(b.mass+b2.mass)	

					b2.vx:-dx*f*b.mass	'push b2 away from b
					b2.vy:-dy*f*b.mass

					b.vx:+dx*f*b2.mass	'push b in the opposite direction
					b.vy:+dy*f*b2.mass

					b.x:-dd*dx*b2.mass/(b.mass+b2.mass)	'position the balls so they are just touching and no longer overlap
					b.y:-dd*dy*b2.mass/(b.mass+b2.mass)
					b2.x:+dd*dx*b.mass/(b.mass+b2.mass)
					b2.y:+dd*dy*b.mass/(b.mass+b2.mass)

				EndIf

			Next
		Wend
	End Function

	Method draw()
		shade#=mass*5
		SetColor shade,shade,shade
		DrawOval x-radius,y-radius,radius*2,radius*2

		'because the edges of the screen wrap round, draw again shifted by a screen's width/height to maintain the illusion
		DrawOval gwidth+x-radius,y-radius,radius*2,radius*2
		DrawOval x-radius,gheight+y-radius,radius*2,radius*2
		DrawOval -gwidth+x-radius,y-radius,radius*2,radius*2
		DrawOval x-radius,-gheight+y-radius,radius*2,radius*2
	End Method

End Type

Function changeState()
	balls=New TList
	state=(state+1) Mod 5
	Select state
	Case 0
		b1:ball=ball.Create(400,300,50,50)
		b1.vx=0
		b1.vy=0
		b2:ball=ball.Create(200,300,50,50)
		b2.vx=1
		b2.vy=0
	Case 1
		b1:ball=ball.Create(400,300,50,50)
		b1.vx=0
		b1.vy=0
		b2:ball=ball.Create(200,300,20,20)
		b2.vx=1
		b2.vy=0
	Case 2
		b1:ball=ball.Create(400,300,50,50)
		b1.vx=1
		b1.vy=0
		b2:ball=ball.Create(200,300,50,50)
		b2.vx=2
		b2.vy=0

		b3:ball=ball.Create(400,450,50,50)
		b3.vx=0
		b3.vy=0
		b4:ball=ball.Create(200,450,50,50)
		b4.vx=1
		b4.vy=0
	Case 3
		b1:ball=ball.Create(400,300,50,50)
		b1.vx=-1
		b1.vy=0
		b2:ball=ball.Create(200,250,50,50)
		b2.vx=1
		b2.vy=0

	Case 4
		For c=1 To 5
			radius#=Rnd(10,50)
			mass=Rnd(.5,1)*radius
			ball.Create Rand(radius,gwidth-radius),Rand(radius,gheight-radius),radius,mass
		Next
	End Select
End Function

Function drawlines(txt$,x,y)
	Local lines$[]=txt.split("~n")
	For i=0 To Len(lines)-1
		DrawText lines[i],0,y
		y:+13
	Next
End Function

Graphics gwidth,gheight,0
SeedRnd MilliSecs()

Global state=-1
Global statetxt$[]=[	"Ball 1 stationary~nBall 2 heading directly at ball 1~nEqual masses",..
					"Ball 1 stationary~nBall 2 heading directly at ball 1~nDifferent masses",..
					"Both balls moving~nBall 2 heading directly at ball 1~nEqual masses~nBall 1's frame of reference shown below",..
					"Ball 1 stationary~nBall 2 hits ball 1 at an angle~nEqual masses",..
					"Lots of balls all over the place!"]

changeState

While Not (KeyHit(KEY_ESCAPE) Or AppTerminate())

	If KeyHit(KEY_SPACE)
		changeState
	EndIf

	ball.collideAll

	For b:ball=EachIn balls
		b.update
	Next

	For b:ball=EachIn balls
		b.draw
	Next

	SetColor 255,255,255
	Drawlines statetxt[state],0,0

	Flip
	Cls
Wend</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpycode.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpycode.wordpress.com/190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpycode.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpycode.wordpress.com/190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/warpycode.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/warpycode.wordpress.com/190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/warpycode.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/warpycode.wordpress.com/190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpycode.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpycode.wordpress.com/190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpycode.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpycode.wordpress.com/190/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpycode.wordpress.com/190/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpycode.wordpress.com/190/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=190&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://warpycode.wordpress.com/2011/05/08/ball-to-ball-collision/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/872c129f4453b92c978756334c8a18ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">christianperfect</media:title>
		</media:content>
	</item>
		<item>
		<title>Calculating the distance to the edge of an ellipse</title>
		<link>http://warpycode.wordpress.com/2011/01/21/calculating-the-distance-to-the-edge-of-an-ellipse/</link>
		<comments>http://warpycode.wordpress.com/2011/01/21/calculating-the-distance-to-the-edge-of-an-ellipse/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 08:38:21 +0000</pubDate>
		<dc:creator>christianperfect</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://warpycode.wordpress.com/?p=174</guid>
		<description><![CDATA[Johnsprogram asks, I&#8217;m having a math-related crisis that one man (me) can not do alone. I&#8217;m trying to figure out a way to calculate the radius of the ellipse on the same angle as the light blue line; in other words, the length of the &#8220;mystery line.&#8221; He attached a picture which didn&#8217;t do too [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=174&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Johnsprogram <a href="http://www.blitzbasic.com/Community/posts.php?topic=93463#1069509">asks</a>,</p>
<blockquote><p>I&#8217;m having a math-related crisis that one man (me) can not do alone. I&#8217;m trying to figure out a way to calculate the radius of the ellipse on the same angle as the light blue line; in other words, the length of the &#8220;mystery line.&#8221;</p></blockquote>
<p>He attached a picture which didn&#8217;t do too much to help explain matters, but what he wanted to know was the distance from the centre of the ellipse to the edge, at a given angle.</p>
<p>We can assume that the ellipse is oriented vertically. The <a href="http://en.wikipedia.org/wiki/Locus_%28mathematics%29" target="_blank">locus</a> of the edge of the ellipse is, in this case,</p>
<p><img src='http://s0.wp.com/latex.php?latex=%28%5Cfrac%7Bx%7D%7Br_x%7D%29%5E2+%2B+%28%5Cfrac%7By%7D%7Br_y%7D%29%5E2+%3D+1%2C&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(&#92;frac{x}{r_x})^2 + (&#92;frac{y}{r_y})^2 = 1,' title='(&#92;frac{x}{r_x})^2 + (&#92;frac{y}{r_y})^2 = 1,' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=r_x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='r_x' title='r_x' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=r_y&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='r_y' title='r_y' class='latex' /> are the horizontal and vertical radii.</p>
<p>We want to know the length of the line heading at an angle <img src='http://s0.wp.com/latex.php?latex=%5Ctheta&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;theta' title='&#92;theta' class='latex' /> from the vertical to the edge of the ellipse. Call the length <img src='http://s0.wp.com/latex.php?latex=L&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='L' title='L' class='latex' />, so the co-ordinates of the end are given by</p>
<p><img src='http://s0.wp.com/latex.php?latex=x+%3D+L+%5Csin%28%5Ctheta%29%2C&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x = L &#92;sin(&#92;theta),' title='x = L &#92;sin(&#92;theta),' class='latex' /></p>
<p><img src='http://s0.wp.com/latex.php?latex=y+%3D+L+%5Ccos%28%5Ctheta%29.&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y = L &#92;cos(&#92;theta).' title='y = L &#92;cos(&#92;theta).' class='latex' /></p>
<p>Substitute those into the first equation:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%28L+%5Cfrac%7B%5Csin%28%5Ctheta%29%7D%7Br_x%7D%29%5E2+%2B+%28L+%5Cfrac%7B%5Ccos%28%5Ctheta%29%7D%7Br_y%7D%29%5E2+%3D+1.&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(L &#92;frac{&#92;sin(&#92;theta)}{r_x})^2 + (L &#92;frac{&#92;cos(&#92;theta)}{r_y})^2 = 1.' title='(L &#92;frac{&#92;sin(&#92;theta)}{r_x})^2 + (L &#92;frac{&#92;cos(&#92;theta)}{r_y})^2 = 1.' class='latex' /></p>
<p>Take a factor of <img src='http://s0.wp.com/latex.php?latex=L%5E2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='L^2' title='L^2' class='latex' /> out:</p>
<p><img src='http://s0.wp.com/latex.php?latex=L%5E2+%28+%5Cleft+%28+%5Cfrac%7B%5Csin%28%5Ctheta%29%7D%7Br_x%7D+%5Cright+%29%5E2+%2B+%5Cleft+%28+%5Cfrac%7B%5Ccos%28%5Ctheta%29%7D%7Br_y%7D+%5Cright+%29%5E2%29+%3D+1%2C&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='L^2 ( &#92;left ( &#92;frac{&#92;sin(&#92;theta)}{r_x} &#92;right )^2 + &#92;left ( &#92;frac{&#92;cos(&#92;theta)}{r_y} &#92;right )^2) = 1,' title='L^2 ( &#92;left ( &#92;frac{&#92;sin(&#92;theta)}{r_x} &#92;right )^2 + &#92;left ( &#92;frac{&#92;cos(&#92;theta)}{r_y} &#92;right )^2) = 1,' class='latex' /></p>
<p>and rearrange:</p>
<p><img src='http://s0.wp.com/latex.php?latex=L+%3D+%5Csqrt%7B+%5Cfrac%7B1%7D%7B%5Cleft+%28+%5Cfrac%7B%5Csin%28%5Ctheta%29%7D%7Br_x%7D+%5Cright+%29%5E2+%2B+%5Cleft+%28+%5Cfrac%7B%5Ccos%28%5Ctheta%29%7D%7Br_y%7D+%5Cright+%29%5E2%7D%7D.&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='L = &#92;sqrt{ &#92;frac{1}{&#92;left ( &#92;frac{&#92;sin(&#92;theta)}{r_x} &#92;right )^2 + &#92;left ( &#92;frac{&#92;cos(&#92;theta)}{r_y} &#92;right )^2}}.' title='L = &#92;sqrt{ &#92;frac{1}{&#92;left ( &#92;frac{&#92;sin(&#92;theta)}{r_x} &#92;right )^2 + &#92;left ( &#92;frac{&#92;cos(&#92;theta)}{r_y} &#92;right )^2}}.' class='latex' /></p>
<p>Here&#8217;s some code:</p>
<pre>
<pre>Graphics 600,600,0

an = 60
While Not (KeyHit(KEY_ESCAPE) Or AppTerminate())

	ew# = MouseX()-300
	eh# = MouseY()-300

	SetColor 100,100,100
	DrawOval 300-ew,300-eh,ew*2,eh*2

	an:+1

	l# = Sqr( 1/( (Sin(an)/ew)^2 + (Cos(an)/eh)^2) )

	lx# = 300+l*Sin(an)
	ly# = 300-l*Cos(an)

	SetColor 0,0,255
	DrawLine 300,300,lx,ly

	Flip
	Cls
Wend</pre>
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpycode.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpycode.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpycode.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpycode.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/warpycode.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/warpycode.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/warpycode.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/warpycode.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpycode.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpycode.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpycode.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpycode.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpycode.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpycode.wordpress.com/174/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=174&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://warpycode.wordpress.com/2011/01/21/calculating-the-distance-to-the-edge-of-an-ellipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/872c129f4453b92c978756334c8a18ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">christianperfect</media:title>
		</media:content>
	</item>
		<item>
		<title>Drawing dotted lines</title>
		<link>http://warpycode.wordpress.com/2011/01/10/170/</link>
		<comments>http://warpycode.wordpress.com/2011/01/10/170/#comments</comments>
		<pubDate>Mon, 10 Jan 2011 22:45:03 +0000</pubDate>
		<dc:creator>christianperfect</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://warpycode.wordpress.com/?p=170</guid>
		<description><![CDATA[Suppose you&#8217;ve got a set of points you want to join up with dotted lines, spaced a certain distance apart. Bresenham isn&#8217;t the right algorithm for this &#8211; the gaps between the dots are bound to be a lot bigger than single pixels, so &#8216;stupid&#8217; linear interpolation should do the job satisfactorily. However, if you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=170&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Suppose you&#8217;ve got a set of points you want to join up with dotted lines, spaced a certain distance apart.</p>
<p>Bresenham isn&#8217;t the right algorithm for this &#8211; the gaps between the dots are bound to be a lot bigger than single pixels, so &#8216;stupid&#8217; linear interpolation should do the job satisfactorily.</p>
<p>However, if you want to make sure you definitely draw the start and end points of a line, as well as dots in between, or if you want to draw a line made up of several segments, it gets a bit more complicated.</p>
<p>Here are two methods:</p>
<p>The first tries to draw each line segment so that its start and end points get a dot, and then it changes the size of the gaps slightly so that all the dots are evenly spaced right up to the end.</p>
<p>The second goes along between the points, drawing dots at the given distance apart from each other. It might not draw dots exactly on the given points, but the dots are always exactly the right distance apart.</p>
<pre>Strict

'this method will draw each line segment individually
'it will change the gap slightly so that the dots are spread evenly along each segment
'it always draws a dot on each corner and endpoint
Function drawdots1(points#[], gap#)
	If Len(points)&lt;4 Return	'need at least two pairs of co-ordinates

	Local sx#,sy#,ex#,ey#
	Local dx#,dy#,d#,steps,tgap#
	Local x#,y#

	DrawDot points[0],points[1]	'draw a dot at the start of the line

	For Local i=0 To Len(points)-3 Step 2		'draw each line segment

		sx=points[i]			'get start and end of this segment
		sy=points[i+1]
		ex=points[i+2]
		ey=points[i+3]

		dx# = ex-sx			'calculate a vector in the direction of the line, so we can get the distance
		dy# = ey-sy
		d# = Sqr(dx*dx+dy*dy)

		steps = round(d/gap)	'work out how many dots to draw (distance between the points, divide by the desired gap size). Round to nearest whole number
		tgap# = d/steps		'work out what the gap needs to be to space the calculated number of needed dots evenly

		dx:*tgap/d			'work out the vector between each dot
		dy:*tgap/d

		For Local j=1 To steps
			x# = sx+j*dx	'calculate the dot's position by adding the gap vector to the position of the start of the line
			y# = sy+j*dy
			DrawDot x,y	'draw a dot
		Next
	Next
End Function

'this method will draw dots evenly spaced along the whole set of lines
'it might not draw dots on the corners.
Function drawdots2(points#[],gap#)
	If Len(points)&lt;4 Return	'need at least two pairs of co-ordinates

	Local sx#,sy#,ex#,ey#
	Local dx#,dy#,d#,tstep#
	Local x#,y#

	Local t#=0	't will keep track of how far along each line segment we are - 0 for at the start, 1 for at the end

	For Local i=0 To Len(points)-3 Step 2	'go through the line segments

		sx=points[i]			'get start and end of this segment
		sy=points[i+1]
		ex=points[i+2]
		ey=points[i+3]

		dx = ex-sx			'work out a vector in the direction of the line
		dy = ey-sy
		d = Sqr(dx*dx+dy*dy)	'work out the length of the line

		tstep# = gap/d		'work out what fraction of the line each gap represents

		While t&lt;1		'draw dots until we reach the end of the line

			x = sx+dx*t	'work out the position of the dot by multiplying the vector by t
			y = sy+dy*t
			DrawDot x,y	'draw the dot
			t:+tstep		'increase t by the amount corresponding to a gap
		Wend

		t:-1				'when we reach the end of the line, t might be more than 1, meaning we didn't manage to get a whole gap in at the end
						'subtract 1 from t, and carry over the remainder to the next line segment
	Next

	DrawDot ex,ey			'draw the end point of the line.
End Function

Function DrawDot(x#,y#)
	DrawOval x-2,y-2,4,4
End Function

Function Round(f#)	'round a floating point number to the nearest whole number
	Local i = Floor(f)
	If f-i&gt;=.5
		Return i+1
	Else
		Return i
	EndIf
End Function

Graphics 800,600,0
Local points#[]
Local mode=0
Local gapsize#=20

While Not (KeyHit(KEY_ESCAPE) Or AppTerminate())
	DrawText "Click the mouse!",0,0
	DrawText "Right-click to change methods",0,15
	DrawText "Press up/down to change gap size",0,30

	If MouseHit(1)
		points :+ [Float(MouseX()),Float(MouseY())]
		If Len(points)&gt;16
			points=points[2..]
		EndIf
	EndIf

	If MouseHit(2)
		mode=1-mode
	EndIf

	gapsize :+ (KeyDown(KEY_UP)-KeyDown(KEY_DOWN))*.25
	If gapsize&lt;1 gapsize=1
	DrawText "Gap Size: "+gapsize,400,15

	If mode=0
		drawdots1(points,gapsize)
		DrawText "Using Method 1",400,0
	Else
		drawdots2(points,gapsize)
		DrawText "Using Method 2",400,0
	EndIf
	Flip
	Cls
Wend</pre>
<p><strong>Edited on 4/5/2011: </strong>anubis pointed out that the posted code was all mangled. I think I&#8217;ve fixed it, but in case I haven&#8217;t, <a href="http://somethingorotherwhatever.com/draw_dotty_lines.bmx" target="_blank">here&#8217;s a link to the bmx file</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpycode.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpycode.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpycode.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpycode.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/warpycode.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/warpycode.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/warpycode.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/warpycode.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpycode.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpycode.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpycode.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpycode.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpycode.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpycode.wordpress.com/170/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=170&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://warpycode.wordpress.com/2011/01/10/170/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/872c129f4453b92c978756334c8a18ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">christianperfect</media:title>
		</media:content>
	</item>
		<item>
		<title>Equation of a line perpendicular to another</title>
		<link>http://warpycode.wordpress.com/2010/08/26/equation-of-a-line-perpendicular-to-another/</link>
		<comments>http://warpycode.wordpress.com/2010/08/26/equation-of-a-line-perpendicular-to-another/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 19:59:54 +0000</pubDate>
		<dc:creator>christianperfect</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://warpycode.wordpress.com/?p=161</guid>
		<description><![CDATA[On the blitzbasic.com forums, Ryan Moody asks: I know the following: - The equation of the line L1 - The point (x1, y1) - The perpendicular distance d - L1 and L are parallel However, I need to determine either: - The equation of the line L, or - The point (X, Y) (from which [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=161&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>On the blitzbasic.com forums, <a href="http://www.blitzbasic.com/Community/posts.php?topic=91690">Ryan Moody asks</a>:</p>
<blockquote><p>I know the following:</p>
<p>- The equation of the line L1<br />
- The point (x1, y1)<br />
- The perpendicular distance d<br />
- L1 and L are parallel</p>
<p>However, I need to determine either:</p>
<p>- The equation of the line L, or<br />
- The point (X, Y) (from which the equation of the line L can be easily derived)</p></blockquote>
<p>Easy enough, when you think in terms of vectors.</p>
<ul>
<li>Let <strong>a</strong> be the point (x1,y1) on L1. Let <strong>b</strong> be the direction of L1.</li>
<li>Find the unit vector <strong>n</strong> perpendicular to <strong>b</strong>.</li>
<li>Then the point (x,y) = <strong>a</strong>+d*<strong>n</strong>.</li>
</ul>
<p>Here&#8217;s some BlitzMax code:</p>
<p><code>Graphics 600,600,0</code></p>
<p><code>Global ax#=300,ay=300#	'a point on L1<br />
Global bx#,by#	'the direction of L1<br />
Global d#=50		'the distance of L from L1</p>
<p>While Not (KeyHit(KEY_ESCAPE) Or AppTerminate())<br />
'interaction stuff<br />
If MouseHit(1)	'left click to place point A<br />
ax=MouseX()<br />
ay=MouseY()<br />
Else				'L1 points in direction of mouse cursor - the vector B<br />
bx=MouseX()-ax<br />
by=MouseY()-ay<br />
EndIf</p>
<p>'press up arrow to increase distance, down to decrease distance<br />
d:+KeyDown(KEY_UP) - KeyDown(KEY_Down)<br />
If d&lt;0 Then d=0</p>
<p>'''' to find L</p>
<p>'first find n, a unit vector perpendicular to b<br />
Local nx#,ny#<br />
If by=0<br />
'if by = 0 then we can't divide by it but the perpendicular vector is easy - it's vertical!<br />
nx=0<br />
ny=-Sgn(bx)<br />
Else<br />
'clever trick to find perpendicular vector<br />
'the *sgn(by) bit is to make sure it's always on the right side of L1<br />
nx=1*Sgn(by)<br />
ny=-bx/by*Sgn(by)<br />
'it needs to be a unit vector, so divide it by its length to end up with length 1<br />
Local n#=Sqr(nx*nx+ny*ny)<br />
nx:/n<br />
ny:/n<br />
EndIf</p>
<p>'make n have the length we want to make the lines the right distance apart<br />
nx:*d<br />
ny:*d</p>
<p>'now the equivalent of point A on L is the point A+N, and the direction of L is the same as that of L - B<br />
cx = ax+nx<br />
cy = ay+ny</p>
<p>'draw L1<br />
SetColor 255,255,255<br />
DrawLine ax-bx*500,ay-by*500,ax+bx*500,ay+by*500<br />
DrawOval ax-4,ay-4,8,8<br />
SetColor 255,0,0<br />
DrawText "A",ax,ay</p>
<p>'draw B<br />
SetColor 0,0,255<br />
drawarrow ax,ay,bx,by<br />
DrawText "B",ax+bx/2,ay+by/2</p>
<p>'draw L<br />
SetColor 255,255,255<br />
DrawLine cx -bx*500, cy -by*500, cx +bx*500, cy +by*500<br />
DrawOval cx-4,cy-4,8,8<br />
SetColor 255,0,0<br />
DrawText "C",cx,cy</p>
<p>'draw N<br />
SetColor 0,0,255<br />
drawarrow ax,ay,nx,ny<br />
DrawText "N",ax+nx/2,ay+ny/2</p>
<p>Flip<br />
Cls<br />
Wend</p>
<p>Function drawarrow(ax,ay,vx#,vy#)<br />
DrawLine ax,ay,ax+vx,ay+vy</p>
<p>Local v#=Sqr(vx*vx+vy*vy)<br />
vx:/v<br />
vy:/v</p>
<p>'getting lazy, going to use trig<br />
Local an#=ATan2(vy,vx)<br />
Local px#=Cos(an+90),py#=Sin(an+90)</p>
<p></code></p>
<p><code> DrawLine ax+vx*v,ay+vy*v,ax+vx*(v-8)+px*8,ay+vy*(v-8)+py*8<br />
DrawLine ax+vx*v,ay+vy*v,ax+vx*(v-8)-px*8,ay+vy*(v-8)-py*8<br />
End Function<br />
</code></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpycode.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpycode.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpycode.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpycode.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/warpycode.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/warpycode.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/warpycode.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/warpycode.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpycode.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpycode.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpycode.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpycode.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpycode.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpycode.wordpress.com/161/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=161&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://warpycode.wordpress.com/2010/08/26/equation-of-a-line-perpendicular-to-another/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/872c129f4453b92c978756334c8a18ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">christianperfect</media:title>
		</media:content>
	</item>
		<item>
		<title>Aiming a projectile over a wall</title>
		<link>http://warpycode.wordpress.com/2010/03/08/aiming-a-projectile-over-a-wall/</link>
		<comments>http://warpycode.wordpress.com/2010/03/08/aiming-a-projectile-over-a-wall/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 22:31:17 +0000</pubDate>
		<dc:creator>christianperfect</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://warpycode.wordpress.com/?p=127</guid>
		<description><![CDATA[therevills asks on the blitzbasic.com forums: Does any one know how to do this? I know the following: * Start x,y * End x,y * Gravity * Height of the wall Diagrams: &#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..wall ____s______&#124;__________e_________ _s_&#124; &#8230;&#8230;&#124; &#8230;&#8230;&#124; &#8230;&#8230;&#124;_e_ etc Any ideas? What I&#8217;ll do is find the smallest possible solution to this problem &#8211; the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=127&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste">therevills asks on the<a href="http://www.blitzbasic.com/Community/posts.php?topic=89371"> blitzbasic.com forums</a>:</div>
<blockquote>
<div><span style="font-family:verdana;line-height:normal;font-size:12px;">Does any one know how to do this?</p>
<p>I know the following:</p>
<p>* Start x,y<br />
* End x,y<br />
* Gravity<br />
* Height of the wall</p>
<p>Diagrams:</p>
<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..wall<br />
____s______|__________e_________</p>
<p>_s_|<br />
&#8230;&#8230;|<br />
&#8230;&#8230;|<br />
&#8230;&#8230;|_e_</p>
<p>etc</p>
<p>Any ideas?</span></div>
</blockquote>
<div>What I&#8217;ll do is find the smallest possible solution to this problem &#8211; the angle and speed to fire the projectile such that it just skims the top of the wall before it hits the end point. I&#8217;ll also only consider the case where the end point is at the same height as the start point. You can use the ideas in <a href="http://warpycode.wordpress.com/2009/10/10/aiming-a-projectile-uphill/">my previous post</a> to sort out the other cases.</div>
<div>The relevant <a href="http://en.wikipedia.org/wiki/Equations_of_motion#Classic_version">equation of motion</a> is:</div>
<div id="_mcePaste"><img src='http://s0.wp.com/latex.php?latex=s+%3D+ut+%2B+%5Cfrac%7B1%7D%7B2%7Dat%5E2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='s = ut + &#92;frac{1}{2}at^2' title='s = ut + &#92;frac{1}{2}at^2' class='latex' /></div>
<div id="_mcePaste">where <img src='http://s0.wp.com/latex.php?latex=s&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='s' title='s' class='latex' /> is distance travelled, <img src='http://s0.wp.com/latex.php?latex=u&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='u' title='u' class='latex' /> is initial velocity, <img src='http://s0.wp.com/latex.php?latex=a&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a' title='a' class='latex' /> is acceleration, and <img src='http://s0.wp.com/latex.php?latex=t&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='t' title='t' class='latex' /> is time elapsed.</div>
<div>Suppose that the projectile reaches the wall at <img src='http://s0.wp.com/latex.php?latex=t_0&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='t_0' title='t_0' class='latex' /> and the end point at <img src='http://s0.wp.com/latex.php?latex=t_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='t_1' title='t_1' class='latex' />.</div>
<div id="_mcePaste">We&#8217;ll say that the distance from the start to the wall is <img src='http://s0.wp.com/latex.php?latex=x_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_1' title='x_1' class='latex' />, the distance to the end point is <img src='http://s0.wp.com/latex.php?latex=x_2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='x_2' title='x_2' class='latex' />, and the height of the wall is <img src='http://s0.wp.com/latex.php?latex=y&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y' title='y' class='latex' />.</div>
<div id="_mcePaste">We&#8217;ll also say that the speed of the projectile is <img src='http://s0.wp.com/latex.php?latex=V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V' title='V' class='latex' /> and the angle it is fired at is <img src='http://s0.wp.com/latex.php?latex=%5Ctheta&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;theta' title='&#92;theta' class='latex' />.</div>
<div id="_mcePaste"></div>
<div>So we want:</div>
<div id="_mcePaste"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brcl%7D+x_2+%26%3D%26+t_1+V+%5Ccos%28%5Ctheta%29+%5C%5C+0+%26%3D%26+t_1+V+%5Ccos%28%5Ctheta%29+%2B+%5Cfrac%7B1%7D%7B2%7Dgt_1%5E2+%5C%5C+x_1+%26%3D%26+t_0+V+%5Ccos%28%5Ctheta%29+%5C%5C+y+%26%3D%26+t_0+V+%5Csin%28%5Ctheta%29+%2B+%5Cfrac%7B1%7D%7B2%7D+g+t_0%5E2+%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rcl} x_2 &amp;=&amp; t_1 V &#92;cos(&#92;theta) &#92;&#92; 0 &amp;=&amp; t_1 V &#92;cos(&#92;theta) + &#92;frac{1}{2}gt_1^2 &#92;&#92; x_1 &amp;=&amp; t_0 V &#92;cos(&#92;theta) &#92;&#92; y &amp;=&amp; t_0 V &#92;sin(&#92;theta) + &#92;frac{1}{2} g t_0^2 &#92;end{array}' title='&#92;begin{array}{rcl} x_2 &amp;=&amp; t_1 V &#92;cos(&#92;theta) &#92;&#92; 0 &amp;=&amp; t_1 V &#92;cos(&#92;theta) + &#92;frac{1}{2}gt_1^2 &#92;&#92; x_1 &amp;=&amp; t_0 V &#92;cos(&#92;theta) &#92;&#92; y &amp;=&amp; t_0 V &#92;sin(&#92;theta) + &#92;frac{1}{2} g t_0^2 &#92;end{array}' class='latex' /></div>
<div></div>
<div>We can rearrange the first two equations (involving three unknowns, so we definitely need the other two!) to find <img src='http://s0.wp.com/latex.php?latex=v&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='v' title='v' class='latex' /> in terms of <img src='http://s0.wp.com/latex.php?latex=%5Ctheta&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;theta' title='&#92;theta' class='latex' />:</div>
<div></div>
<div><img src='http://s0.wp.com/latex.php?latex=t_1+%3D+%5Cfrac%7Bx_2%7D%7BV+%5Ccos%28%5Ctheta%29%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='t_1 = &#92;frac{x_2}{V &#92;cos(&#92;theta)} ' title='t_1 = &#92;frac{x_2}{V &#92;cos(&#92;theta)} ' class='latex' /></div>
<div></div>
<div><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7Bx_2%7D%7B%5Ccos%28%5Ctheta%29%7D+%5Csin%28%5Ctheta%29+%2B%5Cfrac%7B1%7D%7B2%7Dg%5Cfrac%7Bx_2%5E2%7D%7BV%5E2+%5Ccos%5E2+%28%5Ctheta%29%7D+%3D+0+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;frac{x_2}{&#92;cos(&#92;theta)} &#92;sin(&#92;theta) +&#92;frac{1}{2}g&#92;frac{x_2^2}{V^2 &#92;cos^2 (&#92;theta)} = 0 ' title='&#92;frac{x_2}{&#92;cos(&#92;theta)} &#92;sin(&#92;theta) +&#92;frac{1}{2}g&#92;frac{x_2^2}{V^2 &#92;cos^2 (&#92;theta)} = 0 ' class='latex' /></div>
<div></div>
<div><img src='http://s0.wp.com/latex.php?latex=V%5E2+%5Csin%28%5Ctheta%29+%5Ccos%28%5Ctheta%29+%3D+-+%5Cfrac%7B1%7D%7B2%7D+a+x_2+&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V^2 &#92;sin(&#92;theta) &#92;cos(&#92;theta) = - &#92;frac{1}{2} a x_2 ' title='V^2 &#92;sin(&#92;theta) &#92;cos(&#92;theta) = - &#92;frac{1}{2} a x_2 ' class='latex' /></div>
<div></div>
<div>Use the fact that <img src='http://s0.wp.com/latex.php?latex=%5Csin%282%5Ctheta%29+%3D+2%5Csin%28%5Ctheta%29%5Ccos%28%5Ctheta%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;sin(2&#92;theta) = 2&#92;sin(&#92;theta)&#92;cos(&#92;theta)' title='&#92;sin(2&#92;theta) = 2&#92;sin(&#92;theta)&#92;cos(&#92;theta)' class='latex' /> to get</div>
<div><img src='http://s0.wp.com/latex.php?latex=V%5E2+%3D+%5Cfrac%7B-ax_2%7D%7B%5Csin%282%5Ctheta%29%7D%2C&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V^2 = &#92;frac{-ax_2}{&#92;sin(2&#92;theta)},' title='V^2 = &#92;frac{-ax_2}{&#92;sin(2&#92;theta)},' class='latex' /></div>
<div>which will come in handy in a bit.</div>
<div></div>
<div>Doing the same kind of thing with the other two equations, we get</div>
<div><img src='http://s0.wp.com/latex.php?latex=%5Csin%282%5Ctheta%29+%2B+%5Cfrac%7Bax_1%7D%7BV%5E2%7D+%3D+2%5Cfrac%7By%7D%7Bx_1%7D%5Ccos%5E2%28%5Ctheta%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;sin(2&#92;theta) + &#92;frac{ax_1}{V^2} = 2&#92;frac{y}{x_1}&#92;cos^2(&#92;theta)' title='&#92;sin(2&#92;theta) + &#92;frac{ax_1}{V^2} = 2&#92;frac{y}{x_1}&#92;cos^2(&#92;theta)' class='latex' /></div>
<div>Substitute the thing we had for <img src='http://s0.wp.com/latex.php?latex=V%5E2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V^2' title='V^2' class='latex' /> to get</div>
<div><img src='http://s0.wp.com/latex.php?latex=%5Csin%282%5Ctheta%29+-+%5Cfrac%7Bx_1%5Csin%282%5Ctheta%29%7D%7Bx_2%7D+%3D+2%5Cfrac%7By%7D%7Bx_1%7D%5Ccos%5E2%28%5Ctheta%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;sin(2&#92;theta) - &#92;frac{x_1&#92;sin(2&#92;theta)}{x_2} = 2&#92;frac{y}{x_1}&#92;cos^2(&#92;theta)' title='&#92;sin(2&#92;theta) - &#92;frac{x_1&#92;sin(2&#92;theta)}{x_2} = 2&#92;frac{y}{x_1}&#92;cos^2(&#92;theta)' class='latex' /></div>
<div>Rearrange and use that trig fact again to get</div>
<div><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B2%5Ccos%28%5Ctheta%29%5Csin%28%5Ctheta%29%7D%7B2%5Ccos%5E2%28%5Ctheta%29%7D+%3D+%5Cfrac%7Byx_2%7D%7Bx_1x_2+-+x_1%5E2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;frac{2&#92;cos(&#92;theta)&#92;sin(&#92;theta)}{2&#92;cos^2(&#92;theta)} = &#92;frac{yx_2}{x_1x_2 - x_1^2}' title='&#92;frac{2&#92;cos(&#92;theta)&#92;sin(&#92;theta)}{2&#92;cos^2(&#92;theta)} = &#92;frac{yx_2}{x_1x_2 - x_1^2}' class='latex' /></div>
<div>Cancel down the left side and use <img src='http://s0.wp.com/latex.php?latex=%5Ctan%28%5Ctheta%29+%3D+%5Cfrac%7B%5Csin%28%5Ctheta%29%7D%7B%5Ccos%28%5Ctheta%29%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;tan(&#92;theta) = &#92;frac{&#92;sin(&#92;theta)}{&#92;cos(&#92;theta)}' title='&#92;tan(&#92;theta) = &#92;frac{&#92;sin(&#92;theta)}{&#92;cos(&#92;theta)}' class='latex' />:</div>
<div><img src='http://s0.wp.com/latex.php?latex=%5Ctan%28%5Ctheta%29+%3D+%5Cfrac%7Byx_2%7D%7Bx_1x_2+-+x_1%5E2%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;tan(&#92;theta) = &#92;frac{yx_2}{x_1x_2 - x_1^2}' title='&#92;tan(&#92;theta) = &#92;frac{yx_2}{x_1x_2 - x_1^2}' class='latex' /></div>
<div></div>
<div>Hooray, we&#8217;ve got a formula for <img src='http://s0.wp.com/latex.php?latex=%5Ctheta&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;theta' title='&#92;theta' class='latex' />! Once we&#8217;ve found that, we can put it back into the handy equation for <img src='http://s0.wp.com/latex.php?latex=V%5E2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V^2' title='V^2' class='latex' />, and we&#8217;ve got both the angle and speed of the projectile. Remember this give the minimum solution &#8211; you can increase <img src='http://s0.wp.com/latex.php?latex=V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V' title='V' class='latex' /> and rearrange the handy equation to get a suitable value for <img src='http://s0.wp.com/latex.php?latex=%5Ctheta&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;theta' title='&#92;theta' class='latex' />.</div>
<div></div>
<div>As always, here&#8217;s some BlitzMax code demonstrating this:</div>
<pre>Graphics 600,600,0
Global bullets:TList=New TList
Type bullet
	Field x#,y#
	Field vx#,vy#
	Field path#[]

	Function Create:bullet(x#,y#,vx#,vy#)
		b:bullet=New bullet
		bullets.addlast b
		b.x=x
		b.y=y
		b.vx=vx
		b.vy=vy
		Return b
	End Function

	Method update()
		path:+[x,y]

		x:+vx
		y:+vy
		vy:+g
		DrawRect x-5,y-5,10,10

		For i=0 To Len(path)-1 Step 2
			DrawRect path[i],path[i+1],1,1
		Next

		If x&gt;600 Or y&gt;600
			bullets.remove Self
		EndIf
	End Method
End Type

Const g#=1,x1#=300

While Not (KeyHit(KEY_ESCAPE) Or AppTerminate())
	x2#=MouseX()
	y#=MouseY()-300

	DrawLine 300,300,300,300+y
	DrawLine 0,300,x2,300

	If MouseHit(1)
		If x2&gt;x1 And y&lt;0
			theta# = ATan(-y*x2/(x2*x1-x1*x1))
			v=Sqr(g*x2/(Sin(2*theta)))
			Print theta
			Print v
			bullet.Create(0,300,v*Cos(-theta),v*Sin(-theta))
		EndIf
	EndIf

	For bu:bullet=EachIn bullets
		bu.update
	Next

	Flip
	Cls
Wend</pre>
<div></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpycode.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpycode.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpycode.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpycode.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/warpycode.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/warpycode.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/warpycode.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/warpycode.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpycode.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpycode.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpycode.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpycode.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpycode.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpycode.wordpress.com/127/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=127&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://warpycode.wordpress.com/2010/03/08/aiming-a-projectile-over-a-wall/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/872c129f4453b92c978756334c8a18ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">christianperfect</media:title>
		</media:content>
	</item>
		<item>
		<title>Aiming a projectile uphill</title>
		<link>http://warpycode.wordpress.com/2009/10/10/aiming-a-projectile-uphill/</link>
		<comments>http://warpycode.wordpress.com/2009/10/10/aiming-a-projectile-uphill/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 12:12:14 +0000</pubDate>
		<dc:creator>christianperfect</dc:creator>
				<category><![CDATA[Solutions]]></category>
		<category><![CDATA[aim]]></category>
		<category><![CDATA[aiming]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[motion]]></category>
		<category><![CDATA[projectile]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[trajectory]]></category>
		<category><![CDATA[trig]]></category>

		<guid isPermaLink="false">http://warpycode.wordpress.com/?p=112</guid>
		<description><![CDATA[Stevie G asks on the blitzbasic forums: My mind&#8217;s gone a blank so I&#8217;m looking for some help here. What I need is the angle of elevation a weapon needs to be at to launch a projectile and hit a target. The target can be higher or lower than the weapon. I&#8217;m working in 2d [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=112&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Stevie G asks on the<a href="http://www.blitzbasic.com/Community/posts.php?topic=87250"> blitzbasic forums</a>:</p>
<blockquote><p>My mind&#8217;s gone a blank so I&#8217;m looking for some help here. What I need is the angle of elevation a weapon needs to be at to launch a projectile and hit a target. The target can be higher or lower than the weapon. I&#8217;m working in 2d so have the following information available:</p>
<p>X0 = X coord of weapon<br />
Y0 = Y coord of weapon<br />
X1 = X coord of target<br />
Y1 = Y coord of target<br />
Speed = Launch Speed of projectile<br />
Gravity = -9.8</p>
<p>I&#8217;m not interested in including wind resistence etc..</p></blockquote>
<p>A Level maths to the rescue! Use one of the equations of motion:</p>
<p><img src='http://s0.wp.com/latex.php?latex=s+%3D+ut+%2B+%5Cfrac%7B1%7D%7B2%7Dat%5E2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='s = ut + &#92;frac{1}{2}at^2' title='s = ut + &#92;frac{1}{2}at^2' class='latex' /></p>
<p>Where <img src='http://s0.wp.com/latex.php?latex=s&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='s' title='s' class='latex' /> is the distance travelled, <img src='http://s0.wp.com/latex.php?latex=u&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='u' title='u' class='latex' /> is the initial velocity, <img src='http://s0.wp.com/latex.php?latex=a&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='a' title='a' class='latex' /> is the acceleration, and <img src='http://s0.wp.com/latex.php?latex=t&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='t' title='t' class='latex' /> is time elapsed. We want to find an angle <img src='http://s0.wp.com/latex.php?latex=%5Cphi&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;phi' title='&#92;phi' class='latex' /> to shoot the projectile at which will satisfy this equation.</p>
<p>Deal with the x- and y-components of motion separately. There&#8217;s no acceleration in the x-axis, so that equation will be easy to rearrange to get an expression for <img src='http://s0.wp.com/latex.php?latex=t&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='t' title='t' class='latex' />:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brcl%7Ds_x+%26%3D%26+x_1-x_0+%5C%5C+u_x+%26%3D%26+V+%5Ccos%28%5Cphi%29+%5C%5C+a_x+%26%3D%26+0+%5C%5C+%5C%5C+s_x+%26%3D%26+%28V+%5Ccos%28%5Cphi%29%29t+%5C%5C+t+%26%3D%26+%5Cfrac%7Bs_x%7D%7BV+%5Ccos%28%5Cphi%29%7D+%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rcl}s_x &amp;=&amp; x_1-x_0 &#92;&#92; u_x &amp;=&amp; V &#92;cos(&#92;phi) &#92;&#92; a_x &amp;=&amp; 0 &#92;&#92; &#92;&#92; s_x &amp;=&amp; (V &#92;cos(&#92;phi))t &#92;&#92; t &amp;=&amp; &#92;frac{s_x}{V &#92;cos(&#92;phi)} &#92;end{array}' title='&#92;begin{array}{rcl}s_x &amp;=&amp; x_1-x_0 &#92;&#92; u_x &amp;=&amp; V &#92;cos(&#92;phi) &#92;&#92; a_x &amp;=&amp; 0 &#92;&#92; &#92;&#92; s_x &amp;=&amp; (V &#92;cos(&#92;phi))t &#92;&#92; t &amp;=&amp; &#92;frac{s_x}{V &#92;cos(&#92;phi)} &#92;end{array}' class='latex' /></p>
<p>Now we can put that into the equation for the y-axis:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brcl%7Ds_y+%26%3D%26+y_1-y_0+%5C%5C+u_y+%26%3D%26+V+%5Csin%28%5Cphi%29+%5C%5C+a_y+%26%3D%26+g+%5C%5C+%5C%5C+s_y+%26%3D%26+V+%5Csin%28%5Cphi%29+%28%5Cfrac%7Bs_x%7D%7BV+%5Ccos%28%5Cphi%29%7D%29+%2B+%5Cfrac%7B1%7D%7B2%7Dg%28%5Cfrac%7Bs_x%5E2%7D%7BV%5E2%5Ccos%5E2%28%5Cphi%29%7D%29+%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rcl}s_y &amp;=&amp; y_1-y_0 &#92;&#92; u_y &amp;=&amp; V &#92;sin(&#92;phi) &#92;&#92; a_y &amp;=&amp; g &#92;&#92; &#92;&#92; s_y &amp;=&amp; V &#92;sin(&#92;phi) (&#92;frac{s_x}{V &#92;cos(&#92;phi)}) + &#92;frac{1}{2}g(&#92;frac{s_x^2}{V^2&#92;cos^2(&#92;phi)}) &#92;end{array}' title='&#92;begin{array}{rcl}s_y &amp;=&amp; y_1-y_0 &#92;&#92; u_y &amp;=&amp; V &#92;sin(&#92;phi) &#92;&#92; a_y &amp;=&amp; g &#92;&#92; &#92;&#92; s_y &amp;=&amp; V &#92;sin(&#92;phi) (&#92;frac{s_x}{V &#92;cos(&#92;phi)}) + &#92;frac{1}{2}g(&#92;frac{s_x^2}{V^2&#92;cos^2(&#92;phi)}) &#92;end{array}' class='latex' /></p>
<p>Now some clever cancelling and use of <a href="http://www.clarku.edu/~djoyce/trig/identities.html">trig identities</a>:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brcl%7D+s_x+%5Ctan%28%5Cphi%29+%2B+%5Cfrac%7Bgs_x%5E2%7D%7B2V%5E2%7D%5Csec%5E2%28%5Cphi%29+-+s_y+%26%3D%26+0+%5C%5C+%5Cfrac%7Bgs_x%5E2%7D%7B2V%5E2%7D%5Ctan%5E2%28%5Cphi%29+%2B+s_x%5Ctan%28%5Cphi%29+%2B+%28%5Cfrac%7Bgs_x%5E2%7D%7B2V%5E2%7D+-+s_y%29+%26%3D%26+0+%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rcl} s_x &#92;tan(&#92;phi) + &#92;frac{gs_x^2}{2V^2}&#92;sec^2(&#92;phi) - s_y &amp;=&amp; 0 &#92;&#92; &#92;frac{gs_x^2}{2V^2}&#92;tan^2(&#92;phi) + s_x&#92;tan(&#92;phi) + (&#92;frac{gs_x^2}{2V^2} - s_y) &amp;=&amp; 0 &#92;end{array}' title='&#92;begin{array}{rcl} s_x &#92;tan(&#92;phi) + &#92;frac{gs_x^2}{2V^2}&#92;sec^2(&#92;phi) - s_y &amp;=&amp; 0 &#92;&#92; &#92;frac{gs_x^2}{2V^2}&#92;tan^2(&#92;phi) + s_x&#92;tan(&#92;phi) + (&#92;frac{gs_x^2}{2V^2} - s_y) &amp;=&amp; 0 &#92;end{array}' class='latex' /></p>
<p>which, if you&#8217;re willing to believe it, is a quadratic equation in <img src='http://s0.wp.com/latex.php?latex=%5Ctan%28%5Cphi%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;tan(&#92;phi)' title='&#92;tan(&#92;phi)' class='latex' />. Use the <a href="http://en.wikipedia.org/wiki/Quadratic_equation#Quadratic_formula">quadratic formula</a> to find <img src='http://s0.wp.com/latex.php?latex=%5Ctan%28%5Cphi%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;tan(&#92;phi)' title='&#92;tan(&#92;phi)' class='latex' /> and hence <img src='http://s0.wp.com/latex.php?latex=%5Cphi&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;phi' title='&#92;phi' class='latex' />. Simples!</p>
<p>&#8230; that&#8217;s a lie, so here&#8217;s some bmax code:</p>
<pre>Graphics 600,600,0Graphics 600,600,0
Global bullets:TList=New TList
Type bullet
	Field x#,y#
	Field vx#,vy#
	Field path#[]

	Function Create:bullet(x#,y#,vx#,vy#)
		b:bullet=New bullet
		bullets.addlast b
		b.x=x
		b.y=y
		b.vx=vx
		b.vy=vy
		Return b
	End Function

	Method update()
		path:+[x,y]

		x:+vx
		y:+vy
		vy:+g
		DrawRect x-5,y-5,10,10

		For i=0 To Len(path)-1 Step 2
			DrawRect path[i],path[i+1],1,1
		Next

		If x&gt;600 Or y&gt;600
			bullets.remove Self
		EndIf
	End Method
End Type

'the setup is that you've got a cannon at (0,300), trying to fire at the mouse cursor.
'gravity is directed down the screen and the velocity of the projectiles is fixed at 20 px per frame.
'there will be some inaccuracy in the projectiles drawn on the screen because I'm using a discrete timestep model, due to I'm lazy.

Const g#=1,v#=20

While Not (KeyHit(KEY_ESCAPE) Or AppTerminate())
	sx#=MouseX()
	sy#=MouseY()-300

	a#=g*sx*sx/(2*v*v)	'coefficients of the quadratic equation
	b#=sx
	c#=a-sy

	If b*b&gt;4*a*c	'if solution exists

		t#=(-b+Sqr(b*b-4*a*c))/(2*a)		'this is tan(phi)

		an#=ATan(t)
		DrawLine 0,300,sx,sy+300

		If MouseHit(1)
			bullet.Create 0,300,v*Cos(an),v*Sin(an)
		EndIf

	Else
		DrawText "no solution!",0,0
	EndIf

	For bu:bullet=EachIn bullets
		bu.update
	Next

	Flip
	Cls
Wend</pre>
<p><span style="font-size:small;">Hope that&#8217;s useful!</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpycode.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpycode.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpycode.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpycode.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/warpycode.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/warpycode.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/warpycode.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/warpycode.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpycode.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpycode.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpycode.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpycode.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpycode.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpycode.wordpress.com/112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=112&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://warpycode.wordpress.com/2009/10/10/aiming-a-projectile-uphill/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/872c129f4453b92c978756334c8a18ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">christianperfect</media:title>
		</media:content>
	</item>
		<item>
		<title>Making things bounce</title>
		<link>http://warpycode.wordpress.com/2009/06/17/making-things-bounce/</link>
		<comments>http://warpycode.wordpress.com/2009/06/17/making-things-bounce/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 17:53:40 +0000</pubDate>
		<dc:creator>christianperfect</dc:creator>
				<category><![CDATA[Solutions]]></category>
		<category><![CDATA[2d]]></category>
		<category><![CDATA[bounce]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://warpycode.wordpress.com/?p=101</guid>
		<description><![CDATA[Local vx#,vy# &#8216;V, the velocity of the object before it hits the wall Local wallangle# &#8216;the angle of the wall &#8216;now work out the normal vector to the wall &#8211; it&#8217;s just at right angles Local nx#,ny# nx = Cos(wallangle + 90) ny = Sin(wallangle + 90) &#8216;p is the projection of V onto the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=101&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">Local vx#,vy#<span style="white-space:pre;"> </span>&#8216;V, the velocity of the object before it hits the wall</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">Local wallangle#<span style="white-space:pre;"> </span>&#8216;the angle of the wall</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">&#8216;now work out the normal vector to the wall &#8211; it&#8217;s just at right angles</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">Local nx#,ny#</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">nx = Cos(wallangle + 90)</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">ny = Sin(wallangle + 90)</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">&#8216;p is the projection of V onto the normal</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">Local px#,py#</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">Local dotproduct# = vx*nx+vy*ny</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">px = dotproduct*nx</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">py = dotproduct*ny</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">&#8216;the velocity after hitting the wall is V &#8211; 2p, so just subtract 2*p from V</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">vx = vx &#8211; 2*px</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">vy = vx &#8211; 2*py</div>
<div id="_mcePaste" style="position:absolute;left:-10000px;top:1602px;width:1px;height:1px;">&#8216;done!</div>
<p>Andres asked in <a href="http://www.blitzbasic.com/Community/posts.php?topic=85300">this post</a>:</p>
<blockquote><p>i&#8217;m having problems with a formula for my early stage game.</p>
<p>i want to calculate how the object bounces (the angle) from the wall.</p>
<p>i know the angle of the wall, object&#8217;s moving angle, the point where object and wall collide. If possible then the function or formula would work if the object comes from the other side of the wall too.</p></blockquote>
<p>Countless other people have asked this as well. It&#8217;s one of those things that comes up very often but is quite tricky to work out for yourself.</p>
<p>So, to start with, we&#8217;ve got something hitting a wall. We know its velocity before it hits the wall and we want to know what its velocity becomes after it hits the wall. <img class="aligncenter size-medium wp-image-102" title="reflection1" src="http://warpycode.files.wordpress.com/2009/06/reflection1.png?w=288&#038;h=300" alt="reflection1" width="288" height="300" /></p>
<p style="text-align:center;">
<p style="text-align:left;">When an object bounces off a wall, it travels at the same angle from the wall as it went in at, but the other way round. I stated that in terms of angles, but that&#8217;s tricky to get into code because, while people always know to write an angle as 90° anti-clockwise instead of 270° clockwise, and how to do stuff like reflections properly, it isn&#8217;t so simple when you&#8217;re just dealing with numbers in a computer. For this example, there are four choices of escape angle that you could produce if you&#8217;re not careful, but it&#8217;s hard to say which one is the correct one.</p>
<p style="text-align:left;">So instead, consider <img src='http://s0.wp.com/latex.php?latex=V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V' title='V' class='latex' /> as a cartesian vector &#8211; that is, an x-component and a y-component. We could also think of it as having a component moving parallel to the wall, and one moving perpendicular to it.</p>
<p style="text-align:left;"><img class="aligncenter size-medium wp-image-103" title="reflection2" src="http://warpycode.files.wordpress.com/2009/06/reflection2.png?w=288&#038;h=300" alt="reflection2" width="288" height="300" /></p>
<p style="text-align:center;">
<p style="text-align:left;">In the above image, <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> is the component of the vector which is perpendicular to the wall. If <img src='http://s0.wp.com/latex.php?latex=V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V' title='V' class='latex' /> didn&#8217;t bounce off the wall but instead kept travelling as it was before, travelling backwards along <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> from the end of <img src='http://s0.wp.com/latex.php?latex=V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V' title='V' class='latex' /> would get you back on the line:</p>
<p style="text-align:left;"><img class="aligncenter size-medium wp-image-104" title="reflection3" src="http://warpycode.files.wordpress.com/2009/06/reflection3.png?w=288&#038;h=300" alt="reflection3" width="288" height="300" /></p>
<p style="text-align:left;">And if you move back in the direction of <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> one more time, you&#8217;ll end up pointing exactly where you want to be: the angle of escape is the same as the angle of entry.</p>
<p style="text-align:left;"><img class="aligncenter size-medium wp-image-105" title="reflection4" src="http://warpycode.files.wordpress.com/2009/06/reflection4.png?w=288&#038;h=300" alt="reflection4" width="288" height="300" />So that&#8217;s the theory &#8211; we need to work out <img src='http://s0.wp.com/latex.php?latex=V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V' title='V' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' />, and the object&#8217;s velocity after hitting the wall will be <img src='http://s0.wp.com/latex.php?latex=V-2p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V-2p' title='V-2p' class='latex' />.</p>
<p style="text-align:left;"><img src='http://s0.wp.com/latex.php?latex=V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V' title='V' class='latex' /> is easy &#8211; you might already have it in vector form, or if not, you can use <img src='http://s0.wp.com/latex.php?latex=V_x+%3D+V%5Ccos+%5Ctheta&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V_x = V&#92;cos &#92;theta' title='V_x = V&#92;cos &#92;theta' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=V_y+%3D+V%5Csin+%5Ctheta&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V_y = V&#92;sin &#92;theta' title='V_y = V&#92;sin &#92;theta' class='latex' />. <img src='http://s0.wp.com/latex.php?latex=p&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='p' title='p' class='latex' /> is the projection of <img src='http://s0.wp.com/latex.php?latex=V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V' title='V' class='latex' /> onto the wall&#8217;s normal vector. (I&#8217;m planning on writing a post about the various vector operations later, but this one takes just a couple of lines of code to work out and considerably more writing to explain, so I won&#8217;t.)</p>
<p style="text-align:left;">And the great thing about this method is that you don&#8217;t need to check which side of the wall the object is hitting, or which way round the normal is pointing &#8211; it all works out correctly anyway.</p>
<pre>Local vx#,vy#		'V, the velocity of the object before it hits the wall
Local wallangle#	'the angle of the wall

'now work out the normal vector to the wall - it's just at right angles
Local nx#,ny#
nx = Cos(wallangle + 90)
ny = Sin(wallangle + 90)

'p is the projection of V onto the normal
Local px#,py#
Local dotproduct# = vx*nx+vy*ny
px = dotproduct*nx
py = dotproduct*ny

'the velocity after hitting the wall is V - 2p, so just subtract 2*p from V
vx = vx - 2*px
vy = vx - 2*py

'done!</pre>
<p>I hope that was helpful.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpycode.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpycode.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpycode.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpycode.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/warpycode.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/warpycode.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/warpycode.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/warpycode.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpycode.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpycode.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpycode.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpycode.wordpress.com/101/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpycode.wordpress.com/101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpycode.wordpress.com/101/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=101&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://warpycode.wordpress.com/2009/06/17/making-things-bounce/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/872c129f4453b92c978756334c8a18ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">christianperfect</media:title>
		</media:content>

		<media:content url="http://warpycode.files.wordpress.com/2009/06/reflection1.png?w=288" medium="image">
			<media:title type="html">reflection1</media:title>
		</media:content>

		<media:content url="http://warpycode.files.wordpress.com/2009/06/reflection2.png?w=288" medium="image">
			<media:title type="html">reflection2</media:title>
		</media:content>

		<media:content url="http://warpycode.files.wordpress.com/2009/06/reflection3.png?w=288" medium="image">
			<media:title type="html">reflection3</media:title>
		</media:content>

		<media:content url="http://warpycode.files.wordpress.com/2009/06/reflection4.png?w=288" medium="image">
			<media:title type="html">reflection4</media:title>
		</media:content>
	</item>
		<item>
		<title>Computing a 1d Gaussian Kernel</title>
		<link>http://warpycode.wordpress.com/2009/04/13/computing-a-1d-gaussian-kernel/</link>
		<comments>http://warpycode.wordpress.com/2009/04/13/computing-a-1d-gaussian-kernel/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 07:49:21 +0000</pubDate>
		<dc:creator>christianperfect</dc:creator>
				<category><![CDATA[Solutions]]></category>
		<category><![CDATA[gaussian]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[shader]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://warpycode.wordpress.com/?p=97</guid>
		<description><![CDATA[In this post, Gabriel asks: I&#8217;m trying to tweak my blur shaders a bit, and I decided to try a gaussian blur. I separate the blur into two passes which means I can get a 9&#215;9 kernel with 18 samples instead of 81, and it also means I need a 1d kernel. The only algorithm [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=97&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.blitzbasic.com/Community/posts.php?topic=84166">this post</a>, Gabriel asks:</p>
<blockquote><p>I&#8217;m trying to tweak my blur shaders a bit, and I decided to try a gaussian blur. I separate the blur into two passes which means I can get a 9&#215;9 kernel with 18 samples instead of 81, and it also means I need a 1d kernel. The only algorithm I managed to find was for a 2d kernel, and had a couple of symbols I didn&#8217;t recognize anyway. (Always my problem.)</p>
<p>Could anyone point me to some code to calculate a 1D gaussian kernel of a variable size? As I say, I&#8217;m using a size of 9 right now, but that might change so I had better find the code rather than just a kernel someone created for me.</p></blockquote>
<p>From a quick google for &#8220;gaussian kernel&#8221;, <a href="http://en.wikipedia.org/wiki/Gaussian_blur" target="_blank">here&#8217;s the wikipedia page</a> which has the 1d and 2d equations on it, and <a href="http://www.stat.wisc.edu/%7Emchung/teaching/MIA/reading/diffusion.gaussian.kernel.pdf.pdf" target="_blank">here&#8217;s a pdf</a> which is very interesting to me and explains the maths behind it.</p>
<p>So, it&#8217;s just a matter of writing out the equation. The only bit of trouble I had was with picking the right sigma value, but the wikipedia page says</p>
<blockquote>
<div class="quote">In practice, when computing a discrete approximation of the Gaussian function, pixels at a distance of more than 3*sigma are small enough to be considered effectively zero.</div>
</blockquote>
<p>so that&#8217;s that solved. Finally, you need to rescale the values in the kernel a bit because there&#8217;s a slight error introduced by picking discrete values of the gaussian function, which is really a continuous integral.</p>
<pre>Function GaussianKernel#[](radius#)
	width=Ceil(radius)	'kernel will have a middle cell, and width on either side
	Local matrix#[width*2+1]
	sigma# = radius/3		'apparently this is all you need to get a good approximation
	norm# = 1.0 / (Sqr(2*Pi) * sigma)		'normalisation constant makes sure total of matrix is 1
	coeff# = 2*sigma*sigma	'the bit you divide x^2 by in the exponential
	total#=0
	For x = -width To width	'fill in matrix!
		g# = norm * Exp( -x*x/coeff )
		matrix[x+width] = g
		total:+g
	Next
	For x=0 To 2*width	'rescale things to get a total of 1, because of discretisation error
		matrix[x]:/total
	Next
	Return matrix
End Function</pre>
<p>By the way, for anyone else who&#8217;s wondering, the way to split the blur process into two passes is to apply the 1d kernel horizontally in the first pass, and then vertically in the second pass. This works because the gaussian function is linearly separable.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpycode.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpycode.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpycode.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpycode.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/warpycode.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/warpycode.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/warpycode.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/warpycode.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpycode.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpycode.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpycode.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpycode.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpycode.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpycode.wordpress.com/97/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=97&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://warpycode.wordpress.com/2009/04/13/computing-a-1d-gaussian-kernel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/872c129f4453b92c978756334c8a18ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">christianperfect</media:title>
		</media:content>
	</item>
		<item>
		<title>A triangle puzzle</title>
		<link>http://warpycode.wordpress.com/2009/03/30/a-triangle-puzzle/</link>
		<comments>http://warpycode.wordpress.com/2009/03/30/a-triangle-puzzle/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 22:11:30 +0000</pubDate>
		<dc:creator>christianperfect</dc:creator>
				<category><![CDATA[Solutions]]></category>
		<category><![CDATA[2d]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[triangle]]></category>
		<category><![CDATA[trig]]></category>

		<guid isPermaLink="false">http://warpycode.wordpress.com/?p=90</guid>
		<description><![CDATA[Jussi writes, Given the lengths of c1 and c2 and angle alpha, what&#8217;s the simplest and fastest way to calculate the length of d (see the attached image)? Looks tricky at first, but trying to do it the boring way ended up giving me a nice answer. What we want is to have the line [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=90&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Jussi writes,</p>
<blockquote><p>Given the lengths of <strong>c1</strong> and <strong>c2</strong> and angle<strong> alpha</strong>, what&#8217;s the simplest and fastest way to calculate the length of <strong>d</strong> (see the attached image)?</p>
<p><img class="alignnone size-full wp-image-89" title="vector1" src="http://warpycode.files.wordpress.com/2009/03/vector1.gif?w=500" alt="vector1"   /></p></blockquote>
<p>Looks tricky at first, but trying to do it the boring way ended up giving me a nice answer.</p>
<p>What we want is to have the line with angle alpha intersect the hypotenuse of the triangle. So, let&#8217;s write down the equations of the two lines:</p>
<p>The hypotenuse can be written using the normal straight line formula:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=y+%3D+c_1+-+%5Cfrac%7Bc_1%7D%7Bc_2%7Dx&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='y = c_1 - &#92;frac{c_1}{c_2}x' title='y = c_1 - &#92;frac{c_1}{c_2}x' class='latex' /></p>
<p style="text-align:left;">While the other line is in polar co-ordinates because we only know the angle alpha:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brcl%7D+x+%26%3D%26+d+%5Ccos+%5Calpha+%5C%5C+y+%26%3D%26+d+%5Csin+%5Calpha+%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rcl} x &amp;=&amp; d &#92;cos &#92;alpha &#92;&#92; y &amp;=&amp; d &#92;sin &#92;alpha &#92;end{array}' title='&#92;begin{array}{rcl} x &amp;=&amp; d &#92;cos &#92;alpha &#92;&#92; y &amp;=&amp; d &#92;sin &#92;alpha &#92;end{array}' class='latex' /></p>
<p style="text-align:left;">Substitute those into the first equation:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=d+%5Csin+%5Calpha+%3D+c_1+-+%5Cfrac%7Bc_1%7D%7Bc_2%7Dd+%5Ccos+%5Calpha&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='d &#92;sin &#92;alpha = c_1 - &#92;frac{c_1}{c_2}d &#92;cos &#92;alpha' title='d &#92;sin &#92;alpha = c_1 - &#92;frac{c_1}{c_2}d &#92;cos &#92;alpha' class='latex' /></p>
<p style="text-align:left;">We want to find d, so collect all the terms with d together:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=d%28+%5Csin+%5Calpha+%2B+%5Cfrac%7Bc_1%7D%7Bc_2%7D%29+%3D+c_1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='d( &#92;sin &#92;alpha + &#92;frac{c_1}{c_2}) = c_1' title='d( &#92;sin &#92;alpha + &#92;frac{c_1}{c_2}) = c_1' class='latex' /></p>
<p style="text-align:left;">And finally divide by the stuff in the brackets:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=d+%3D+%5Cfrac%7Bc_1%7D%7B%5Csin+%5Calpha+%2B+%5Cfrac%7Bc_1%7D%7Bc_2%7D+%5Ccos+%5Calpha%7D+&amp;bg=ffffff&amp;fg=333333&amp;s=3' alt='d = &#92;frac{c_1}{&#92;sin &#92;alpha + &#92;frac{c_1}{c_2} &#92;cos &#92;alpha} ' title='d = &#92;frac{c_1}{&#92;sin &#92;alpha + &#92;frac{c_1}{c_2} &#92;cos &#92;alpha} ' class='latex' /></p>
<p style="text-align:left;">Now let&#8217;s check that it works with some bmax code:</p>
<pre style="text-align:left;">Graphics 600,600,0

c1#=300
c2#=400

an#=0

While Not (KeyHit(KEY_ESCAPE) Or AppTerminate())
	x#=300-c2/2
	y#=300-c1/2
	DrawLine x,y,x+c2,y
	DrawLine x,y,x,y+c1
	DrawLine x+c2,y,x,y+c1

	an = (an+1) Mod 90

	t#=an/90
	r#=c1/(Sin(an)+(c1/c2)*Cos(an))
	DrawLine x,y,x+Cos(an)*r,y+Sin(an)*r

	Flip
	Cls
Wend</pre>
<p>Yep!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpycode.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpycode.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpycode.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpycode.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/warpycode.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/warpycode.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/warpycode.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/warpycode.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpycode.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpycode.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpycode.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpycode.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpycode.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpycode.wordpress.com/90/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=90&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://warpycode.wordpress.com/2009/03/30/a-triangle-puzzle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/872c129f4453b92c978756334c8a18ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">christianperfect</media:title>
		</media:content>

		<media:content url="http://warpycode.files.wordpress.com/2009/03/vector1.gif" medium="image">
			<media:title type="html">vector1</media:title>
		</media:content>
	</item>
		<item>
		<title>Aiming accurately at moving targets</title>
		<link>http://warpycode.wordpress.com/2009/03/30/aiming-accurately-at-moving-targets/</link>
		<comments>http://warpycode.wordpress.com/2009/03/30/aiming-accurately-at-moving-targets/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 11:04:08 +0000</pubDate>
		<dc:creator>christianperfect</dc:creator>
				<category><![CDATA[Solutions]]></category>
		<category><![CDATA[2d]]></category>
		<category><![CDATA[aiming]]></category>
		<category><![CDATA[quadratic]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://warpycode.wordpress.com/?p=73</guid>
		<description><![CDATA[In this post, SpaceTW asked: Hi, I need help aiming at moving targets for a program. I have the variables for the speed and direction of the enemies, but the targeting is still not accurate enough. There&#8217;s a very simple solution if you don&#8217;t mind the bullets travelling at different speeds, but that can lead [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=73&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.blitzbasic.com/Community/posts.php?topic=83782#945737">this post</a>, SpaceTW asked:</p>
<blockquote><p>Hi, I need help aiming at moving targets for a program. I have the variables for the speed and direction of the enemies, but the targeting is still not accurate enough.</p></blockquote>
<p>There&#8217;s a very simple solution if you don&#8217;t mind the bullets travelling at different speeds, but that can lead to bullets travelling very fast or very slow, so let&#8217;s say that we want every bullet to go the same speed. Here&#8217;s the solution:</p>
<p>Suppose you have a tower at a fixed position <img src='http://s0.wp.com/latex.php?latex=%28x_0%2Cy_0%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(x_0,y_0)' title='(x_0,y_0)' class='latex' />, and an enemy at initial position <img src='http://s0.wp.com/latex.php?latex=%28x_1%2Cy_1%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(x_1,y_1)' title='(x_1,y_1)' class='latex' /> moving in the direction <img src='http://s0.wp.com/latex.php?latex=%28v_x%2Cv_y%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(v_x,v_y)' title='(v_x,v_y)' class='latex' />.</p>
<p>You want to shoot a bullet travelling at a certain velocity <img src='http://s0.wp.com/latex.php?latex=V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V' title='V' class='latex' /> so that it collides with the enemy.</p>
<p>Let the direction of the bullet be <img src='http://s0.wp.com/latex.php?latex=%28d_x%2Cd_y%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(d_x,d_y)' title='(d_x,d_y)' class='latex' />.</p>
<p>If <img src='http://s0.wp.com/latex.php?latex=d_x%3Dv_x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='d_x=v_x' title='d_x=v_x' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=d_y%3Dv_y&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='d_y=v_y' title='d_y=v_y' class='latex' />, then the bullet will move in parallel with the enemy, always staying the same distance from it.</p>
<p>You can now consider that the enemy is not moving at all, relative to the bullet, and the difference in position between them is <img src='http://s0.wp.com/latex.php?latex=%28x_1-x_0%2C+y_1-y_0%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='(x_1-x_0, y_1-y_0)' title='(x_1-x_0, y_1-y_0)' class='latex' />. The bullet can move along this line to collide with the bullet.</p>
<p>If the bullet takes <img src='http://s0.wp.com/latex.php?latex=t&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='t' title='t' class='latex' /> frames to reach the bullet, we now get:</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brcl%7Dd_x+%3D+%28x_1-x_0%29%2Ft+%2B+v_x+%5C%5C+d_y+%3D+%28y_1-y_0%29%2Ft+%2B+v_y+%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rcl}d_x = (x_1-x_0)/t + v_x &#92;&#92; d_y = (y_1-y_0)/t + v_y &#92;end{array}' title='&#92;begin{array}{rcl}d_x = (x_1-x_0)/t + v_x &#92;&#92; d_y = (y_1-y_0)/t + v_y &#92;end{array}' class='latex' /></p>
<p>As we said that the bullet&#8217;s velocity is <img src='http://s0.wp.com/latex.php?latex=V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='V' title='V' class='latex' />, we get</p>
<p style="text-align:center;"><img src='http://s0.wp.com/latex.php?latex=d_x%5E2+%2B+d_y%5E2+%3D+V%5E2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='d_x^2 + d_y^2 = V^2' title='d_x^2 + d_y^2 = V^2' class='latex' /></p>
<p>Which expands out to:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brcl%7D+%5Cleft%28%5Cfrac%7B1%7D%7Bt%7D%5Cright%29%5E2+%5Cleft%5B+%28x_1+-+x_0%29%5E2+%2B+%28y_1+-+y_0%29%5E2+%5Cright%5D+%26%26+%5C%5C+%2B+%5Cfrac%7B1%7D%7Bt%7D+%5Cleft%5B+v_x+%28x_1+-+x_0%29+%2B+v_y+%28+y_1+-+y_0%29+%5Cright%5D+%26%26%5C%5C+%2B+v_x%5E2+%2B+v_y%5E2+-+V%5E2+%26%3D%26+0+%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rcl} &#92;left(&#92;frac{1}{t}&#92;right)^2 &#92;left[ (x_1 - x_0)^2 + (y_1 - y_0)^2 &#92;right] &amp;&amp; &#92;&#92; + &#92;frac{1}{t} &#92;left[ v_x (x_1 - x_0) + v_y ( y_1 - y_0) &#92;right] &amp;&amp;&#92;&#92; + v_x^2 + v_y^2 - V^2 &amp;=&amp; 0 &#92;end{array}' title='&#92;begin{array}{rcl} &#92;left(&#92;frac{1}{t}&#92;right)^2 &#92;left[ (x_1 - x_0)^2 + (y_1 - y_0)^2 &#92;right] &amp;&amp; &#92;&#92; + &#92;frac{1}{t} &#92;left[ v_x (x_1 - x_0) + v_y ( y_1 - y_0) &#92;right] &amp;&amp;&#92;&#92; + v_x^2 + v_y^2 - V^2 &amp;=&amp; 0 &#92;end{array}' class='latex' /></p>
<p>(I had to split that over a few lines so it would fit in the post column, sorry!)</p>
<p>Which is a quadratic equation! So set:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cbegin%7Barray%7D%7Brcl%7D+a+%26%3D%26+%28x_1-x_0%29%5E2+%2B+%28y_1-y_0%29%5E2+%5C%5C+b+%26%3D%26++2%5B+v_x%28x_1-x_0%29+%2B+v_y%28y_1-y_0%29+%5D+%5C%5C+c+%26%3D%26++v_x%5E2+%2B+v_y%5E2+-+V%5E2+%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{rcl} a &amp;=&amp; (x_1-x_0)^2 + (y_1-y_0)^2 &#92;&#92; b &amp;=&amp;  2[ v_x(x_1-x_0) + v_y(y_1-y_0) ] &#92;&#92; c &amp;=&amp;  v_x^2 + v_y^2 - V^2 &#92;end{array}' title='&#92;begin{array}{rcl} a &amp;=&amp; (x_1-x_0)^2 + (y_1-y_0)^2 &#92;&#92; b &amp;=&amp;  2[ v_x(x_1-x_0) + v_y(y_1-y_0) ] &#92;&#92; c &amp;=&amp;  v_x^2 + v_y^2 - V^2 &#92;end{array}' class='latex' /></p>
<p>Then, using the <a href="http://en.wikipedia.org/wiki/Quadratic_equation#Quadratic_formula">quadratic formula</a>:</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7Bt%7D+%3D+%5Cfrac%7B-b+%2B+%5Csqrt%7B+b%5E2+-+4ac%7D%7D%7B2a%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;frac{1}{t} = &#92;frac{-b + &#92;sqrt{ b^2 - 4ac}}{2a}' title='&#92;frac{1}{t} = &#92;frac{-b + &#92;sqrt{ b^2 - 4ac}}{2a}' class='latex' /></p>
<p>You can now put that back into the equations for <img src='http://s0.wp.com/latex.php?latex=d_x&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='d_x' title='d_x' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=d_y&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='d_y' title='d_y' class='latex' />, and you&#8217;re done!</p>
<p>Here&#8217;s the result, written in BlitzMax:</p>
<pre class="code">diffx:double=x2-x1
diffy:double=y2-y1
v:Double=3                'desired speed of bullet

a:Double=diffx*diffx+diffy*diffy
b:Double=2*(vx*diffx+vy*diffy)
c:Double=vx*vx+vy*vy-v*v

tInv#=(-b+Sqr(b*b-4*a*c))/(2*a)   ' solve 1/t
dx=diffx*tInv+vx
dy=diffy*tInv+vy</pre>
<p>That&#8217;s it!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpycode.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpycode.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpycode.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpycode.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/warpycode.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/warpycode.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/warpycode.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/warpycode.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpycode.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpycode.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpycode.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpycode.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpycode.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpycode.wordpress.com/73/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpycode.wordpress.com&amp;blog=7138438&amp;post=73&amp;subd=warpycode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://warpycode.wordpress.com/2009/03/30/aiming-accurately-at-moving-targets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/872c129f4453b92c978756334c8a18ef?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">christianperfect</media:title>
		</media:content>
	</item>
	</channel>
</rss>
