<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The Right Moves : JavaScript</title><link>http://blogs.msdn.com/ben_anderson/archive/tags/JavaScript/default.aspx</link><description>Tags: JavaScript</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Moving the Popfly Game Creator engine from Silverlight 1 to 2</title><link>http://blogs.msdn.com/ben_anderson/archive/2008/12/05/moving-the-popfly-game-creator-engine-from-silverlight-1-to-2.aspx</link><pubDate>Sat, 06 Dec 2008 05:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9180923</guid><dc:creator>Ben Anderson</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/ben_anderson/comments/9180923.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ben_anderson/commentrss.aspx?PostID=9180923</wfw:commentRss><description>&lt;p&gt;In our most recent &lt;a href="http://www.popfly.com" target="_blank" mce_href="http://www.popfly.com"&gt;Popfly&lt;/a&gt; update, we shipped the project I’ve been working on for the last couple months (along with Patrick Wong, Tim Rice and help from the rest of the team), which is a fully backwards compatible game engine, completely rewritten from browser based JavaScript rendering using &lt;a href="http://silverlight.net" mce_href="http://silverlight.net"&gt;Silverlight&lt;/a&gt; 1 to a C# based engine using Silverlight 2.&amp;nbsp; As noted &lt;a href="http://popflyteam.spaces.live.com/blog/cns%2151018025071FD37F%21327.entry" target="_blank" mce_href="http://popflyteam.spaces.live.com/blog/cns!51018025071FD37F!327.entry"&gt;elsewhere&lt;/a&gt;, the new engine is a heck of a lot faster than the old (both due to switching to a compiled language from an interpreted one, and due to many efficiency improvements), but the most impressive thing about the switch in my mind is the fact that it’s as fully backwards compatible as any incremental change we’ve made since the original release.&amp;nbsp; Think about it – the engine’s written in C#, but all the custom code is still in JavaScript.&amp;nbsp; &lt;/p&gt;  &lt;p&gt;That is largely thanks to Patrick’s efforts.&amp;nbsp; In order to keep custom code working, what we did was take the current version of the Silverlight version of the &lt;a href="http://www.codeplex.com/sdlsdk" target="_blank" mce_href="http://www.codeplex.com/sdlsdk"&gt;DLR&lt;/a&gt; (the “Silverlight Dynamic Languages SDK”).&amp;nbsp; We did tweak the JavaScript implementation a bit to make code intended to work with the JavaScript Silverlight interfaces play nice with the managed interfaces, but overall, it was pretty much plug and play aside from all the hard work figuring out a largely undocumented piece of prerelease software :) (I think it’s on version 0.4.0 currently).&amp;nbsp; If you haven’t checked it out, the DLR is a pretty slick piece of technology – despite the fact we’re running a bunch of JavaScript code, it’s still miles and away faster than the old engine which ran on the browser’s JavaScript engine (although I’m sure efforts in IE8, Chrome and Firefox 3.1 will narrow the gap).&lt;/p&gt;  &lt;p&gt;What does this mean for you?&amp;nbsp; If your a Popfly game creator, it means a much faster engine that is more consistent from browser to browser.&amp;nbsp; That means you can add more actors and other craziness to your games.&amp;nbsp; If your a software developer with a lot of code in Silverlight 1?&amp;nbsp; Well, for you I’d say it means you should feel free to move to C# and Silverlight 2.&amp;nbsp; It took me a couple months, largely on my own with about a month of help from Patrick to rewrite a large chunk of code that had been developed over the previous year or so by a team that grew from one to three (and occasionally more) devs.&amp;nbsp; The cost was larger than it might have been because I also used it as an opportunity to re-architect and clean up the game engine, and I had to maintain compatibility with a large and complex set of existing data (the thousands of games already on popfly.com).&amp;nbsp; I would expect that for most other Silverlight applications, the cost would be significantly less.&amp;nbsp; What I got out of the experience was the vastly superior tooling experience of C# and Silverlight 2, a huge perf boost by moving to a compiled language, the maintainability of C#, the chance to follow the computer science adage of "throw one away”, and a much better code browsing and IntelliSense experience (again due to the fact that C# is strongly typed and compiled).&amp;nbsp; My advice if your considering a similar move would be to take a look on what code you’re likely to touch again, and if you can see yourself working on it regularly, or if it has strong perf requirements take the leap.&amp;nbsp; &lt;/p&gt;  &lt;p&gt;If you’d like to hear more about Silverlight 2, working with the DLR, the port, or the engine, let me know in the comments and I can go into more details on the areas that interest you.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9180923" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Popfly+Game+Creator/default.aspx">Popfly Game Creator</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/PGC/default.aspx">PGC</category></item><item><title>Capping your actor’s speed</title><link>http://blogs.msdn.com/ben_anderson/archive/2008/05/07/capping-your-actor-s-speed.aspx</link><pubDate>Thu, 08 May 2008 01:23:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8467525</guid><dc:creator>Ben Anderson</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ben_anderson/comments/8467525.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ben_anderson/commentrss.aspx?PostID=8467525</wfw:commentRss><description>&lt;p&gt;If you’ve been playing around with Popfly Game Creator long enough, you’ve probably noticed that if actors get going fast enough, they can sometimes manage to fly right through a wall.&amp;#160; There are some things you can do to get around this like making your walls extra thick, or making sure your actors go slow, but I’ll be the first to admit that this can get kind of annoying.&amp;#160; &lt;/p&gt;  &lt;p align="center"&gt;&lt;img src="http://blogs.msdn.com/photos/ben_anderson/images/8467216/original.aspx" /&gt; &lt;/p&gt;  &lt;p&gt;The issue is that with the alpha release of PGC, we only check for collisions once during each frame.&amp;#160; If your actor is moving so fast that one frame it’s on the left side of the wall and on the next it’s on the right, we will never know that in between it should have collided.&amp;#160; We have to actually catch the actors colliding during a frame in order to realize they collided and then resolve the collision.&amp;#160; This was due to both time limitations and the performance limitations of running on the browser’s JavaScript engine (we could probably do it, but given our short timeframe before releasing the Alpha, we chose to go with the “good enough” method you see in the Alpha) since doing continuous collision checking is both harder to code and less performant [yes, I know this is &lt;a href="http://weblogs.asp.net/jgalloway/archive/2007/05/10/performant-isn-t-a-word.aspx"&gt;not&lt;/a&gt; a &lt;a href="http://boulter.com/blog/2004/08/19/performant-is-not-a-word/"&gt;word&lt;/a&gt; that &lt;a href="http://dictionary.reference.com/browse/performant"&gt;means&lt;/a&gt; &lt;a href="http://dictionary.oed.com/cgi/entry/30003374?single=1&amp;amp;query_type=word&amp;amp;queryword=performant&amp;amp;first=1&amp;amp;max_to_show=10"&gt;fast&lt;/a&gt; – but dude, English is a living language] due to all the math.&lt;/p&gt;  &lt;p&gt;We’ll probably do something about this down the road sometime after Silverlight 2.0 RTMs, we begin requiring it and can begin using C# (no promises as usual).&amp;#160; But in the meantime, I figured I’d post some quick custom code which you can help dealing with this by capping the speed of your actor.&lt;/p&gt;  &lt;p&gt;You might think that you can just use the “Max Speed” value in the motion dialog and be done, however there are many cases where this isn’t good enough.&amp;#160; While Max Speed does work, it only concerns itself with the current behavior.&amp;#160; What Max Speed does under the covers is calculate the amount of acceleration to apply such that the contribution from that behavior will reach terminal velocity at the value you enter.&amp;#160; If you have two behaviors, both with a Max Speed of 100, and they both are pushing in the same direction, your max speed will actually be 200.&amp;#160; While that example is a little contrived, there are many times, especially in physics based games like &lt;a href="http://www.popfly.com/users/team/Badly%20Built%20Wall"&gt;Badly Built Wall&lt;/a&gt;, that over time, the contribution from various behaviors and collisions will add up to making you go really fast.&amp;#160; &lt;/p&gt;  &lt;p&gt;How do you overcome this?&amp;#160; Why with a custom behavior of course!&amp;#160; Adding a custom behavior with the following code should do the trick:&lt;/p&gt;  &lt;blockquote&gt;   &lt;div style="font-size: 10pt; font-family: monospace; background-color: #d5d5d5"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span style="color: black"&gt; maxSpeed = 200;        &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span style="color: black"&gt; maxSpeedSquared = maxSpeed * maxSpeed;        &lt;br /&gt;        &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span style="color: black"&gt; velX = &lt;/span&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;&lt;span style="color: black"&gt;.GetValue(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;XVelocity&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;);        &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span style="color: black"&gt; velY = &lt;/span&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;&lt;span style="color: black"&gt;.GetValue(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;YVelocity&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;);        &lt;br /&gt;        &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span style="color: black"&gt; velocity = {X: velX, Y: velY};        &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span style="color: black"&gt; speedSquared = Vector2.MagnitudeSquared(velocity);        &lt;br /&gt;        &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;if&lt;/span&gt;&lt;span style="color: black"&gt; (speedSquared &amp;gt; maxSpeedSquared)        &lt;br /&gt;{ &lt;/span&gt;&lt;span style="color: black"&gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span style="color: black"&gt; normalizedVelocity = Vector2.Normalize(velocity);        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;&lt;span style="color: black"&gt;.SetValue(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;XVelocity&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;, normalizedVelocity.X * maxSpeed);        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;&lt;span style="color: black"&gt;.SetValue(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;YVelocity&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;, normalizedVelocity.Y * maxSpeed);        &lt;br /&gt;}&lt;/span&gt; &lt;/div&gt; &lt;/blockquote&gt;  &lt;p&gt;Add this as a the last behavior to your actor and set its event to Timer-&amp;gt;Every Frame.&amp;#160; What this code does is check your speed every frame after all your motion behaviors have contributed to it and if it exceeds the value you set for maxSpeed at the top, resets your velocity to point in the same direction as before, but to go only as fast as maxSpeed.&amp;#160; You can set maxSpeed to a whatever value you want, and as long as that value is low enough that your actor can’t pass through the thickness of your walls in a single frame, you should avoid any unexpected jumping through walls. &lt;em&gt; [Note:&lt;/em&gt;&amp;#160; All the *Squared values are used as a programmer’s trick to avoid the square root operation (Math.sqrt) which is fairly costly in terms of processing time.&lt;em&gt;] &lt;/em&gt; For even simpler code, check out the following from Badly Built Wall:&lt;/p&gt;  &lt;blockquote&gt;   &lt;div style="font-size: 10pt; font-family: monospace; background-color: #d5d5d5"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span style="color: black"&gt; velX = &lt;/span&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;&lt;span style="color: black"&gt;.GetValue(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;XVelocity&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;);        &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span style="color: black"&gt; velY = &lt;/span&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;&lt;span style="color: black"&gt;.GetValue(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;YVelocity&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;);        &lt;br /&gt;        &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;if&lt;/span&gt;&lt;span style="color: black"&gt; (velX &amp;gt; 201) &lt;/span&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;&lt;span style="color: black"&gt;.SetValue(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;XVelocity&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;, 200);        &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;if&lt;/span&gt;&lt;span style="color: black"&gt; (velY &amp;gt; 201) &lt;/span&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;&lt;span style="color: black"&gt;.SetValue(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;YVelocity&amp;quot;&lt;/span&gt;&lt;span style="color: black"&gt;, 200);&lt;/span&gt; &lt;/div&gt; &lt;/blockquote&gt;  &lt;p&gt;This code doesn't actually cap the max speed, but rather the velocity in the vertical and horizontal directions. This is subtly different in that it means you could actually end up going 200 in the horizontal and 200 in the vertical directions, which, if we remember our distance formula means we end up going sqrt(200^2 + 200^2) altogether or about 283 pixels / second total.&amp;#160; Basically, it means you can go faster in the diagonals than you can just straight up and down or left and right.&amp;#160; As you can probably tell it’s quite a bit less complicated (and therefore faster) and even though it’s less general can actually be desirable in games like Badly Built Wall where you are colliding mostly against axis aligned rectangles (their sides are horizontal or vertical rather than at an angle).&amp;#160; &lt;/p&gt;  &lt;p&gt;Happy creating!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8467525" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Popfly/default.aspx">Popfly</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Popfly+Game+Creator/default.aspx">Popfly Game Creator</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Casual+Games/default.aspx">Casual Games</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Games/default.aspx">Games</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/PGC/default.aspx">PGC</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Custom+Code/default.aspx">Custom Code</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Code/default.aspx">Code</category></item></channel></rss>