tag:blogger.com,1999:blog-66588440846247001422017-08-15T17:54:04.830-07:00code and composeTrevor Gastnoreply@blogger.comBlogger14125tag:blogger.com,1999:blog-6658844084624700142.post-4139252524723236432017-02-25T00:58:00.001-08:002017-02-25T00:59:39.254-08:00Ready. Aim. FIRE!If you've ever programmed a game, then you know that physics is critical to realistic play.<br /><br />No, I am not going to explain how to build a physics engine. I just started with the basics.<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://games.codeandcompose.com/trajectory.html" target="_blank"><img border="0" height="213" src="https://1.bp.blogspot.com/-8_NRLBKrFIA/WLFGHVkh7ZI/AAAAAAAAAtE/28W734LQQQc_K5noPxgp8dFSOOfW09VagCLcB/s320/Screen%2BShot%2B2017-02-15%2Bat%2B13.19.38.png" width="320" /></a></div><h4>How to Launch Things</h4><div>Whether it is catapults, tanks or worms with slingshots, the most important thing is: whatever goes up must come down... specifically in a parabolic trajectory.</div><div><br /></div><div>Now, computers are smart, but they're also dumb. It would be nice if we could just tell it a speed and an angle and have it determine everything else. OR give it an x,y coordinate and have it calculate the speed and angle. But computers don't solve algebra equations unless we tell them how. They just plug-n-chug input and give output.</div><div><br /></div><div>That means we have to do the algebra ourselves. *Sigh*</div><div><br /></div><div>Programming basic kinematics is the easy part. We provide the initial position, speed, angle and time since "launch" and we can determine the speed, direction and position. Do this 60 times a second and we can animate it. If you need a refresher on the <a href="https://www.khanacademy.org/science/physics/one-dimensional-motion/kinematic-formulas/a/what-are-the-kinematic-formulas" target="_blank">big four, I recommend Khan Academy</a>.</div><div><br /></div><div>The only real challenge making this app was solving the algebra to adjust the angle of launch based on a selected (arbitrary) target. If you plug the final position into the big four and solve for the angle, you get something pretty ugly. A few trigonometric identities and a quadratic formula later we get this code:</div><div><pre class="prettyprint">//global declarations<br /> var g = -9.80665;<br /> var time = 0; //time<br /> var v0 = 10;//initial velocity (muzzle)<br /> var theta = Math.PI/4;//trajectory angle<br /> var x0 = 0;//initial position<br /> var y0 = 0;<br /> var target = {"X":0,"y":0,"active":false};<br /><br />//x and y are the coordinates that were clicked by the user<br />function defineTrajectory(x,y){<br /> tanTheta1 = quadraticFormula(g*x*x/(2*v0*v0),x,-(y-y0)+(g*x*x/(2*v0*v0)),1);<br /> tanTheta2 = quadraticFormula(g*x*x/(2*v0*v0),x,-(y-y0)+(g*x*x/(2*v0*v0)),-1);<br /> //two solutions, so we pick positive first<br /> if (tanTheta1 > 0) theta = Math.atan(tanTheta1);<br /> else theta = Math.atan(tanTheta2);<br /> //this updates the visual layout to reflect the angle<br /> $('#angle').val(theta*180/Math.PI);<br /> //this tells us where to draw the big red target<br /> target.x = x;<br /> target.y = y;<br /> target.active = true;<br /> //then we calculate the rest of the trajectory<br /> //using the big four and then draw the scene<br /> calcValues();<br /> drawScene();<br /> drawTrajectory();<br />}<br />function quadraticFormula(a,b,c,sign){<br /> if (b*b-(4*a*c) < 0) return false;//imaginary solution<br /> return (-b+(sign*Math.sqrt(b*b-(4*a*c))))/(2*a);<br />}</pre><br /></div><div>What happens if there isn't enough speed to reach the selected point, no matter the angle? I'll leave that for someone else to decide. On to the next project! Orbital trajectories!</div>Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-47341897759773098262016-10-03T03:58:00.001-07:002016-10-03T03:58:48.435-07:00What's your vector, Victor?If you have studied physics in 3 dimensions, then you know what a pain it is to do vector math. It's abstract, it's tricky to draw on paper...<br /><div><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://games.codeandcompose.com/3dvmv/" target="_blank"><img border="0" height="267" src="https://1.bp.blogspot.com/-wgTrs6y9s1Y/V_I5cT_gsHI/AAAAAAAAArk/K82sXZifVKwKGnYsRZEkNVjVsTYOpDFYgCLcB/s400/Screen%2BShot%2B2016-10-03%2Bat%2B12.56.24.png" width="400" /></a></div><br />So I devised a <a href="http://games.codeandcompose.com/3dvmv/">little aid</a>. It's much easier to visualize the sum of Force and Moment (torque) vectors when you can actually turn it around, and compare at different angles.</div><div><br /></div><div>With a little help from WebGL, it wasn't too hard to draw some axes, planes and arrows (although adding text was <a href="http://webglfundamentals.org/webgl/lessons/webgl-text-texture.html" target="_blank">a bit trickier</a>).<br /><br />This little script lets you create up to 6 forces (or moments) and it will sum them into a resultant force and moment. If the resultants are 0, then the forces are in equilibrium. This is very handy if you need to check your homework, of course. However, it won't solve it for you... unless you are looking for a single force to cancel the existing ones!<br /><br />Features so far:<br /><ul><li>add up to 6 colored vectors</li><li>rotate the graph 90 degrees in each direction (click the graph once to unlock, again to lock)</li><li>scale the graph</li><li>calculates resultant force and moment vector displayed in white</li></ul><div>Planned features:</div></div><div><ul><li>adding larger number of vector inputs</li><li>animated vectors</li><li>unit adjustments, degrees and radians</li><li>vector solver...? (to determine unknown value of a vector with known direction)</li></ul><div>The app currently runs on glMatrix 0.9.5. Next version will likely be updated to 2.0 (more info on http://glmatrix.net). It is a nice library already optimized for use with WebGL.</div></div><div><br /></div><div>Enjoy the app. I use it to check my homework...</div><div><br /></div><div>Feel free to fork <a href="https://github.com/Shytor/3dvmv" target="_blank">my repo on GitHub</a> and let me know if you have any feature suggestions or bugs!</div>Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-34497089464929230762016-05-05T11:03:00.003-07:002016-05-05T11:03:51.767-07:00A Random PostIt's been awhile so I figured: it is time for a random post. Seriously, this is a post about random numbers.<br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-JtRHRPpYaEQ/VyuK9HaFk5I/AAAAAAAAAkw/UFtdcjYoVhw-FAplrvxDkaY1bj9UcG0oQCLcB/s1600/rolling-dice-gif-3.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://2.bp.blogspot.com/-JtRHRPpYaEQ/VyuK9HaFk5I/AAAAAAAAAkw/UFtdcjYoVhw-FAplrvxDkaY1bj9UcG0oQCLcB/s1600/rolling-dice-gif-3.gif" /></a></div><br />If you code in Javascript, then Math.random() is probably already familiar to you. It does a nice job of generating (pretty) uniform variables from 0 to 1. A little multiplying and rounding and you can get integers from 0 to x, or -x to x, or whatever your needs.<br /><br />But what if you want NON-uniform randoms? For statistics freaks: what if you need normal distribution? or Gamma? or Beta? Exponential? Chi-squared?<br /><br />Well you are in luck! I found myself in need of some common distributions for a little side game I'm making, and went the extra mile to read some thesis papers about how to generate these useful distributions.<br /><br />Rather than bore you with how it all works, here is the code for a RANDOM object you can insert into your code. I'm still working on Betas, they are bit trickier.<br /><h4><span style="font-family: "courier new" , "courier" , monospace;">RANDOM.norm(n) </span></h4><span style="font-family: inherit;">This returns an (approximately) normal bell curve centered on mean 0.5. The parameter "n" can be between 2 and 5 (default 2). Higher values of "n" makes the bell curve more narrow. The value returned is between 0 and 1.</span><br /><h4><span style="font-family: "courier new" , "courier" , monospace;">RANDOM.inorm(n) </span></h4><span style="font-family: inherit;">This returns an inverse bell curve centered on mean 0.5. The parameter "n" can be between 2 and 5 (default 2). Higher n makes output more likely to be near 0 or 1.</span><br /><h4><span style="font-family: "courier new" , "courier" , monospace;">RANDOM.exp(n) </span></h4><span style="font-family: inherit;">A nice easy exponential variable from 0 to 1. Increasing "n" makes the graph skewed towards 0. Default value for "n" is 2.</span><br /><h4><span style="font-family: "courier new" , "courier" , monospace;">RANDOM.invexp(n) </span></h4><span style="font-family: inherit;">This is just RANDOM.exp(n) times -1.</span><br /><h4><span style="font-family: "courier new" , "courier" , monospace;">RANDOM.gamma(alpha,beta) </span></h4><span style="font-family: inherit;">This is a conglomeration of a few generators. For alpha > 1 and < 0, it uses an algorithm <a href="http://home.iitk.ac.in/~kundu/paper120.pdf" target="_blank">developed by Kundu and Gupta</a>. No beta is used for this algorithm.</span><br /><br /><span style="font-family: inherit;">For alpha >= 1 it uses the <a href="http://arxiv.org/pdf/1304.3800.pdf" target="_blank">algorithms proposed by Martino and Luengo</a></span>. It generates very quick results due to a low rejection rate. Default values are 1 and 1.<br /><br />I could not have completed this without those great algorithms! If you use this code, please leave the credits they deserve.<br /><br /><b>TIP:</b> RANDOM.gamma(2,2) returns a very nice gamma distribution. Be aware: the values returned may be higher than 1 (especially for high alpha and beta values). Alpha values below 1 resemble exponential distributions.<br /><h4><span style="font-family: "courier new" , "courier" , monospace;">RANDOM.igamma(alpha,beta) </span></h4><span style="font-family: inherit;">Simply RANDOM.gamma() times -1.</span><br /><h4><span style="font-family: "courier new" , "courier" , monospace;">RANDOM.chi2(k) </span></h4><span style="font-family: inherit;">A commonly used distribution resembling gamma distributions (I use this one a lot). Again, returned values may be higher than 1, especially for high "k".</span><br /><h4><span style="font-family: "courier new" , "courier" , monospace;">RANDOM.coinFlip(weight) </span></h4><span style="font-family: inherit;">A handy weighted coin flip that returns 0 or 1. Default weight is 2 (50/50) and MUST be > 1. Weights over 2 favor a positive outcome (i.e. weight of 3 will have 2:3 odds of returning 1).</span><br /><h3><span style="font-family: inherit;">The code:</span></h3><span style="font-family: inherit;">As usual, the code is free to use under the GNU. Please give credit though, and I'd <b>love</b> to hear how this has been useful for anyone! Also, feel free to play around with the <a href="http://jsfiddle.net/pb086em6/5/" target="_blank">fiddle</a>! (The code is adjusted slightly for display).</span><br /><span style="font-family: inherit;"><br /></span><span style="font-family: inherit;">I'll post a follow-up if <span style="font-family: inherit;">I <span style="font-family: inherit;">ever get around to adding Beta randoms too!</span></span></span><br /><br /><pre class="prettyprint">var RANDOM = { //returns random between 0 and 1, normal distribution centered on 0.5<br /> "norm": function(n) {<br /> if (!(n > 2 && n <= 5)) n = 2;<br /> var nrand = 0;<br /> n = Math.floor(n);<br /> for (var i = 1;i<=n*2;i++){<br /> nrand += Math.random();<br /> }<br /> return nrand/(2*n);<br /> },<br /> "inorm": function(n) { //returns random between 0 and 1<br /> if (!(n > 2 && n <= 5)) n = 2;<br /> var nrand = 0;<br /> n = Math.floor(n);<br /> for (var i = 1;i<=n*2;i++){<br /> nrand += Math.random();<br /> }<br /> return ((1 - Math.abs((nrand-n) / n))*(Math.abs(nrand-n)/(nrand-n)) + 1)/2;<br /> },<br /> "exp": function(n) { //returns greater than 0<br /> if (!(n > 2 && n <= 5)) n = 2;<br /> var nrand = Math.random();<br /> for (var i = 2;i<=n;i++){<br /> nrand *= Math.random();<br /> }<br /> return 2*nrand;<br /> },<br /> "invexp": function(n) { //returns less than 0<br /> return -RANDOM.exp(n);<br /> },<br /> "gamma3": function(alpha) { //Kundu and Gupta algorithm 3 http://home.iitk.ac.in/~kundu/paper120.pdf<br /> if (!alpha || Math.abs(alpha) > 1) alpha = 1; //alpha between 0 and 1<br /> var d = 1.0334 - (0.0766*Math.exp(2.2942*alpha));<br /> var a = Math.pow(2,alpha)*Math.pow(1-Math.exp(-d/2),alpha);<br /> var b = alpha*Math.pow(d,alpha-1)*Math.exp(-d);<br /> var c = a + b;<br /> var U = Math.random();<br /> var X = (U <= a/(a+b)) ? -2*Math.log(1-(Math.pow(c*U,1/alpha)/2)) : -Math.log(c*(1-U)/(alpha*Math.pow(d,alpha-1)));<br /> var V = Math.random();<br /> if (X <= d) {<br /> var mess = (Math.pow(X,alpha-1)*Math.exp(-X/2))/(Math.pow(2,alpha-1)*Math.pow(1-Math.exp(-X/2),alpha-1));<br /> if (V <= mess) return X;<br /> else return this.gamma3(alpha);<br /> } else { //X > d<br /> if (V <= Math.pow(d/X,1-alpha)) return X;<br /> else return this.gamma3(alpha);<br /> }<br /> //output is > 0 and possibly > 1<br /> },<br /> "gamma": function(alpha,beta) { //Martino and Luengo http://arxiv.org/pdf/1304.3800.pdf luca@tsc.uc3m.es luengod@ieee.org<br /> if (!alpha || alpha <= 0) alpha = 1; //alpha >= 1 if negative or 0<br /> if (alpha > 0 && alpha < 1) return this.gamma3(alpha); // use different algorithm<br /> if (!beta || beta <= 0) beta = 1; //beta > 0<br /> var alphap = Math.floor(alpha);<br /> var X = Math.random();<br /> for (var i=2;i<=alphap;i++){<br /> X *= Math.random();<br /> } <br /> var betap = (alpha < 2) ? beta/alpha : beta*(alphap-1)/(alpha-1);<br /> X = -Math.log(X)/betap;<br /> var Kp = (alpha < 2) ? Math.exp(1-alpha)*Math.pow(alpha/beta,alpha-1) : Math.exp(alphap-alpha)*Math.pow((alpha-1)/beta,alpha-alphap);<br /> //then accept with prob p(X)/pi(X)<br /> if (alphap >= 2) {<br /> if (Kp*Math.pow(X,alphap-1)*Math.exp(-betap*X) >= Math.pow(X,alpha-1)*Math.exp(-beta*X)) return X/alpha;<br /> else return this.gamma(alpha,beta);<br /> }<br /> else if (alphap < 2) {<br /> if (Kp*Math.exp(-betap*X) >= Math.pow(X,alpha-1)*Math.exp(-beta*X)) return X/alpha;<br /> else return this.gamma(alpha,beta);<br /> }<br /> },<br /> "igamma": function(alpha,beta) { // returns less than 0<br /> return -RANDOM.gamma(alpha,beta);<br /> },<br /> "chi2": function(k) { // returns greater than 0<br /> var nrand = RANDOM.norm(2);<br /> nrand = nrand*nrand;<br /> if (!k || k <= 1) return nrand;<br /> for (var i=2;i<=k;i++){<br /> var krand = RANDOM.norm(2);<br /> krand = krand*krand;<br /> nrand += krand;<br /> }<br /> return nrand;<br /> },<br /> "coinFlip": function(weight){<br /> if (!weight || weight < 1) weight = 2;<br /> if (Math.random() > 1/weight) return 1;<br /> else return 0;<br /> }<br />};<br />//Copyright 2016 Trevor Gast: codeandcompose.com<br />//RANDOM, non-uniform random generators (5 May 2016)<br />//GNU General Public License<br /></pre><br />Dice gif courtesy of <a href="http://bestanimations.com/Games/Dice/Dice.html">http://bestanimations.com/Games/Dice/Dice.html</a>Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-90448316572982376172015-11-21T21:24:00.000-08:002015-12-02T10:58:59.618-08:00Take to the skies! Take the skies... recursively.If the game has been too much fun for you...<br /><br />I kid. I'll finish it someday. Probably.<br /><br />In the meantime, I've been taking an <a href="https://www.edx.org/course/introduction-aeronautical-engineering-delftx-ae1110x-0" target="_blank">Introduction to Aeronautical Engineering course on edx.org</a>. It's a great course (and free) from Delft Technical University. If you are interested in Aerospace, start there. It'll help you determine if you like math enough...<br /><br />It turns out: I do like math.<br /><br />So much so, that I programmed this <a href="http://games.codeandcompose.com/ISAcalculator.html" target="_blank">International Standard Atmosphere Calculator</a>. Using the 1976 international standard (which apparently hasn't changed since) you can plug in your geopotential altitude and get the air density and pressure. Very handy if you want check your flight altitude to make sure you won't <a href="http://codeandcompose.blogspot.com/2014/11/point-to-point-b-part-4.html">collide</a> with any other airplanes. Or if you need to do your Aerospace homework.<br /><br />I added a nice graph, so you can just click the altitude rather than typing it. I hope to add unit conversions and also altitude prediction based on pressure or air density. One thing at a time.<br /><br />Programming this little job also gave me a good excuse to use a recursive function.<br /><h3>Re...CURSES!</h3>If you have ever taken a programming course, you've been taught about recursion. And then promptly forgotten it thinking, "that's too confusing! I'll just do it a different way..."<br /><br />And you can solve most problems without ever using recursion. So why bother right?<br /><br />Because it is so much TIDIER!<br /><br />In the case of ISA, we have a piece-wise function that stacks on more function pieces as we progress to higher altitudes. Lapse rates change and we glide through isothermal layers... nevermind. Back to recursion!<br /><br />Without getting too into depth on ISA calculations, let's just say this:<br />If we want to calculate the air density for an altitude of 40 kilometers, we need to first calculate it for 11 using one equation, then up to 20 with another, then up to 32; all based on the calculations made for lower layers.<br />If we want to calculate at higher altitudes, we need even MORE equations dependent on all those below.<br /><br />So how does recursion help?<br /><br />Recursion allows us to call one function for any altitude, that calls itself for the lower calculations. Then it spits out just the answer we need.<br /><br />Without recursion, we'd have to write separate if.. then... statements for each case (retyping a lot of the same code). OR (as some ISA calculators do) we could cheat and store the key values for each layer in an array. But what's the fun in that?<br /><br />Here's the commented code:<br /><br /><pre class="prettyprint">//this function takes two parameters:<br />//alt is altitude in meters<br />//To is temperature offset in Kelvin<br />function getAtmosphere(alt,To) {<br /> // constants!<br /> var R = 287.058; //gas constant<br /> var TEMP = 288.15; //in Kelvin<br /> var ATM1 = 101325; //in Pascals<br /> var DENSITY = 1.225; //in kg/cubic meter<br /><br /> // range check (altitude in meters)<br /> if (alt > 84852) alt = 84852;<br /> if (alt < 0) alt = 0;<br /><br /> //this is our OUT. no infinite recursion!<br /> //it returns 1 standard atmosphere at sea level in an object<br /> if (alt === 0) { //sea level<br /> return {"alt":alt,"T":TEMP+To,"To":To,"p":ATM1,"rho":DENSITY};<br /> } <br /><br /> // THIS IS WHERE THE MAGIC HAPPENS!<br /> // the function calls ITSELF...<br /> var atm0 = getAtmosphere(getAltRange(alt),To);<br /> // getAltRange() is a simple function that returns the altitude for<br /> // the "layer" beneath. This is used to find the correct lapse rate<br /> // and equation below...<br /><br /> // lapseRate() returns the rate used to calculate temp for a given layer<br /> // here is where the calculation is done.<br /> // notice it uses output from itself (atm0)<br /> if (lapseRate(alt) != 0) {<br /> var T1 = atm0.T + lapseRate(alt)*(alt-getAltRange(alt));<br /> var rho1 = atm0.rho*Math.pow(T1/atm0.T,(-9.80665/(lapseRate(alt)*R)-1));<br /> var p1 = atm0.p*Math.pow(T1/atm0.T,-9.80665/(lapseRate(alt)*R)); <br /> } else { // lapseRate = 0<br /> var con = Math.pow(Math.E,-9.80665/(R*atm0.T)*(alt-getAltRange(alt)));<br /> var T1 = atm0.T<br /> var p1 = atm0.p * con;<br /> var rho1 = atm0.rho * con;<br /> }<br /><br /> // calculations "complete" it returns the data as an object<br /> return {"alt":alt,"T":T1,"To":To,"p":p1,"rho":rho1};<br />}<br /></pre>So wait? How can a function call itself and then use its own output to calculate. If it doesn't have the input... how can it give output... wouldn't it fail or return 'undefined'??<br /><br />Well, no. Let's look at an example:<br /><br />Let's say we call getAtmosphere(25000).<br /><br />The function needs input from getAtmosphere(20000), so it calls that and waits...<br /><br />THAT call needs input from getAtmosphere(11000), so it calls that and waits...<br /><br />THAT call needs input from getAtmosphere(0), so it calls it and... WAIT! That's the out!<br /><br />Once it calls that, it gets data returned! This is the KEY to recursion. It hits an end, now it can finish!<br /><br />Then, getAtmosphere(11000) takes the input from (0) and runs the calculation. It passes that to (20000) which calculates and passes to (25000). And like magic, we get our data.<br /><br />And no matter what altitude we call it on, it knows just how many times to call itself to get the right answer! Brilliant!<br /><br />I think I've waited 15 years to have a need for recursion. And I'm proud to say I only froze my browser once...<br /><br />Don't forget the OUT!<br /><br />EDIT:<br /><br />I have updated the calculator to properly return the density when there is a temperature offset. You can now change units and get the temperature up to 1000 km. A few other minor adjustments for accuracy too. Coming soon: density and pressure over 86 km!<br /><br />Also, feel free to <a href="https://github.com/Shytor/ISAcalculator" target="_blank">fork on GitHub</a>!<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-WvVU2pxn9ig/VlFTGKw6S5I/AAAAAAAAAgo/dOLaUeTvs34/s1600/clouds_in_earths_atmosphere_m.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-WvVU2pxn9ig/VlFTGKw6S5I/AAAAAAAAAgo/dOLaUeTvs34/s1600/clouds_in_earths_atmosphere_m.jpg" /></a></div>Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-2405755720883257742015-10-15T20:51:00.001-07:002015-10-15T20:54:10.765-07:00Comedy is all about... timing.In the case of my game concept, so is user input.<br /><br />"So what is this crazy game concept you keep talking about?"<br /><br />Here it is:<br /><br />Do you remember <a href="https://www.youtube.com/watch?v=uTVL-nIfw1k" target="_blank">Super Mario RPG and timed hits</a>? It was a fun addition to the usual turn-based RPG style game that added an extra layer of player interaction.<br /><br />It was a nice touch that made the game very unique, in my opinion. I was surprised that it didn't seem to catch on. But it got me thinking...<br /><br />I'm a music composer and sound designer; why not combine those skills with the concept of timed hits?<br /><br />For this demo, I (quickly) made some sound effects using musical sounds. There is a background layer to keep time -- a simple 4/4 beat. When you send a character to attack, he first must arrive close enough to attack, and then he begins "priming".<br /><br />During this "priming" phase, the character will pulse in time with the music. Then you click on the target and if you are on the beat, you get an extra power boost. Yay!<br /><br />Depending on other factors, there will be other ways to maximize attacks. However be aware, in this demo, the enemies can fight back if you get too close!<br /><br /><h3>Music, Audio and Timing</h3><br />Before we can time our hits, we need to time our audio! For this we use the Web Audio API. I learned from this great <a href="http://www.html5rocks.com/en/tutorials/audio/scheduling/" target="_blank">information here</a>.<br /><br />For those of you into instant gratification: here is the code used to schedule the music.<br /><br /><pre class="prettyprint">var lastBeatTime = null;<br />var lastBeat = null;<br />var nextBeatTime = null; // when will the next beat occur<br />var nextBeat = null; // 32 total (8 bars of 4)<br />var nextFrameTime = null; // when will the next frame draw?<br />// tempo (in beats per minute)<br />var tempo = 125.0;<br />// these must be recalculated if the tempo changes <br />var secondsPerBeat = 60.0 / tempo;<br />var framesPerBeat = 3600 / tempo;<br /><br />var calibration = -0.001; <br />// use this to fudge the click time back for accuracy (clicking late "sounds" correct)<br /><br />window.onload = function (){<br /> <br /> //make sure the webkit Audio API is available<br /> try { // Fix up for prefixing<br /> window.AudioContext = window.AudioContext||window.webkitAudioContext;<br /> audioctx = new AudioContext();<br /> } catch(e) {<br /> alert('Web Audio API is not supported in this browser');<br /> }<br /><br /> //load the audio (code for this class is below)<br /> gMusic.parseAudioFile();<br />}<br /><br />function startMusic(){<br /> secondsPerBeat = 60.0 / tempo;<br /> framesPerBeat = 3600 / tempo;<br /> nextBeat = 32;<br /> lastBeat = 31;<br /> nextBeatTime = audioctx.currentTime;<br /> lastBeatTime = nextBeatTime - secondsPerBeat;<br /><br />}<br /><br />function scheduler(time){<br /> //schedule the next beat<br /> lastBeat = nextBeat;<br /> lastBeatTime = nextBeatTime;<br /> if (nextBeat === 16) nextBeat = 1;<br /> else nextBeat++;<br /> nextBeatTime += 60.0 / tempo; // schedule the next quarter note<br /> scheduleSound(nextBeat,nextBeatTime);<br />}<br /><br />function scheduleSound(beatNumber,time){<br /> // create an oscillator -- it makes the beep<br /> var osc = audioctx.createOscillator();<br /> osc.connect( audioctx.destination );<br /> if (beatNumber % 32 === 1) {<br /> gMusic.playSound("Pad1.wav",time);<br /> }<br /> if (beatNumber % 16 === 1) {<br /> gMusic.playSound("Beat1.wav",time);<br /> gMusic.playSound("Drums3.5.wav",time + (secondsPerBeat*2.5));<br /> }<br />}<br /><br />function animate(){<br /> var NOW = audioctx.currentTime; //gets the time of the last draw...when is now?<br /> nextFrameTime = NOW + (1 / 60); // calculate when this frame draw will occur<br /> if (nextBeatTime <= nextFrameTime) scheduler(nextFrameTime);<br /> //calculate the part of the beat the frame will draw at (as a percentage)<br /> var bp = Math.round((nextFrameTime - lastBeatTime) / (nextBeatTime - lastBeatTime) * 100);<br /> <br /> // ALL OTHER ANIMATION OCCURS<br />}<br /></pre><br />gMusic is an instance of an AudioFontClass I created. More on that later, but this is the playSound function it uses when scheduling audio.<br /><br /><pre class="prettyprint">playSound: function (name,time) {<br /> //first find the buffer<br /> var b = null;<br /> for(var i = this.sounds.length - 1; i >=0; i--) {<br /> // find the buffer for the name<br /> if(this.sounds[i].name === name) b = this.sounds[i].buffer;<br /> }<br /> // if we don't find the sound, do nothing<br /> if (!b) return;<br /><br /> var soundSource = audioctx.createBufferSource();<br /> //schedule the sound<br /> soundSource.buffer = b;<br /> soundSource.connect(audioctx.destination);<br /> soundSource.start(time);<br />} </pre><br />How does it work?<br /><br />Simple: each animation frame checks to see if the next frame will happen after the current beat. If it will, it calls the scheduler to schedule the audio for the next beat. The scheduled keeps track of which beat it is on, and loops the music every 4 bars (or 1 or 2).<br /><br />The nice thing about the Web Audio API is that you can schedule audio way ahead of time, so nothing plays late.<br /><br />However, one of the future challenges in making this web game will be keeping the load time down. Too much audio means longer load times.<br /><br />For now, <a href="http://games.codeandcompose.com/gamedemo1/" target="_blank">try the demo here!</a> Enjoy the terrible artwork!<br /><br /><b>Controls are as follows:</b><br /><br />Click and drag from a character to aim your "wave attack".<br /><br />A single click will select him. If you release on the character, you can click elsewhere to move, or an enemy to attack. (Don't forget to click the enemy when you arrive for a "timed" attack! On the beat gives you a boost!)<br /><br />Once selected, Z and X cycle through the colors of the "wave attack". Different waves will have a different effect on the enemies.<br /><br />A second click on a selected character will bring up a menu.<br /><br />You can choose a formation: some formations require a certain arrangement to already exist. Try moving characters around first. Be aware that the enemies react to formation changes.<br /><br />Taunting an enemy or protecting an ally can help keep the enemies away from wounded allies.<br /><br />And escape... well, you don't need that.Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-31117261283285220822015-06-28T15:50:00.005-07:002015-06-28T15:50:48.796-07:00Enough Fun and GamesOk, So I am taking a short break from the game to do some real work. Sort of.<br /><br />In learning Javascript, I decided it would be worth my while to learn some jQuery as well. Turns out it is super-easy and useful for making simple applications that have "real work" uses.<br /><br />I developed two small tools that could be used in a number of ways. One is just a <a href="http://games.codeandcompose.com/WorkApps/Contents/Contents.html" target="_blank">big-old table of contents style drop down, complete with link descriptions</a>.<br /><br />The other is what I call a "workflow" that could be used for giving instructions or even trouble-shooting common issues. <a href="http://games.codeandcompose.com/WorkApps/Workflow/Workflow.html" target="_blank">Check it out here</a>.<br /><br />Both are easily adaptable and open source. So feel free to <a href="http://games.codeandcompose.com/WorkApps/sourcecode.zip">pilfer the code</a>. Feel free to ask me questions if you need help using it.<br /><br /><h3>Table of Contents</h3>For this little app, I just needed a list that I could easily add links to and would fold itself up, so there wouldn't be too many on a page to look at at once. You can just look at the relevant section and ignore the rest.<br /><br />First, each "category" is listed inside list tag with class "slide". Then underneath, we create a list of links with class "articles". You can make unlimited categories. <br /><pre class="prettyprint"> <br /><ul class='slide'><br /><li><h3>Category 1</H3></li><br /> <ul class="articles"><br /> +Each line is split at the plus<br /> +type "#plus" if you want to actually see a plus sign as text<br /> +It automatically parses addresses as links that are listed with http:<br /> +the script parses out the part after "Description:" for the mouseover<br /> +download the code here! http://games.codeandcompose.com/WorkApps/sourcecode.zip<br /> Description:The best code ever written! Or not.<br /> </ul><br />... </pre><br />The app splits each line in the "articles" list by the +. Then it creates a link with an address (if there is one starting with http:// or https://). If there is a description (split at "Description:"), that will show up when you hover.<br /><br />There is a tiny cosmetic glitch with the hover descriptions. I haven't yet come up with a great fix, but it's not too noticeable.<br /><br />The string parsing was pretty simple. It uses some <a href="http://regexr.com/" target="_blank">regular expressions</a> and Javascript string.replace() function. After breaking the strings, it puts them into list elements and links (when used).<br /><br /><pre class="prettyprint"> <br />$.fn.extend({<br /> stringSplitToList: function() //this function parses the html body into a list for the jquery<br /> {<br /> var htmlList = '', aclass = 'none';<br /> $.each($(this).html().split('+'), function(i, v) {<br /> v = v.replace(/#plus/g,"+");//put plusses back in<br /> var tempSplit = v.split("Description:");<br /> v = tempSplit[0];<br /><br /> //makes sure the description not undefined.<br /> var description = (tempSplit[1]) ? tempSplit[1] : "No description."; <br /><br /> if (v.match(/(https:\/\/|http:\/\/)\S+/igm)) {<br /> var alink = v.match(/(https:\/\/|http:\/\/)\S+/igm)[0];<br /> var title = v.replace(alink,"");<br /> if (title.match(/\*/gm)){<br /> title = title.replace("*","");<br /> aclass = 'internal';<br /> } else if (title.match(/\^/gm)){<br /> title = title.replace("^","");<br /> aclass = 'archive';<br /> } else {<br /> aclass = 'none';<br /> }<br /> htmlList += '<li><a href="' + alink + '" class="' + aclass + '">' + title + '</a><div class="des">' + description + '</div></li>';<br /><br /> //handle categories header<br /> } else if (v.match(/(href=)\S+/igm)) {<br /> var alink = v.match(/(href=)\S+/igm)[0];<br /> var title = v.replace(alink,"");<br /> alink = alink.replace("href=","");<br /> <br /> if (title.match(/\*/gm)){<br /> title = title.replace("*","");<br /> aclass = 'internal';<br /> } else {<br /> aclass = 'none';<br /> }<br /> htmlList += '<li><a href="' + alink + '" class="' + aclass + '">' + title + '</a><div class="des">' + description + '</div></li>';<br /><br /> //handle categories header<br /> } else {<br /> htmlList += '<li><div class="category">' + v + '</div></li>';<br /> }<br /> });<br /> $(this).html(htmlList);<br /> }<br />});</pre><br />Each link can also be marked as "internal" with a * or "archived" with a ^. You can even use internal links marked by href=<br /><br />Just call this line once the page loads and it parses the text for you.<br /><br /><pre class="prettyprint">$('.articles').each(function(index){$(this).stringSplitToList();});<br /></pre><br /><h3>Workflow App</h3>The workflow app can take very complex paths. By following the formula in each js file, you can add unlimited questions and answers. As the user chooses their answers, links, pictures and tips are given to help them out. The logic can be created to direct to a certain path based on previous answers form one or multiple questions.<br /><br />I used it to make a wi-fi router trouble-shooting guide. The example given on this website is just for fun though.<br /><br />Here is a graffle showing how the questions are programmed.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-CF_yR0zDzjw/VZBsVRCebeI/AAAAAAAAAU8/iyBXsB5bkz0/s1600/colorgraffle.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-CF_yR0zDzjw/VZBsVRCebeI/AAAAAAAAAU8/iyBXsB5bkz0/s1600/colorgraffle.jpg" /></a></div><br /><br />Each question is loaded in the following format:<br /><br /><pre class="prettyprint">addQuestion(1, //the question number<br /> //the question<br /> "This is workflow 1. Use this workflow?",<br /> //answer options (as many as you want)<br /> ["Maybe","No","Yes"],<br /> //answer direction, either a question number or LOGIC or RESET<br /> [1,"LOGIC",1],1, // the extra number is default<br /> //tips text<br /> "This area will show helpful tips. Or not...",<br /> //image urls (as many as you want) or null<br /> ["image1.png","image2.png"],<br /> // url and text for helpful links<br /> [{"url":"http://tagsounds.com","title":"A Link to TAGSOUNDS.COM"}],<br /> // callback function for when the direction of an answer is LOGIC<br /> function(){<br /> if (AA[0] === "Red"){<br /> nextQuestion(201);<br /> return;<br /> } else loadWorkflow("workflow2.js");<br /> }<br />);<br /></pre><br />If you like it, or would like to know more about how to adapt it for your needs, please contact me using the <a href="http://tagsounds.com/contact/" target="_blank">contact form at tagsounds.com</a>.<br /><br /><br />Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-78803174270335365742015-04-28T11:10:00.000-07:002015-04-28T11:10:12.571-07:00INPUT (Click me! Click me!)<div class="separator" style="clear: both; text-align: center;"><a href="http://1.bp.blogspot.com/-cz-up02DC2k/VT7p4FdY8oI/AAAAAAAAAR4/lkbqBAw_LuE/s1600/clickme.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-cz-up02DC2k/VT7p4FdY8oI/AAAAAAAAAR4/lkbqBAw_LuE/s1600/clickme.gif" /></a></div>One of the most important elements of game-play is user interaction.<br /><br />So many RPG games are great tactical games, but lose the sense of real-time enjoyment while bouncing through menus. An early prototype of my game was menu centric. But as I make some mini demos, I'm trying to limit the amount of time spent in the menus in favor of faster input choices.<br /><br />For this demo, there is no need for a menu. One click on a character selects him and then click the enemy and he attacks. OR, click and drag to aim the projectile, release to fire. Click twice on the character (not double-click for reasons I will explain) and then click an empty space to move them.<br /><br />I'd like to keep it setup in a way that may be ported to touch screen devices. I imagine that will introduce new challenges. Perhaps I'll learn Objective-C first and make an app... ?<br /><br />Anyway. Step one is capturing the click. Since everything is done inside the canvas, we use the following code:<br /><br /><pre class="prettyprint">window.onload = function (){<br /> var canvas = document.getElementById("gamewindow");<br /> document.getElementById('gamewindow').onmousemove = moveHandler;<br /> document.getElementById('gamewindow').onmousedown = clickHandler; //onclick is too slow for timing<br /> document.getElementById('gamewindow').onmouseup = clickReleaseHandler;<br /> document.onkeypress = onkeypressHandler;<br />}<br />function moveHandler(event){}<br />function clickHandler(event){}<br />function clickReleaseHandler(event){}<br />function onkeypressHandler(event){}<br /></pre>Notice that I used mousedown instead of onclick. This is important for calling functions quickly so that when the player tries to synchronize with the music, it is more accurate. Plus we can catch the mouseup event separately. The downside, is that differentiating click from double click is harder, so we just don't do it!<br /><br />Each event listener is directed to a handler function. Now we have to do, you know, logic.<br /><br />Unlike normal interactive elements, there are a few challenges to overcame when dealing with a live game environment. Starting with the fact that everything moves. Thus, step one is finding out what was clicked.<br /><br />Or as I quickly learned, the FIRST step is to offset your click location to account for the placement of the canvas and scrolling...<br /><br /><pre class="prerttyprint">function clickHandler(event){<br /> // cache the time/location of the click<br /> clickTime = audioctx.currentTime;<br /><br /> event = event || window.event;<br /> var scrollX = window.pageXOffset || document.body.scrollLeft;<br /> var scrollY = window.pageYOffset || document.body.scrollTop;<br /> clickX = event.clientX + scrollX - xoffset;<br /> clickY = event.clientY + scrollY - yoffset;<br /><br /> var gclick = guyClicked(clickX,clickY,"Allies",true);<br />}<br /><br />function guyClicked(X,Y,dic,mustBeReady){<br /> var dict = activeGuyDict[dic];<br /> for (var guy in dict){<br /> if (!(X < dict[guy].pos.x + dict[guy].bounding.x || X > dict[guy].pos.x + dict[guy].bounding.x + dict[guy].bounding.w || Y < dict[guy].pos.y + dict[guy].bounding.y || Y > dict[guy].pos.y + dict[guy].bounding.y + dict[guy].bounding.h)){<br /> if ((mustBeReady && dict[guy].ready) || !mustBeReady) return guy;<br /> //character must be ready if "mustBeReady" is true<br /> else return null;<br /> }<br /> }<br /> //no guy was found<br /> return null;<br />}<br /></pre>If you don't feel like ripping through that IF statement in guyClicked, here's the rough explanation. It's a beautiful use of NOT OR logic. We need to see if the click occurred INSIDE the bounding box for the character. If the box is 50x100 at point (200,130), we see if the x was between 200 and 250 and if y was between 130 and 230, right?<br /><br />We could setup the logic to see if x > 200 AND x < 250 AND y > 130 AND y < 230...<br /><br />OR<br /><br />... we could check if the click falls OUTSIDE the parameters, and use NOT OR. Only one condition must be true for the whole thing to fail. Not sure if it matters here, but this is useful if you are comparing BOXES to look for overlap.<br /><br />For my game, we also check to see if the character is "ready" before selecting. Now onto the real tricky logic.<br /><br />First click down gets the aiming out. Then release to choose an enemy. Then click an enemy or click the good guy again. Or click another guy to change the selection. Then click a spot to move. Or if an enemy was selected, click again to time the attack. Or, um... what else can you do with just a click? It gets complicated fast.<br /><br />I found that (although counter-intuitive) it works best to write the logic backwards. Start with the most specific scenario first and work backwards through input order. That way, the handler can check for the specific scenarios and bail, or move on.<br /><br />In plain text it looks like this:<br /><div class="mylist"><ul><li>Check to see if a selected character is waiting to move.</li><ul><li>If a character was clicked, change focus</li><li>If an empty space was clicked, move </li></ul><li>Check to see if an attacking character is "PRIMING".</li><ul><li>Was the appropriate enemy clicked?</li></ul><li>Check to see if an enemy must be selected to attack.</li><ul><li>Did you click an enemy?</li><li>If a character was clicked change focus</li></ul><li>If none of those criteria are met, select the clicked character. </li></ul></div><pre class="prettyprint"> <br />for (var pg in activeGuyDict.Allies){<br /> // check to see if a character needs to move<br /> if (activeGuyDict.Allies[pg].moveForm){<br /> activeGuyDict.Allies[pg].moveForm = false;<br /> activeGuyDict.Allies[pg].selected = false;<br /> colorWheel.reset();<br /> //need to check if the space is occupied by the other character<br /> if (guyClicked(clickX,clickY,"Allies")) continue;<br /> activeGuyDict.Allies[pg].formation = {"name":"Formation","pos":{cx:clickX,cy:clickY},"halo":{cr:0,r:5}};<br /> activeGuyDict.Allies[pg].action = "FORMATION";<br /> activeGuyDict.Allies[pg].target = activeGuyDict.Allies[pg].formation;<br /> return;<br /> }<br /> <br /> if (activeGuyDict.Allies[pg].animation.name != 'PRIME') continue;<br /> if (!activeGuyDict.Allies[pg].attackTarget) continue;<br /> else var primeClick = guyClicked(clickX,clickY,"Enemies");<br /> if (!primeClick) continue;<br /> if (primeClick === activeGuyDict.Allies[pg].attackTarget){<br /> var cT = clickTime; //un-cache the click time<br /> activeGuyDict.Allies[pg].setAnim('ATTACK',cT);<br /> <br /> //this code determines the power, based on the timing<br /> var sync = 0;<br /> var hbl = secondsPerBeat / 2; //half a beat length<br /> if (cT - lastBeatTime < nextBeatTime - cT) sync = Math.round((cT - lastBeatTime)/hbl * 50);<br /> else sync = Math.round((nextBeatTime - cT)/hbl * -50); <br /> //calculate the power/damage here<br /> var power = activeGuyDict.Allies[pg].power(sync);<br /> activeGuyDict.Enemies[activeGuyDict.Allies[pg].attackTarget].takeHit(power,pg);//determine the damage taken from the hit<br /> clickTime = null;<br /> return; //don't do anything else with the click<br /> }<br />}//end FOR loop<br /> <br />if (selectThis === "ENEMY"){//picking an enemy to attack<br /> var gc = guyClicked(clickX,clickY,"Allies",true);<br /> var g = findSelectedChar();<br /> if (gc) { // a good guy was clicked... handle it<br /> if (gc != g){ //a different ready character was selected..switches focus<br /> selectThis = "";<br /> activeGuyDict.Allies[g].selected = false;<br /> activeGuyDict.Allies[g].action = '';<br /> var cT = clickTime;<br /> activeGuyDict.Allies[gc].selected = true;<br /> colorWheel.newSC(gc);<br /> selectThis = "AIM";<br /> activeGuyDict.Allies[gc].setAnim('AIM',cT);<br /> activeGuyDict.Allies[gc].action = 'ATTACK';<br /> mouseDown = true;//keep track of the mouse state<br /> return;<br /> } else if (gc === g) { //same character was clicked... moveForm<br /> activeGuyDict.Allies[g].moveForm = true;<br /> //cancel ATTACK action<br /> selectThis = "";<br /> activeGuyDict.Allies[g].action = "";<br /> return;<br /> }<br /> }<br /> var e = guyClicked(clickX,clickY,"Enemies");<br /> if (!e) return; //make sure an enemy was clicked<br /> if (activeGuyDict.Allies[g].action === 'ATTACK'){<br /> activeGuyDict.Allies[g].ready = false;<br /> activeGuyDict.Allies[g].selected = false;<br /> colorWheel.reset();<br /> activeGuyDict.Allies[g].target = {"name":e,"pos":activeGuyDict.Enemies[e].pos,"halo":activeGuyDict.Enemies[e].halo};<br /> selectThis = "";<br /> colorWheel.reset();//close the color wheel<br /> } else if (activeGuyDict.Allies[g].action === 'TAUNT'){<br /> activeGuyDict.Enemies[e].taunted(activeGuyDict.Allies[g].pos,activeGuyDict.Allies[g].halo);<br /> activeGuyDict.Allies[g].animation.name = 'TAUNT';<br /> activeGuyDict.Allies[g].selected = false;<br /> colorWheel.reset();<br /> selectThis = "";<br /> }<br /> return;<br />}<br />//see if a ready guy was clicked<br />var gclick = guyClicked(clickX,clickY,"Allies",true);<br />if (gclick){<br /> var cT = clickTime;<br /> activeGuyDict.Allies[gclick].selected = true;<br /> colorWheel.newSC(gclick);<br /> selectThis = "AIM";<br /> activeGuyDict.Allies[gclick].setAnim('AIM',cT);<br /> activeGuyDict.Allies[gclick].action = 'ATTACK';<br /> mouseDown = true;//keep track of the mouse state<br />}</pre>This is the mouseup handler. It only pays attention if the player is holding the mouse down to "AIM". Thus avoiding unnecessary event functions. <br /><pre class="prettyprint">function clickReleaseHandler(event){<br /> // cache the time/location of the click<br /> if (selectThis != "AIM") return;<br /> clickTime = audioctx.currentTime; <br /> event = event || window.event;<br /> var scrollX = window.pageXOffset || document.body.scrollLeft;<br /> var scrollY = window.pageYOffset || document.body.scrollTop;<br /> clickX = event.clientX + scrollX - xoffset;<br /> clickY = event.clientY + scrollY - yoffset;<br /> <br /> var g = findSelectedChar();<br /> var gc = guyClicked(clickX,clickY,"Allies");<br /> if (gc === g) {<br /> selectThis = "ENEMY";<br /> activeGuyDict.Allies[g].clearAnim();<br /> mouseDown = false;<br /> return; //bail<br /> } else {<br /> //they released on the aimed area<br /> var cT = clickTime; <br /> var sync = 0;<br /> var hbl = secondsPerBeat / 2; //half a beat length<br /> //click is closer to last beat<br /> if (cT - lastBeatTime < nextBeatTime - cT) sync = Math.round((cT - lastBeatTime)/hbl * 50);<br /> else sync = Math.round((nextBeatTime - cT)/hbl * -50);//click is closer to next beat<br /> //click was before last beat (in case the beat changes between click and frame)<br /> //if (cT < lastBeatTime) sync = 50 - Math.round((lastBeatTime - cT)/hbl * 50);<br /> var power = activeGuyDict.Allies[g].power(sync);<br /> clickTime = null;<br /><br /> //create the projectile<br /> projectiles.add("wave",activeGuyDict.Allies[g].pos,clickX,clickY,power,8,500,activeGuyDict.Allies[g].color);<br /><br /> selectThis = "";<br /> activeGuyDict.Allies[g].ready = false;<br /> activeGuyDict.Allies[g].selected = false;<br /> activeGuyDict.Allies[g].setAnim('ATTACK',cT);<br /> activeGuyDict.Allies[g].target = null;<br /> mouseDown = false;<br /> return;<br /> }<br />}</pre>As always, feel free to steal my code. Just let me know how you are using it! I'm curious to know how and if anyone finds this code useful! <br /><br />And as promised, here's a <a href="http://games.codeandcompose.com/projectilegame.html" target="_blank">link to the demo</a>!<br /><br />Instructions:<br /><div class="mylist"><ul><li>CLICK and DRAG from a character to shoot a projectile. They will not do damage, but different colors will have different effects,</li><li>PRESS Z or X to change the color of a character after he is selected.</li><li>Just CLICK a character once to select him, and then click an enemy to attack. The character will chase the enemy. You MUST CLICK the enemy once he has arrived in order to initiate the attack. Try to time it with the pulsing to do extra damage.</li><li>CLICK the selected character again, and then click an empty space to move him.</li><li>No damage indication is given, just keep attacking, and the enemies will eventually disappear.</li></ul></div><br />Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-86886741887844073032015-04-17T19:50:00.000-07:002015-04-17T19:50:06.088-07:00Color and DissonanceSo what exactly is this game concept I keep mumbling about?<br /><br />Well, here's part of the picture: You are immersed in a world with a mysterious energy force. Not much is known about it except two things. First, it's great for making powerful machines and weapons. And when it is harvested, horrifying and dangerous spirits are released.<br /><br />Naturally, along your quest, you must face these spirits to survive. But you can't just beat them to oblivion like normal RPG monsters or zombies. They have to be "in phase".<br /><br />As a composer, I decided that I should make a game that fit my specific knowledge and skill set. Thus, music and sound had to be solidly integrated into the game play. I'll get more into the music side later, but there is an underlying function of music in the battles as well.<br /><br />Imagine each spirit is assigned a "note" or frequency. In addition to fighting them, you can also blast them with another "note" using the <a href="http://codeandcompose.blogspot.com/2015/04/projectiles-as-javascript-objects.html">wave projectiles we created here</a>. Depending on the "interval" between the notes, a different effect is achieved.<br /><br />For those of you not familiar with music theory, this gets a little intense. And since I want the game play to be inclusive, I had to come up with a visual representation. So why not color each note?<br /><br />This presented me with more of a challenge than I had expected. HOW do you color all 12 notes?<br /><br />Many people have tried to color the scale for educational purposes, but usually focus only on a 7 note scale. With Roy G Biv, that works out nicely. But I had to get a little more creative.<br /><br />Since the effect of the interval will be based on its "dissonance" or "consonance", I decided to match a color wheel using the Circle of Fifths. If you know what I'm talking about, see the diagram. If not... um, well... take a music class.<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-xV8JKu5pL7U/VTEWVNXcDrI/AAAAAAAAARk/PEx6rSrOSkM/s1600/COFcolorwheel.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-xV8JKu5pL7U/VTEWVNXcDrI/AAAAAAAAARk/PEx6rSrOSkM/s1600/COFcolorwheel.jpg" /></a></div>The adjacent notes are all an interval of a 5th. Which is consonant. As you move around the circle, away from one note, the dissonance increases. For example, C is a semi-tone from B and a tri-tone from F#... very dissonant. This, similar colors blend, opposites, not so much.<br /><br />For game play purposes I grouped them like this:<br />Unison (same note) will heal the enemy.<br />5th is consonant, so no effect. <br />2nd (whole-tone) will have a disruptive effect, like stopping the enemy for a moment.<br />Minor 3rd will have a positive effect, like speeding up the enemy.<br />Major 3rd will have a negative effect, like slowing.<br />Semi-tone and tri-tones are so disruptive they will actually change the note of the enemy.<br /><br />The math for this works out really well too. The HSL color mode has 360 hues. That means the intervals can be determined by subtracting the color hues. For example, if C=150 and A=240, the difference, 90, represents a minor 3rd.<br /><br />Now, it would be no fun to let each character have access to all 12 notes. So they get a "key palette". Basically, a set of notes that are available during battle.<br /><br />I had to add some properties to my good guy object:<br /><pre class="prettyprint">this.color = 150;//may be unnecessary = this.palette[0];<br />this.palette = [150,210,270,120,180,240,300];//an array of the colors available<br />this.changeStep = function(step){<br /> this.palette.rotate(step)<br /> this.color = this.palette[0];<br /> //return this.color;<br />};</pre>The changeStep() function is called with an interval so the character can move through his palette (currently by pressing Z or X). What is palette.rotate()? A handy little method added to the Array.prototype that pushes items around the array in order. There are <a href="http://jsperf.com/js-rotate-array/14" target="_blank">other methods of doing this here</a>... but I liked this the best.<br /><pre class="prettyprint">Array.prototype.rotate = (function() {<br /> return function(inc) {<br /> for (var l = this.length, inc = (Math.abs(inc) >= l && (inc %= l), inc < 0 && (inc += l), inc), i, x; inc; inc = (Math.ceil(l / inc) - 1) * inc - l + (l = inc))<br /> for (i = l; i > inc; x = this[--i], this[i] = this[i - inc], this[i - inc] = x);<br /> return this;<br /> };<br />})();<br /></pre>Then of course, we need to know when the projectile hits anything. So while we check for collisions between the enemy and other obstacles, we also call the projectile.HIT() method below. Now these waves don't dissipate upon hitting something, in fact they pass right through. So what's the best method for determining collisions?<br /><br />Unlike collisions that ricochet, we don't need any fancy vector math to get the angles or energy transfer. All we really need to know is how close the hit was.<br /><br />I decided on a simple formula for this. Once the collision is detected, we keep track of the exact distance between the center of the enemy and the center of the projectile. Then, when the distance starts to increase, we record the closest point and call the appropriate function for the enemy's reaction. That distance is passed to help determine the power (effectiveness) of the wave. This code is part of the "projectile" object constructor.<br /><pre class="prettyprint">this.HIT = function (Epos,Ehalo,Ename){<br /> var dis = getDistanceSq(this.pos,Epos);<br /> if (dis.ds > (this.halo.r+Ehalo.cr)*(this.halo.r+Ehalo.cr)) return false;//not close enough<br /> //have it check to see if it has reached the closest location<br /> if (!this.hitList[Ename]) {<br /> this.hitList[Ename] = dis.ds;<br /> return true;<br /> }<br /> if (this.hitList[Ename] === "HIT") return false;<br /> // they have already hit...is it getting closer or farther?<br /> if (this.hitList[Ename] >= dis.ds) { //closer<br /> this.hitList[Ename] = dis.ds;<br /> return true;<br /> } else {<br /> // use the closest distance and calculate damage<br /> //takewave is the call that tells the enemy what to do with the "damage"<br /> activeGuyDict.Enemies[Ename].takeWave(this.mass,this.hitList[Ename],this.color);<br /> //remove the enemy from the list so he isn't hit again!<br /> this.hitList[Ename] = "HIT";<br /> delete this.hitList[Ename];<br /> return true;<br /> }<br />};<br />this.hitList = {};<br /><br />// This is the helper function to get the squared distance <br /><br />function getDistanceSq(a,b){ //for when the square will do, to save energy rooting it...<br /> var x = b.cx - a.cx;<br /> var y = b.cy - a.cy;<br /> var ds = (x*x)+(y*y);<br /> return {ds:ds,x:x,y:y};<br />}</pre>getDistanceSq() is a handy little function that uses ol' Pythagoras, but returns the distance squared (and the x and y vector) as an object. By using the square for comparison, we save the trouble of using a costly Math.SQRT().<br /><br />One more blog post on the click cycle for battle mechanics, and then a DEMO! Yay! It will be playable, however without sound. And it won't be hard since you can't die yet... (one thing at a time).Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-88906451194870019592015-04-16T07:33:00.000-07:002015-04-16T07:34:03.201-07:00Projectiles as Javascript ObjectsIn my game, like many games, there is a chance to throw, shoot, fling or otherwise propel something hazardous at your enemy.<br /><br />I'll get more into the specific game mechanics in the next post. For now, we'll just focus on aiming and firing "something".<br /><br />I chose to treat each projectile as a separate entity, just like the characters and enemies. However since there are an unknown number of projectiles coming and going at any time, they don't need names. Thus we use an array to collect them.<br /><br />The nice thing about Javascript objects, is that we can make them do whatever we want. I decided to make a list object that not only keeps the projectiles, but adds and removes them thus:<br /><br /><pre class="prettyprint">var projectiles = {<br /> list:[],<br /> add : function (type,pos,x,y,mass,speed,range,color){<br /> var newP = new projectile(type,pos,x,y,mass,speed,range,color);<br /> newP.parent = this;<br /> this.list.push(newP);<br /> },<br /> remove : function (p){<br /> for (var i = this.list.length - 1; i >= 0; i--) {<br /> if (this.list[i] == p) {<br /> this.list.splice(i, 1);<br /> return;<br /> }<br /> }<br /> }<br />}</pre><br />Then I created an object constructor for the projectiles thus: <br /><pre class="prettyprint">function projectile(type,pos,x,y,mass,speed,range,color){<br /> this.parent = null;<br /> this.type = type;//wide, dart, explosive...etc<br /> this.pos = {"cx":pos.cx,"cy":pos.cy};<br /> this.halo = {cr:4,r:8};<br /> this.heading = normVec(pos,{"cx":x,"cy":y});//normalized vector vx and vy<br /> this.mass = mass;<br /> this.speed = 8;<br /> this.range = 500;<br /> this.irange = 500;<br /> this.color = color;<br /> this.delay = 25;//frame delay to allow for attack animation<br /> this.HIT = function (Epos,Ehalo,Ename){}<br /> this.hitList = {};<br /> this.move = function (){};<br /> this.kill = function (){<br /> this.parent.remove(this);<br /> };<br />}<br /><br /></pre><br />Most of these properties can be used in many ways to determine the exact behaviors. For now I'll be focusing on the HIT() and move() functions.<br /><br />The move() function will be called every frame and move the projectile by multiplying the heading by the speed. It also checks to verify that the range of the projectile has not been exceeded. If it has it calls kill().<br /><br /><br /><pre class="prettyprint">this.move = function (){<br /> this.pos.cx += this.heading.vx * this.speed;<br /> this.pos.cy += this.heading.vy * this.speed;<br /> this.range -= this.speed;//loses power as it moves<br /> if (this.range <= 0) this.kill();<br />};</pre><br />Easy. Now we have to DRAW them... For this game, the projectiles are actually a sound wave. So I used the arc() method of the canvas 2d context. By using some gradients and color stops, we can make a nice row of faded arcs. I created an "origin" for the center of the arcs 80 px behind the center. That way the arcs themselves are still drawn on the point that will be used for collisions. Then add a gradient that goes from the color to alpha 0. If you get the gradient just right, the arcs fade into nothing.<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-2wvb6XH25EQ/VS_GgSlSO7I/AAAAAAAAARQ/kcD8DO5TuPY/s1600/waveprojectile.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-2wvb6XH25EQ/VS_GgSlSO7I/AAAAAAAAARQ/kcD8DO5TuPY/s1600/waveprojectile.gif" /></a></div><br /><br /><pre class="prettyprint">for (var p = projectiles.list.length - 1; p >= 0; p--){<br /> if (projectiles.list[p].delay > 0) {<br /> projectiles.list[p].delay--;<br /> } else {<br /> //draw the projectile<br /> var originx = projectiles.list[p].pos.cx - (projectiles.list[p].heading.vx * 80);<br /> var originy = projectiles.list[p].pos.cy - (projectiles.list[p].heading.vy * 80);<br /> var c = projectiles.list[p].color;<br /> var rangeD = projectiles.list[p].range;<br /> if (rangeD > 100) rangeD = 1;<br /> else rangeD = rangeD / 100;<br /> var dr = projectiles.list[p].irange - projectiles.list[p].range;<br /> for (n = 1; n<=8; n++){<br /> if (dr < (9-n) * 4 + 10) continue;//don't draw wave lines behind the character<br /> var wave = Math.abs((n+frameCount)%fpb-(fpb/2))*10+50;<br /> var w = 4*(n+4);<br /> var nOffset = 4*n+48;<br /> var offX = projectiles.list[p].heading.vx * nOffset + originx;<br /> var offY = projectiles.list[p].heading.vy * nOffset + originy;<br /> var b = Math.pow(-1,n)*20 + 80;//for now<br /> if (b === 100) b = wave;<br /> var Gradient3 = ctx.createRadialGradient(offX,offY,0,offX,offY,w);<br /> Gradient3.addColorStop(0 , 'hsla(' + c + ',' + wave + '%,' + b + '%,' + rangeD + ')');<br /> Gradient3.addColorStop(1 , 'hsla(' + c + ',' + wave + '%,' + b + '%,0)');<br /> ctx.beginPath();<br /> var nAngle = getAngle(projectiles.list[p].heading);<br /> ctx.arc(originx,originy,nOffset, nAngle - (Math.PI/4), nAngle + (Math.PI/4), false); <br /> ctx.strokeStyle = Gradient3;<br /> ctx.lineWidth = 2;<br /> ctx.stroke();<br /> }<br /> projectiles.list[p].move();//move each projectile<br /> }<br />}</pre><br />This code could be rolled into the projectile object... but I left it out while writing. Oh well. #thingsiwillfixlater #imtoolaz<br /><br />The delay at the beginning is there to hold the drawing until the animation of the character has time to complete his "attack". Although I don't yet have any animations sooo it just counts down for now. <br /><br />The 'framecount' and 'fbp' (frames per beat) are global variables that keep track of where in the beat the animation will be drawn. This is used for synchronization with the music. Obviously not very necessary, but I think it will look cooler if the wave pulses with the sounds.<br /><br />And in case you haven't seen previous posts, the getAngle() function is below. It takes an x,y vector and turns it into radians. Thus telling us which way to "point" the arcs.<br /><pre class="prettyprint">function getAngle(p2,p1){//takes two positions as parameters, or a single vector<br /> if (!p1) var angle = Math.atan2(p2.vy,p2.vx);<br /> else var angle = Math.atan2(p2.cy-p1.cy,p2.cx-p1.cx);<br /> return angle;<br />}</pre><br />Full code and a playable demo (without sound for now) coming soon. Next post will be about how the waves work in the game play mechanics and the use of color and "dissonance". Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-62276299489102011452014-12-12T19:57:00.004-08:002015-04-16T07:33:45.019-07:00Accidents Do HappenSo after four great posts about how to avoid collisions, now it's time to talk about collision detection.<br /><br />Our characters are now successfully moving from point A to point B and usually avoiding each other. Just like in real life though, when it's crowded, people bump into each other. This looks like a job for Collision Detection!<br /><br />The concept is pretty simple: in my sample, each entity is described by a circle, or more practically, a center point and a radius. We merely determine if they overlap or touch. Solved.<br /><br />UNLESS you also want to know the <i>point of collision</i> and the force of the collision. Then you need some fancy vector math. Here's a <a href="http://gamedevelopment.tutsplus.com/tutorials/when-worlds-collide-simulating-circle-circle-collisions--gamedev-769" target="_blank">great link</a> to explain the details. I'll break it down into the important steps though.<br /><h4>Step 1: Did the objects collide?</h4><div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-si6k0rNj0qA/VHy1uA9Lr7I/AAAAAAAAAPo/ebPz5-HAoLY/s1600/Screen%2Bshot%2B2014-12-01%2Bat%2B1.38.16%2BPM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-si6k0rNj0qA/VHy1uA9Lr7I/AAAAAAAAAPo/ebPz5-HAoLY/s1600/Screen%2Bshot%2B2014-12-01%2Bat%2B1.38.16%2BPM.png" /></a></div>This is easy math. We take the sum of the radii of each object (since they are all treated like circles). If the distance between their centers is <i>less</i> than sum of the radii, the objects have collided. Since the Math.sqrt() function is somewhat costly, it's faster to skip the square root when determining the distance and use the square fo the sum of the radii.<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-PcoL0juhb-4/VIu4khTwaqI/AAAAAAAAAQU/606BQ7SKeVw/s1600/collision.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-PcoL0juhb-4/VIu4khTwaqI/AAAAAAAAAQU/606BQ7SKeVw/s1600/collision.jpg" /></a></div><h4>Step 2: What was the collision point?</h4>Some more simple math. The collision occurs at a point directly between the centers of the two circles. DUH. If the objects have an equal radius it will be exactly between them. If they are different sizes though, we just use the radii to create a ratio*. If (x1,y1) and (x2,y2) are the centers then the collision point (x,y) can be calculated thus.<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-FH2dGQcFZlQ/VIu1gt0Rx7I/AAAAAAAAAQA/Iq0zwzRc8ig/s1600/Screen%2BShot%2B2014-12-12%2Bat%2B10.38.49%2BPM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-FH2dGQcFZlQ/VIu1gt0Rx7I/AAAAAAAAAQA/Iq0zwzRc8ig/s1600/Screen%2BShot%2B2014-12-12%2Bat%2B10.38.49%2BPM.png" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-FhCDwyGSN8c/VIu1gqiKiiI/AAAAAAAAAP8/v0gGsR7FaxQ/s1600/Screen%2BShot%2B2014-12-12%2Bat%2B10.39.02%2BPM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-FhCDwyGSN8c/VIu1gqiKiiI/AAAAAAAAAP8/v0gGsR7FaxQ/s1600/Screen%2BShot%2B2014-12-12%2Bat%2B10.39.02%2BPM.png" /></a></div> And because this is true:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-tMexpwCo0FM/VIu1go8tgLI/AAAAAAAAAP4/wBkzaQsjzB0/s1600/Screen%2BShot%2B2014-12-12%2Bat%2B10.39.12%2BPM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-tMexpwCo0FM/VIu1go8tgLI/AAAAAAAAAP4/wBkzaQsjzB0/s1600/Screen%2BShot%2B2014-12-12%2Bat%2B10.39.12%2BPM.png" /></a></div>...point 1 and 2 can be reversed.<br /><br /><i>*Note: Since we only check for collisions each frame, this method does not account for the fact that the true time of collision actually happened between frames. Thus, the "true" collision point is usually different. However, for the sake of simplicity, this method is more practical.</i><br /><h4>Step 3: How hard did they hit?</h4>Now we get some fancy math. When two objects collide, they exert force on each other and "bounce" like pool balls. Newton was a smart guy. This is the code to calculate everything. It returns the new velocity for the "guy" passed to the function. You can add that to his movement vector to simulate bounce, or apply it as the new velocity if it is free-floating in space.<br /><br /><pre class="prettyprint">function findCollisions(guy){<br /> var velx = 0;<br /> var vely = 0;<br /> var collided = false;<br /> //loop through all the other objects<br /> for (var c in dict){<br /> if (guy === c) continue; //skip yourself<br /> if (!dict[c].collisions) continue;//skip if collisions are off<br /> var haloSq = dict[guy].halo.cr + dict[c].halo.cr;<br /> haloSq *= haloSq; //use the square to save rooting...<br /> var dist = getDistanceSq(dict[guy].pos,dict[c].pos);<br /> if (dist.ds > haloSq) continue; //they did not collide<br /> else if (dist.ds <= haloSq) { //they collided<br /> //find the collision point<br /> var collisionPointX = ((dict[guy].pos.cx * dict[c].halo.cr) + (dict[c].pos.cx * dict[guy].halo.cr)) / (dict[guy].halo.cr + dict[c].halo.cr);<br /> var collisionPointY = ((dict[guy].pos.cy * dict[c].halo.cr) + (dict[c].pos.cy * dict[guy].halo.cr)) / (dict[guy].halo.cr + dict[c].halo.cr);<br /> //find the TOTAL velocity of the collision<br /> var xVelocity = dict[c].vel.vx - dict[guy].vel.vx;<br /> var yVelocity = dict[c].vel.vy - dict[guy].vel.vy;<br /> //use the dot product to calculate the "exit" velocity<br /> var dotProduct = -dist.x * xVelocity + -dist.y * yVelocity;<br /> if (dotProduct > 0){<br /> collided = true;<br /> var collisionScale = dotProduct / dist.ds;<br /> var xCollision = dist.x * collisionScale;<br /> var yCollision = dist.y * collisionScale;<br /> //The Collision vector is the speed difference projected on the Dist vector,<br /> //thus it is the component of the speed difference needed for the collision.<br /> var combinedMass = dict[guy].mass + dict[c].mass;<br /> var collisionWeightA = 2 * dict[c].mass / combinedMass;<br /> //var collisionWeightB = 2 * dict[guy].mass / combinedMass;<br /> velx -= collisionWeightA * xCollision;//new vel for dict[guy]<br /> vely -= collisionWeightA * yCollision;<br /> //new vel for activeGuyDict.Allies[c]...not necessary since it will be calculated on another pass<br /> //B.xVel -= collisionWeightB * xCollision;<br /> //B.yVel -= collisionWeightB * yCollision;<br /> //draw a green dot at the collision point<br /> ctx.fillStyle = "green";<br /> ctx.fillRect(collisionPointX-2,collisionPointY-2,4,4);<br /> }<br /> }<br /> }<br /> if (collided) return {nvx:velx,nvy:vely};//return the new vector to change after all collisions have been detected and calculated<br /> else return null;<br />}<br /></pre><br />This is a <a href="http://codeandcompose.blogspot.com/p/blog-page.html" target="_blank">fully functioning script</a> that demonstrates my own collision avoidance/detection algorithm at work. It isn't the fastest algorithm, but it functions well enough with 32 entities on the screen. For the purposes of my own demo, there will probably be no more than 12 characters on the screen at once, so this will do fine.<br /><h4>Instructions:</h4>Press any key to load the entities.<br />Press R to organize them into a circle.<br />Press B to have them randomly switch places.<br />Press T to split them into 3 groups that will move to a target on the opposite side of the circle.<br />Press G to split them into two groups. They will organize into block formations.<br />Press B to have them exchange sides.<br />Press P to organize them into two vertical lines.<br />Press B to have them invert the line and switch sides.<br /><br />The entities turn red after a collision, until they reach their target.Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-43486219509086885142014-11-19T13:33:00.004-08:002014-11-19T13:35:54.828-08:00Point A to Point B: part 4We can now move fluidly around <a href="http://codeandcompose.blogspot.com/2014/11/point-to-point-b-part-3.html">groups of objects</a>. Amazing! On to animation and sound!<br /><br />Hold your horses! One more problem to solve.<br /><br />What if the obstacle we want to avoid is moving? I didn't realize this was a problem until THIS happened.<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-RkxkpaBtv4o/VGuSYtoy6yI/AAAAAAAAAOU/UjHXt1p2v48/s1600/VG-crossFail.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-RkxkpaBtv4o/VGuSYtoy6yI/AAAAAAAAAOU/UjHXt1p2v48/s1600/VG-crossFail.gif" /></a></div><br />Each character is heading towards a different target, and since their paths cross, they get "locked" with each other. It's a classic "who goes first?" routine.<br /><i><br /></i><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> After you...</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Costello:<i> Right, after Yu...</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> No, I insist, after you!</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Costello:<i> That's what I said. After Yu!</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> After me?</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Costello:<i> No. Yu, then Mi!</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> Right. Me, then you...</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Costello:<i> NO! Yu before Mi. Then Hoo.</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> I don't know. Who is there?</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Costello:<i> Hoo is already here.</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> Just me and you. But who is going first?</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Costello:<i> No. First Yu, then Mi, then Hoo.</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> I don't know, but I'm going anyway!</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Costello:<i> You can't! Not until Yu goes.</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> I'll goes when I wants to goes!</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Yu:<i> Not yet! Wait for Mi!</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> Who's that?</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Costello:<i> That's Yu!</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> How can he be me?</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Yu:<i> I'm not Mi. I'm YU!</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> Then who am I?!</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Costello:<i> Oh, hello, Mi!</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Mi:<i> Hello, Mr. Costello!</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Costello:<i> Yu, Mi, Mr. Abbott. (introducing them)</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Mi:<i> Hello. I'm Mi. Nice to meet you!</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> Well at least you know who you are! Now who are YOU?</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Hoo:<i> I am Hoo.</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> That's what I asked you.</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Costello:<i> Well, we better get going. After you. (indicating Yu)</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;"><br /></span><span style="font-family: Georgia,"Times New Roman",serif;"><i>Abbott takes a step towards the door at the same time as Yu. He has to stop to let Yu go. The same happens for Mi and Hoo. Abbott glares at Costello to let him go. Neither goes.</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;"><br /></span><span style="font-family: Georgia,"Times New Roman",serif;">Costello: <i>After you...</i></span><br /><span style="font-family: Georgia,"Times New Roman",serif;">Abbott:<i> Just GO ALREADY!</i></span><br /><br /><h3>Path Crossing and Look-ahead</h3>This problem took a lot more forethought than the others. I decided to take each character and assess the information he has available from his own point of view. That way his decision making process will be able to mimic that of people walking around. Here's what you know:<br /><br /> - Your own target<br /> - Your direction and speed<br /> - The direction and speed of the "obstacle"<br /><br />What you don't necessarily know is the target of the obstacle. But this should be enough information to make a relatively informed choice. So the question is, when do we adjust our path, and when don't we.<br /><br />As people, if we see someone about to cross our path, we can judge our relative speeds and make an assumption about the likelihood of a collision at the point of crossing. The farther ahead we look though, the less accurate our prediction.<br /><br />If we look ahead 40 animation frames, we can tell if our current trajectories cross, and if the obstacle's trajectory crosses our desired path to the target. By judging the speed of each object, we can tell who will hit the point of intersection first. We'll be polite and let whoever gets there first pass.<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://1.bp.blogspot.com/-7RTva7TcH3w/VGz7VrYatWI/AAAAAAAAAO8/LSkKPShGWvM/s1600/crosspath.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-7RTva7TcH3w/VGz7VrYatWI/AAAAAAAAAO8/LSkKPShGWvM/s1600/crosspath.jpg" /></a></div>The black dashed lines indicate each object's desired path. The dotted lines represent their current trajectory. Since that is the only information BOTH parties can anticipate, we'll use it to determine who gets the right-of-way. The green dot will reach the point of intersection one frame sooner than the orange dot, so he gets to keep his current trajectory. So what do we do to adjust the direction of the orange dot?<br /><br />My first thought was to have him "chase" the other, by flipping the red vector 180 degrees. That proved not to work well. Instead, I found that mirroring the red vector around the desired path as an axis got the desired result.<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-IcNAisTq27Y/VG0AHwcm6JI/AAAAAAAAAPY/rwoyy4IivEU/s1600/crossmirror.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-IcNAisTq27Y/VG0AHwcm6JI/AAAAAAAAAPY/rwoyy4IivEU/s1600/crossmirror.jpg" /></a></div>As you can see, by flipping the direction around the desired path as an axis (black dashes), it drastically changes his direction (red become dotted red). Keep in mind, the red vector is added back to the desired vector to produce the final direction. This works great to avoid a potential train wreck in scenarios like this. However there are some scenarios where we want to ignore a moving obstacle all together, like chasing.<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-e8GgcrAeuG0/VGz7YlxvHoI/AAAAAAAAAPE/0VpFS5w0IZs/s1600/crosschase.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-e8GgcrAeuG0/VGz7YlxvHoI/AAAAAAAAAPE/0VpFS5w0IZs/s1600/crosschase.jpg" /></a></div>Everyone loves a good chase. But if we have no chance of catching the obstacle we are following, then we might as well ignore its anti-gravity vector completely. What about a good ol' fashioned game of chicken?<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-MzTw9wXdZHc/VGz7YynuyQI/AAAAAAAAAPI/yxuf4p6lQBQ/s1600/crossheadon.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-MzTw9wXdZHc/VGz7YynuyQI/AAAAAAAAAPI/yxuf4p6lQBQ/s1600/crossheadon.jpg" /></a></div><br />Naturally we don't want a crash. Fortunately, the normal collision avoidance algorithm takes care of this. And with both objects trying to avoid each other, they do it quite effectively.<br /><br />Here was my first attempt at making it all work.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://1.bp.blogspot.com/-9CwLBdeY9zU/VGwomvbygaI/AAAAAAAAAOk/ejnUdfSGAjg/s1600/VG-cross1.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-9CwLBdeY9zU/VGwomvbygaI/AAAAAAAAAOk/ejnUdfSGAjg/s1600/VG-cross1.gif" /></a></div><br />Naturally, we need to scale down the adjustments as we near the time of intersection. This makes for a smooth transition out of the adjustment and back to normal collision avoidance. This is done by using ratios created with how close teach party is to the point of intersection.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-HVrGqC3un_U/VGwom9bymAI/AAAAAAAAAOo/jV27Gr0i6ck/s1600/VG-crossGoods.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-HVrGqC3un_U/VGwom9bymAI/AAAAAAAAAOo/jV27Gr0i6ck/s1600/VG-crossGoods.gif" /></a></div> <br />There's a few bits of fancy math going on in this algorithm, starting with the calculation to find the time/point of intersect between the estimated trajectories. This handy function takes two line segments as arguments and returns the point of intersection (or false for no intersection).<br /><br /><pre class="prettyprint">function pathIntersect(p1a,p1b,p2a,p2b){<br /> //input 4 points as arguments, 2 line segments<br /> //returns the intersect point if they collide or false if they do not<br /> var s1_x, s1_y, s2_x, s2_y,ix,iy;<br /> var s1_x = p1b.x - p1a.x;<br /> var s1_y = p1b.y - p1a.y;<br /> var s2_x = p2b.x - p2a.x;<br /> var s2_y = p2b.y - p2a.y;<br /><br /> var s = (-s1_y * (p1a.x-p2a.x) + s1_x * (p1a.y-p2a.y))/(-s2_x * s1_y + s1_x * s2_y);<br /> var t = ( s2_x * (p1a.y-p2a.y) - s2_y * (p1a.x-p2a.x))/(-s2_x * s1_y + s1_x * s2_y);<br /><br /> if (s >= 0 && s <= 1 && t >= 0 && t <= 1){<br /> // Collision detected, save the intersection point if necessary<br /> var ix = p1a.x + (t * s1_x);<br /> var iy = p1a.y + (t * s1_y);<br /> return {x:ix,y:iy};<br /> }<br /> return false; // No collision<br />}</pre>Did you notice in the last gif that the vectors turning "D" are practically pointing in the wrong direction? The adjustment I found most useful was to rotate the vectors around the path, like a mirror image. Here's the handy function for that. It mirrors mvec around vec as an axis.<br /><br /><pre class="prettyprint">function mirrorVector(vec,mvec){ <br />// mirrors mvec around vec as an axis<br /> //get a line for vec through origin:<br /> //y = vec.vy/vec.vx * x<br /> //get a perpendicular line through mvec:<br /> //y = (vec.vx/vec.vy) * x + (mvec.vy - ((vec.vx/vec.vy)*mvec.vx))<br /> //find the intersect point<br /> var iy = (mvec.vy + (vec.vx / vec.vy * mvec.vx)) / (1 + ((vec.vx*vec.vx)/(vec.vy*vec.vy)));<br /> var ix = vec.vx / vec.vy * iy;<br /> var newx = 2 * (ix - mvec.vx) + mvec.vx;<br /> var newy = 2 * (iy - mvec.vy) + mvec.vy;<br /> return {"vx":newx,"vy":newy};<br />}</pre>In addition to these helpers, we must insert this code in the original "move" function.<br /><br /><pre class="prettyprint">av.vx = Math.cos(aTheta)*mag;<br />av.vy = Math.sin(aTheta)*mag;<br /><br />//place this code after the above lines from part 3.<br /> <br />////////experimental adjustment for crossing paths////////<br />var d40 = getVecLength(character.vel) * 40 + (character.w/2);//adjust this line!!!<br />// distance to obstacle must be less than velocity x 40 frames <br />// delta must be between -90 and 90<br />// other guy must be moving, not stationary<br />if (d < d40 && dx > -Math.PI/2 && dx < Math.PI/2 && Obstacles[o].isMoving()) {<br /> var vv = crossPathFix(d40,d,vector,v,av,character,Obstacles[o]);<br /> v.vx = vv.v.vx;<br /> v.vy = vv.v.vy;<br /> av.vx = vv.av.vx;<br /> av.vy = vv.av.vy;<br />}<br /><br />///////////End of experimental adjustment/////////////////<br /></pre>Here is the code for the fix. There are a few more helper functions at the bottom. <br /><br /><pre class="prettyprint">function crossPathFix (d40,dis,vector,v,av,guy1,guy2){<br /> //distance in 40 frames depending on current speed<br /> //distance to path crosser<br /> //desired vector<br /> //angle of path crosser<br /> //adjusted angle<br />//make line segments from current positions to anticipated position in 40 frames<br /> var g1 = guy1.lookAhead(vector);<br /> var g1v = guy1.lookAhead();<br /> var g2 = guy2.lookAhead();<br /><br /> intersect = pathIntersect(g1.pos,g1.des,g2.pos,g2.des);//desired path and other's path<br /> intersect2 = pathIntersect(g1v.pos,g1v.des,g2.pos,g2.des);//current path and other's path<br /> ctx.strokeStyle = "hsl(30,50%,50%)"; <br /> ctx.beginPath();<br /> ctx.moveTo(g1.pos.x,g1.pos.y);<br /> ctx.lineTo(g1.des.x,g1.des.y);<br /> ctx.stroke();<br /> ctx.strokeStyle = "hsl(30,20%,20%)";<br /> ctx.beginPath();<br /> ctx.moveTo(g2.pos.x,g2.pos.y);<br /> ctx.lineTo(g2.des.x,g2.des.y);<br /> ctx.stroke();<br /> if (!intersect) {//<br /> var ratio1 = (1-(1.25*dis/d40))*(1-(1.25*dis/d40));<br /> if (dis/d40 > 0.8) ratio1 = 0;<br /> //account for the angle of direction too...are they heading toward or away from one another?<br /> var ng1 = normalize(guy1.vel);<br /> var ng2 = normalize(guy2.vel);<br /> var ratiod = -(((ng1.vx + ng2.vx) * (ng1.vx + ng2.vx)) + ((ng1.vy + ng2.vy) * (ng1.vy + ng2.vy)))/2 + 1;//between -1 and 1<br /> if (ratiod < 0) ratiod = 0;<br /> ratiod += ratio1;<br /> return {"v":{"vx":v.vx * ratiod,"vy":v.vy * ratiod},<br /> "av":{"vx":av.vx * ratiod,"vy":av.vy * ratiod}};<br /> }<br /> //intersect2 is used to determine who should go first<br /> if (intersect2) {//the current paths also intersect<br /> //number of frames until intersect<br /> var t1 = getDistance(intersect2,g1.pos) / guy1.speed;<br /> var t2 = getDistance(intersect2,g2.pos) / guy2.speed;<br /><br /> } else {<br /> //number of frames until intersect<br /> var t1 = getDistance(intersect,g1.pos) / guy1.speed;<br /> var t2 = getDistance(intersect,g2.pos) / guy2.speed;<br /> //if (t2 > t1 || t1 < 1) return {"v":v,"av":av};<br /> }<br /><br /> //no adjustment if t1 < 1... nearly crossing the path<br /> // guy1 will cross the path first, no adjustment<br /> if (t2 > t1 || t1 < 1) return {"v":v,"av":av};<br /><br /> var ratio = t2/t1;//between 0 and 1<br /> var ratio1 = (1-(1.25*dis/d40))*(1-(1.25*dis/d40));<br /> if (dis/d40 > 0.8) ratio1 = 0;<br /> var ratio2 = 3;// * ((d40 - (t1 * guy1.speed)) / d40 + 1);//should yield ratio between 2 and 4<br /> if (t2 - t1 > 50) ratio2 = ratio2 * (1 - ((t2 - t1 - 50) / 50));<br /> if (ratio2 < 0) ratio2 = 0;<br /> if (t1 > 80) ratio2 = ratio2 * (120-t1)/40;<br /> //another adjustment to help ease the transition once their path is almost crossed<br /> if (t2 < 20 && !intersect2) ratio2 = ratio2 * (t2 / 20) * ratio;<br /><br /> ratio2 += ratio1;<br /> <br /> var newv = mirrorVector(vector,v)<br /> var newav = mirrorVector(vector,av);<br /> return {"v":{"vx":newv.vx * ratio2,"vy":newv.vy * ratio2},<br /> "av":{"vx":newav.vx * ratio2,"vy":newav.vy * ratio2}}; <br />}<br /></pre>Helper functions and object constructor...<br /><br /><pre class="prettyprint">function getDistance(a,b){<br />//provided the x.y position of two objects, find the distance,<br />//cx and cy represent the center<br /> if (a.cx && b.cx){<br /> var x = b.cx - a.cx;<br /> var y = b.cy - a.cy;<br /> } else {<br /> var x = b.x - a.x;<br /> var y = b.y - a.y;<br /> }<br /> var d = Math.sqrt((x*x)+(y*y));<br /> return d;<br />}<br /><br />///this goes in the constructor for each Obstacles object<br /> this.lookAhead = function(vector){<br /> if (vector) {<br /> var nx = vector.vx * 120 * this.speed + this.pos.cx;<br /> var ny = vector.vy * 120 * this.speed + this.pos.cy;<br /> } else {<br /> var nx = this.vel.vx * 120 + this.pos.cx;<br /> var ny = this.vel.vy * 120 + this.pos.cy;<br /> }<br /> return {"pos":{"x":this.pos.cx,"y":this.pos.cy},"des":{"x":nx,"y":ny}};<br /> //position and destination coordinates<br /> };</pre><br />There it is. My complete algorithm to avoid obstacles! (Satisfaction not guaranteed).<br /><br /> I intend to make a simple web app to demonstrate with full script available for download. Then I can test it out in extreme circumstances and see how it holds up. For the purposes of the game, there will probably be a maximum of 12 characters on the screen at once.<br /><br />Stay tuned for a related issue: collision detection! Even with a great avoidance algorithm, collisions are inevitable. And frankly, they make games fun!Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-47154214510993935642014-11-17T21:53:00.004-08:002014-11-19T13:35:27.975-08:00Point A to Point B: part 3The problem solving continues. In <a href="http://codeandcompose.blogspot.com/2014/11/point-to-point-b-part-1.html">part 1</a> we explored how to avoid objects in front of us, but not behind us. In <a href="http://codeandcompose.blogspot.com/2014/11/point-to-point-b-part-2.html">part 2</a> we solved the issue of avoiding objects directly in our path.<br /><br />Now what's wrong? THIS:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-9kuLmZamgeI/VGgdybXWuzI/AAAAAAAAANA/YGw_yFi7Sqg/s1600/VG-2obsFail.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-9kuLmZamgeI/VGgdybXWuzI/AAAAAAAAANA/YGw_yFi7Sqg/s1600/VG-2obsFail.gif" /></a></div><br />If you look carefully, there are two characters in the way, "Mickey" and "Talan". Since the art is so bad (sorry!) it's hard to tell, but they are perfectly the same distance from Shytor's path to the enemy, Ultros.<br /><br />Didn't we already solve this? Well, no. We set the algorithm to adjust for something directly in our path, but not for two items creating equal but opposing anti-gravity forces.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-TbbN25f-3dA/VGggbU_tDqI/AAAAAAAAANM/0AHeB_Erll8/s1600/cancelout.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-TbbN25f-3dA/VGggbU_tDqI/AAAAAAAAANM/0AHeB_Erll8/s1600/cancelout.jpg" /></a></div>Whether we use the direct anti-gravity force vectors (red lines) or the adjusted ones (blue lines) they cancel each other out because the obstacles are <i>exactly</i> the same distance from the path. While this may be a rare circumstance, we have to consider the implications. When faced with multiple objects to avoid, how do we choose which way to go?<br /><br />The answer was actually a bit more simple than I originally thought. All we have to do is add the original anti-gravity vectors <i>first, </i>and then adjust the remaining vector, as if it was one obstacle to avoid.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-v0u9wFMMk-8/VGgiIOuZa8I/AAAAAAAAANY/DgYdWKyKqw8/s1600/canceloutfix.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-v0u9wFMMk-8/VGgiIOuZa8I/AAAAAAAAANY/DgYdWKyKqw8/s1600/canceloutfix.jpg" /></a></div>The yellow line represents the sum of the original anti-gravity vectors (red). The green line represents the vector after the adjustment. Obviously we choose to go to the right, because that's the side of the road you should drive on (sorry British colonies!).<br /><br />Now, no matter how many obstacles there are, we end up with two vectors to make our choice: first the sum of all the adjusted vectors, and the adjusted vector of the sum of the original anti-gravity vectors. Follow?<br /><br />The sum of the adjusted vectors line is not visualized in the picture above. If it was, it would be a short vector pointing in the same direction as the yellow line.<br /><br />But out of those two vectors, which do we use? In this scenario, we want to use "green", but in others we might want to use our previous algorithm. Here's a basic description of how it works.<br /><br />Let's call the sum of adjusted vectors "B" (sum of the blue lines above) and the adjusted sum of vectors "A" (green line above). If we add them together, we get a new vector, X. Then we find the difference between this new vector and the angle to the target: we'll call it "delta". If delta is near 180, then we want to ignore either A or B. We can defer to A or B depending on the situation, if either vector is near 180 degrees from the target angle, we want to use the other. I used the following equation to scale the deferral smoothly (it's a quarter ellipse since "delta" is less than 180 degrees). We end up with a value between 0 and 1 that is multiplied by the B vector.<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-SiPByh7kjS4/VGgyn0DXmpI/AAAAAAAAAN0/FSTPqVvCNUc/s1600/Screen%2BShot%2B2014-11-16%2Bat%2B12.10.39%2BAM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-SiPByh7kjS4/VGgyn0DXmpI/AAAAAAAAAN0/FSTPqVvCNUc/s1600/Screen%2BShot%2B2014-11-16%2Bat%2B12.10.39%2BAM.png" /></a></div>In code:<br /><br /><pre class="prettyprint">if (delta != 0) igFac = Math.sqrt(1 - Math.pow(((2 * delta) - Math.PI) / Math.PI,2));<br />else igFac = 0;<br /><br />vector.vx += A.vx + (igFac*B.vx);<br />vector.vy += A.vy + (igFac*B.vy);<br /></pre><br />It could also be a linear scale to avoid using the costly Math.sqrt() function more than necessary. However, computers are getting faster, and I like smooth transitions. The result is this:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-8ZpeRShFR5I/VGgkNHd5yPI/AAAAAAAAANk/2dq2pH2JjZo/s1600/VG-2obsGood.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-8ZpeRShFR5I/VGgkNHd5yPI/AAAAAAAAANk/2dq2pH2JjZo/s1600/VG-2obsGood.gif" /></a></div><br />Notice that once he makes a choice to go right, the normal algorithm takes over since both obstacles are now to his left. Problem solved! What else could go wrong?<br /><br />THIS:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://1.bp.blogspot.com/-JoUBU4FRSeU/VGjPNcpRnNI/AAAAAAAAAOE/lR61K86_-1Q/s1600/VG-superfail.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-JoUBU4FRSeU/VGjPNcpRnNI/AAAAAAAAAOE/lR61K86_-1Q/s1600/VG-superfail.gif" /></a></div><br />This particular issue was actually a simple math error, but you get the idea. Here's the code.<br /><br /><pre class="prettyprint">function moveGuy(character){<br /> //sanity check, no movement if there is no target<br /> if (!character.target) return;<br /> <br /> //find the vector to the target and normalize <br /> //then convert it to an angle in radians<br /> //the "pos" property contains x,y coordinates <br /> var vector = normVec(character.pos,character.target.pos);<br /> var angle = Math.atan2(vector.vy,vector.vx);<br /> <br /> //declarations<br /> var d = 0;<br /> var delta = 0;<br /> var r = 0;<br /> //this is the sum of the anti-gravity, yellow line<br /> var AGSumx = 0;<br /> var AGSumy = 0;<br /> //this is the sum of the adjusted vectors, blue line<br /> var AdjAGSumx = 0;<br /> var AdjAGSumy = 0;<br /><br /> //"Obstacles" is a dictionary of all obstacles to avoid<br /> for (var o in Obstacles){ <br /> if (o === character.name) continue; //skip yourself<br /> if (o === character.target.name) continue;//skip your target<br /> //get the distance to the obstacle<br /> d = getDistance(character.pos,Obstacles[o].pos);<br /> <br /> //calculate the anti-gravity magnitude<br /> //the halo.cr property is the width with a buffer<br /> var mass = (character.halo.cr + Obstacles[o].halo.cr);<br /> //multiply by "personal space" constant for math fudging<br /> //this effects the strength of the antigravity<br /> var mass = mass * mass * 3;<br /> var mag = mass / (d * d);<br /> //v is anti-gravity vector (red)<br /> var v = normVec(Obstacles[o].pos,character.pos,mag);<br /> var av = {"vx":0,"vy":0}; //av is adjusted vector (blue)<br /> <br /> //angle for the red lines<br /> var vTheta = Math.atan2(v.vy,v.vx);<br /> var obsAngle = 0;//angle to the obstacle<br /> if (vTheta >= 0) obsAngle = vTheta - Math.PI;<br /> else if (vTheta < 0) obsAngle = vTheta + Math.PI;<br /><br /> //get the difference between the angle to target and obstacle<br /> //correct it be between -180 and 180<br /> delta = obsAngle - angle;<br /> if (delta > Math.PI) delta = delta - (2*Math.PI);<br /> if (delta < -Math.PI) delta = delta + (2*Math.PI);<br /> //magnitude of the force is scaled based on direction<br /> r = (1 + Math.cos(delta))/2; //unit cardioid<br /> r = r * r * r;<br /> <br /><br /> //get the difference between the target vector and antigravity vector<br /> delta = vTheta - angle;<br /> if (delta > Math.PI) delta = delta - (2*Math.PI);<br /> if (delta < -Math.PI) delta = delta + (2*Math.PI);<br /><br /> //make the adjustment to get the blue lines<br /> if (delta != 0) {<br /> if (Math.abs(delta)>=Math.PI/2)var r2 = 1 - Math.sqrt(1 - Math.pow(((2 * Math.abs(delta)) - Math.PI) / Math.PI,2));//inverted quarter elipse<br /> else {var r2 = 0;}// if delta > 90 else 0<br /> var theta = Math.PI*r*r2/2;<br /> //one method of correcting the sign if the angles are negative<br /> var dir = Math.abs(delta)/delta;<br /> var aTheta = vTheta - (theta * dir);<br /> } else {<br /> var aTheta = vTheta;<br /> }<br /> <br /> //convert the blue line angle to a vector<br /> av.vx = Math.cos(aTheta)*mag;<br /> av.vy = Math.sin(aTheta)*mag;<br /> <br /> AGSumx += v.vx*r;//sum of red vectors (yellow) <br /> AGSumy += v.vy*r;<br /><br /> AdjAGSumx += av.vx*r;//sum of blue vectors<br /> AdjAGSumy += av.vy*r;<br /> }//end for loop<br /> <br /> //to fix the splitting issue, choose a direction.<br /> //this algorithm has to choose which of the vectors to use<br /> //so it's a bit more complex.<br /> //basically it scales vectors to 0 based on their direction relative to the target<br /> <br /> //magold is mag of yellow, magnew is mag of sum of blue<br /> var magold = Math.sqrt((AGSumx*AGSumx)+(AGSumy*AGSumy));<br /> var magnew = Math.sqrt((AdjAGSumx*AdjAGSumx)+(AdjAGSumy*AdjAGSumy));<br /> var newx = 0;//placeholder for the adjusted anti-gravity sum (green)<br /> var newy = 0;<br /> //only adjust the yellow if the magnitude is greater than the sum of blue<br /> if (magold >= magnew){<br /> //convert the vector ratio to an angle, between 90 and 0<br /> var newTheta = -(1-(magnew/magold))*(1/(magnew+1))*(Math.PI/2);<br /> //find the difference between the old vector and the target vector<br /> //is it between 90 and 180?<br /> var oldVangle = Math.atan2(AGSumy,AGSumx);//yellow line<br /> delta = oldVangle - angle;//diff from target vector to yellow<br /> if (delta > Math.PI) delta = delta - (2*Math.PI);<br /> if (delta < -Math.PI) delta = delta + (2*Math.PI);<br /> //translate dTheta from between 90 and 180 to a ratio<br /> if (Math.abs(delta) > Math.PI/2) {<br /> //linear scaling<br /> var axxx = (Math.abs(delta) - (Math.PI/2))/(Math.PI/2);<br /> /square and give it a sign<br /> axxx = axxx * axxx * (delta/Math.abs(delta));/<br /> } else { axxx = 0;<br /> }<br /> <br /> var finalAngle = newTheta * axxx;<br /> <br /> //calculate the adjustment, this is the green line<br /> newx = AGSumx*Math.cos(finalAngle) - AGSumy*Math.sin(finalAngle);<br /> newy = AGSumy*Math.cos(finalAngle) + AGSumx*Math.sin(finalAngle);<br /> newx *= 1 - (magnew/magold);//adjust magnitude based on inverted mag ratio<br /> newy *= 1 - (magnew/magold);<br /> newx *= 1/(magnew + 1);<br /> newy *= 1/(magnew + 1);<br /> newx *= Math.abs(axxx);//if the old vector isn't near 180, don't add it<br /> newy *= Math.abs(axxx);<br /> }<br /> <br /> //this scales out the sum of adjusted vectors<br /> //first get the sum of both the adjusted vectors<br /> var igAng = Math.atan2(newy + AdjAGSumy,newx + AdjAGSumx);<br /> //find the difference between this combined vector and the target vector<br /> delta = Math.abs(igAng - angle);<br /> if (delta > Math.PI) delta = (2*Math.PI) - delta;<br /> //if it's near 180 degrees from the target vector, we don't use it<br /> //the sum of blue will be fine<br /> if (delta != 0) var igFac = Math.sqrt(1 - Math.pow(((2 * delta) - Math.PI) / Math.PI,2));//quarter ellipse equation<br /> else var igFac = 0; <br /><br /> //the movement vector is green vector + sum of blue vectors scaled<br /> vector.vx += newx + (igFac*AdjAGSumx);<br /> vector.vy += newy + (igFac*AdjAGSumy);<br /> //normalize the vector, so it can be used for movement<br /> vector = normalize(vector);<br /> //set the movement vector for next frame<br /> //then move on to the next character<br /> character.nextmove(vector);<br />}</pre><a href="http://codeandcompose.blogspot.com/2014/11/point-to-point-b-part-4.html">Part 4</a> comes with the thrilling conclusion and something I initially failed to consider at all: moving obstacles!Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-32321396998281551342014-11-13T20:36:00.005-08:002014-11-19T13:44:15.310-08:00Point A to Point B: part 2Okay. So we <a href="http://codeandcompose.blogspot.com/2014/11/point-to-point-b-part-1.html">solved the issue of optimizing our anti-gravity obstacle avoidance algorithm by using a cardioid</a>. What problem could possibly be next?<br /><br />Well, how about, what happens if the obstacle is <i>straight ahead</i>?<br /><br /><h3>Choosing Left or Right</h3><br />It is possible, and even likely, that the obstacle will be <i>directly</i> in the path of the character. So if anti-gravity of the obstacle is pushing in the exact opposite direction of the movement vector, what happens when you get too close to the obstacle?<br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-wa9vs5NTdtc/VGV7WNePQAI/AAAAAAAAAKo/fhvl68sf15I/s1600/direct.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-wa9vs5NTdtc/VGV7WNePQAI/AAAAAAAAAKo/fhvl68sf15I/s1600/direct.jpg" /></a></div>The forces eventually cancel out, or it's possible that the anti-gravity will exceed the movement vector. Normally the obstacle is outside the direct path, and the character gets nudged more and more as he moves closer, eventually rolling around. But with perfect math in a digital game, THIS is possible.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-F2FF3mgi7yM/VGV77pNfBoI/AAAAAAAAAKw/5pPQ91KusnA/s1600/VG-1obsFail.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://2.bp.blogspot.com/-F2FF3mgi7yM/VGV77pNfBoI/AAAAAAAAAKw/5pPQ91KusnA/s1600/VG-1obsFail.gif" /></a></div><br />The solution is simple to you or me... just pick a direction and go around, duh?! But computers aren't so clever... they need to be told what to choose.<br /><br />In this perfect scenario, the angle of anti-gravity is exactly 180 from the angle of the path. Ideally, this is when we want to adjust our path the most, since there is something REALLY in our way. So, let's just adjust the angle of anti-gravity by 90 degrees!<br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/--gAr68Ij4DQ/VGV-cfQHU1I/AAAAAAAAAK8/nbClV8NUrsE/s1600/adjust1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://2.bp.blogspot.com/--gAr68Ij4DQ/VGV-cfQHU1I/AAAAAAAAAK8/nbClV8NUrsE/s1600/adjust1.jpg" /></a></div>The red line is the initial anti-gravity vector, the blue line is the new adjusted version.<br /><br />Done. Solved! Wait, but now we are making a huge adjustment. As soon as the obstacle is not directly in our path (probably after one frame) we'll only be making the same small adjustment... so let's scale the adjustment. If the angle of anti-gravity is off by 180 degrees, we adjust a lot, if it is off by 90 or less, we don't adjust.<br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-s_BAYpSptQc/VGWGUhROYlI/AAAAAAAAAMg/Vu3c6KGFeY0/s1600/adjust2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://1.bp.blogspot.com/-s_BAYpSptQc/VGWGUhROYlI/AAAAAAAAAMg/Vu3c6KGFeY0/s1600/adjust2.jpg" /></a></div><div class="separator" style="clear: both; text-align: center;"></div>Here's the code:<br /><br /><pre class="prettyprint">//get the difference between the path vector and the obstacle<br />//invert it as necessary get a result between -180 and 180<br />delta = vTheta - angle;<br />if (delta > Math.PI) delta = delta - (2*Math.PI);<br />if (delta < -Math.PI) delta = delta + (2*Math.PI);<br /> <br /> if (delta != 0) {<br /> //this creates an inverted quarter ellipse <br /> //that we use to scale the adjustment smoothly<br /> if (Math.abs(delta)>=Math.PI/2)var r2 = 1 - Math.sqrt(1 - Math.pow(((2 * Math.abs(delta)) - Math.PI) / Math.PI,2));<br /> else {var r2 = 0;}// if delta > 90 degrees<br /><br /> var theta = Math.PI*r*r2/2;<br /> //one method of correcting the sign if the angles are negative<br /> var dir = Math.abs(delta)/delta;<br /> var aTheta = vTheta - (theta * dir);<br /> } else {<br /> var aTheta = vTheta;<br /> }<br /><br />//this calculates the vector of the new blue line<br />//as seen in the diagrams above<br />av.vx = Math.cos(aTheta)*mag;<br />av.vy = Math.sin(aTheta)*mag;<br />//then add these to the movement vector! </pre>The result is a nice fluid movement around the object. It even makes the path react a bit sooner, so your character isn't waiting to the last minute to make a reasonable adjustment. In this code, it chooses left (d*mn hippy!).<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-qdEBITvBhGE/VGWCo8qvYlI/AAAAAAAAALQ/jF0fGWSo4-M/s1600/VG-1obsGood.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://1.bp.blogspot.com/-qdEBITvBhGE/VGWCo8qvYlI/AAAAAAAAALQ/jF0fGWSo4-M/s1600/VG-1obsGood.gif" /></a></div><br />Done. Solved! Or not. Stay tuned for <a href="http://codeandcompose.blogspot.com/2014/11/point-to-point-b-part-3.html">part 3</a>, where I discuss another scenario we must overcome. What if there are 2 obstacles perfectly evenly spaced directly in our path?Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0tag:blogger.com,1999:blog-6658844084624700142.post-17546475668489120202014-11-12T12:46:00.002-08:002014-11-19T13:41:53.663-08:00Point A to Point B: part 1Welcome to my very first post! What is this blog about, you ask? Let's get right down to it.<br /><br />I'm a composer, who wants to make a game, or at least a demo. I'll talk more about what my idea is with each post, but for now we'll focus on the basics, since that's about all my programming skills can handle.<br /><br />I'm using javascript as my language for now, but we'll see where this goes.<br /><br />The game will be a top-down strategy/RPG style game. In order to manage a team of characters, they will have to have some kind of individual movement AI. For nearly all the actions, the character will have to move from point A to point B on his/her own. Attacking is the most fun action, so here we go...<br /><h2><span style="font-size: large;">Action one: Attack.</span></h2>You choose a "Hero", then choose an enemy. The Hero walks over, body-slams the sh*t out of the enemy and walks back to his home position. Easy, right? But what if there is something in his way?<br /><br />When I first researched solutions for this simple problem (oh, how naive I was) I first encountered information about path-finding, and A* algorithms. Once my head stopped spinning, I stumbled across a much more elegant solution: <a href="http://buildnewgames.com/vector-field-collision-avoidance/" target="_blank">vector fields</a>.<br /><br />To simplify the explanation, imagine each obstacle has anti-gravity. The closer you get to it, the more it "pushes" you away. This is nice, because as your movement vector pushes you in one direction, the force of the "anti-gravity" pushes you away, and eventually around said obstacle.<br /><br />To illustrate, the green ball is moving to the right. As it approaches the obstacle, the obstacle exerts its "force" and pushes the ball away. As you can see in the exquisite to-scale drawing, the force is stronger based on proximity.<br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/--eCfk9HkzwE/VGWDoyKYkQI/AAAAAAAAALc/m2wIL2Orqyc/s1600/antigravity.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://3.bp.blogspot.com/--eCfk9HkzwE/VGWDoyKYkQI/AAAAAAAAALc/m2wIL2Orqyc/s1600/antigravity.jpg" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"></div><br />Great! Problem solved. NEXT!<br /><br />Or not. Here's the issue (the first of many). The anti-gravity is exerted in all directions equally, which so as the green ball moves past the orange one (to the point where the orange ball is no longer an obstacle) he is STILL affected by its anti-gravity. Now, why would you want to avoid an obstacle you've already passed?<br /><br />Well, some programmers have solved this by using a complex "look-ahead" to see just how in-the-way the obstacle is. But I found a much simpler solution. Math-geeks, prepare yourselves!<br /><h3><span style="font-size: small;">Cardioids</span></h3><span style="font-size: small;">I heart cardioids. No, seriously. If you don't know what one is, plug this equation into a <a href="https://www.desmos.com/calculator" target="_blank">graphing calculator</a>.</span><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-FgqFggXh-Ds/VGWDy6gTxRI/AAAAAAAAALw/gNclWy2xKpM/s1600/Screen%2Bshot%2B2014-11-11%2Bat%2B9.57.04%2BPM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="//2.bp.blogspot.com/-FgqFggXh-Ds/VGWDy6gTxRI/AAAAAAAAALw/gNclWy2xKpM/s1600/Screen%2Bshot%2B2014-11-11%2Bat%2B9.57.04%2BPM.png" /></a></div><div class="separator" style="clear: both; text-align: center;"></div><span style="font-size: small;">Imagine that normal anti-gravity is calculated in a circle (i.e. objects in all directions are treated equally) so why not use a cardioid? This allows us to calculate 100% anti-gravity on objects directly in our path, and scale to 0% for objects behind us. And multiplying the cardioid by a power increases the effect. These images show the circle vs. cardioid being applied to the moving player, aimed in the direction of the target.</span><br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-u4XFwxNvgps/VGWDyvCfQcI/AAAAAAAAAMQ/BgBqMzJHiYo/s1600/Screen%2Bshot%2B2014-11-11%2Bat%2B9.22.46%2BPM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://4.bp.blogspot.com/-u4XFwxNvgps/VGWDyvCfQcI/AAAAAAAAAMQ/BgBqMzJHiYo/s1600/Screen%2Bshot%2B2014-11-11%2Bat%2B9.22.46%2BPM.png" /></a></div><div class="separator" style="clear: both; text-align: center;"></div>"Shytor" is moving to the left. "M" is at 45 degree angle to his motion, so is sort of in the way. The colored lines represent the forces exerted on him due to anti-gravity. His resulting vector pushes him down a little, to avoid M.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-LdwaLTiUeuM/VGWDyQKOxdI/AAAAAAAAALs/phdPP2XZ9Pc/s1600/Screen%2Bshot%2B2014-11-11%2Bat%2B9.21.20%2BPM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://1.bp.blogspot.com/-LdwaLTiUeuM/VGWDyQKOxdI/AAAAAAAAALs/phdPP2XZ9Pc/s1600/Screen%2Bshot%2B2014-11-11%2Bat%2B9.21.20%2BPM.png" /></a></div><div class="separator" style="clear: both; text-align: center;"></div><span style="font-size: small;">Here, "Shytor" and "D" are moving toward their target, "Ultros". The colored lines protruding from Shytor represent the forces of anti-gravity exerted on him (I'll explain those in detail later). Both M and D are in his way (a little). D has nothing directly in his way, so there are no colored lines (very tiny ones). Without the cardioid, D would be more affected by M, but due to the scaling, the effect is negligible.</span><br /><span style="font-size: small;"><br /></span><span style="font-size: small;">The result is a change in overall path. Notice the "circle" path continues to push away from orange ball, even after it is passed. Whereas, the cardioid, straightens out once it is clear. The perfectly to-scale anti-gravity vector lines (in red) show how the effect diminishes greatly as the green ball passes its target.</span><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-uklBk-EFHb4/VGWDy3gsZdI/AAAAAAAAAL0/rTyaUgWrKAM/s1600/cardioid.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-uklBk-EFHb4/VGWDy3gsZdI/AAAAAAAAAL0/rTyaUgWrKAM/s1600/cardioid.jpg" /></a></div><div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"></div><span style="font-size: small;"><br /></span>So that's the explanation. SHOW ME THE CODE!<br /><br />This code would run each frame, and can be looped if there are multiple obstacles present. The "orange" object is the obstacle. <br /><br /><pre class="prettyprint">function moveObject(green,orange){<br />//first get the normalized vector of green's target <br />//(i.e. which direction is he trying to move)<br />//this returns vector object with vx and vy properties<br /> var vector = normVec(green.pos,green.target.pos);<br />//convert the vector to an angle in radians, 0 is East<br /> var angle = Math.atan2(vector.vy,vector.vx);<br /><br />//get the distance to the obstacle<br /> d = getDistance(green.pos,orange.pos);<br /><br />//calculate the anti-gravity magnitude based on distance<br />//it's not really the mass, but the "width" of the obstacle<br /> var mass = (green.radius + orange.radius);<br />//multiply by "personal space" constant for math fudging<br />//this adjusts the strength of the anti-gravity<br /> var mass = mass * mass * 2;<br />//the magnitude of the effect as distance approaches the "mass" is 1 <br /> var mag = mass / (d * d);<br /><br />//find the angle between the two objects (as an "x, y" vector)<br />//multiplying by the magnitude<br /> var v = normVec(orange.pos,green.pos,mag);<br />//convert the angle to radians<br /> var vTheta = Math.atan2(v.vy,v.vx);<br />//invert it to get the "anti-gravity force"<br /> var obsAngle = 0;<br /> if (vTheta >= 0) obsAngle = vTheta - Math.PI;<br /> else if (vTheta < 0) obsAngle = vTheta + Math.PI;<br /><br />//get the difference between angles to the target and the obstacle<br /> delta = obsAngle - angle;<br />//invert if more than 180 deg, this keeps the value in a usable range<br /> if (delta > Math.PI) delta = delta - (2*Math.PI);<br />//invert if less than -180<br /> if (delta < -Math.PI) delta = delta + (2*Math.PI);<br /><br />//make a unit cardioid, if the difference in angles is 0 effect is 1<br />//if angle is 180 effect is 0<br /> r = (1 + Math.cos(delta))/2;<br />//multiply the magnitude exponentially (optional)<br /> r = r * r * r;<br /><br />//add the calculated anti-gravity force to the original vector<br />vector.vx += v.vx*r;<br />vector.vy += v.vy*r;<br /><br />//after all anti-gravity is calculated then move the character<br />vector = normalize(vector);//normalize the new movement vector<br />//then call the movement function of the character<br />//basically multiply the direction vector by speed<br />//and move to the next frame<br />green.move(vector);<br />}<br /><br />//helper functions<br /> function getDistance(a,b){<br />//provided the x.y position of two objects, find the distance<br />//cx and cy represent the center<br /> var x = b.cx - a.cx;<br /> var y = b.cy - a.cy;<br /> var d = Math.sqrt((x*x)+(y*y));<br /> return d;<br /> }<br /> function normVec(a,b,mag){<br />//find the direction vector between two points, multiplied by mag<br /> if (!mag) mag = 1;<br /> var x = b.cx - a.cx;<br /> var y = b.cy - a.cy;<br /> var d = Math.sqrt((x*x)+(y*y));<br /> var v = {"vx": x / d * mag,"vy": y / d * mag};<br /> return v;<br /> }<br /> function normalize(v){<br />//normalizes a vector object to 1<br /> var d = Math.sqrt((v.vx*v.vx)+(v.vy*v.vy));<br /> var v = {"vx": v.vx / d,"vy": v.vy / d};<br /> return v;<br /> } </pre><br />Stay tuned for <a href="http://codeandcompose.blogspot.com/2014/11/point-to-point-b-part-2.html">part 2</a> where I solve the next problem: choosing left or right!<br /><br />The tile artwork is from "<span class="Apple-style-span" style="font-style: italic;">Wilderness Tile Set</span>" art by Daniel Cook (<a href="http://lunar.lostgarden.com/labels/free%20game%20graphics.html" target="_blank">Lostgarden.com</a>). Thanks to him for making some great free artwork available! Trevor Gasthttps://plus.google.com/113826309365753218003noreply@blogger.com0