quinta-feira, 2 de fevereiro de 2012

Approximating damping calculations using Taylor expansions in physics simulation

I was happily following Game Physics Engine Development Book when I came across the damping approximation. Without any real explanations we got this 2 lines of code:
// Impose drag.
velocity *= real_pow(damping, duration);
An equation that involves a power of t, I freaked out. How is this possible? And then, researching a little about this I tried to understand how to numerical approximate the friction equation F = -cv. Researching more, I started to explore Box2D code, when I finnaly got a reasonable explanation, but still, there was some magical steps. Time to get my calculus books from the shelf.

This can be coded in as3 as:
velocity.scaleBy(1 - damping*duration);

It's always nice when you have the opportunity to use the stuff you learned in your undergrad course. I still don't understand the power of t thing, but I can accept this solution.

quarta-feira, 1 de fevereiro de 2012

How to make a High Scores board to your Flash game using Google App Engine - Part 1

In this tutorial you will learn how to make an online high scores board to your flash game using Google App Engine infrastructure. It'll be a two posts tutorial, on this first you'll build the server in Python and in the second post the client in AS3.

First, you need to install Python 2.5 and Google App Engine. Both sites are full of tutorials on how to do this in many operating systems.

Google App Engine is a platform for developing and hosting web applications. So, to make things easier, our flash game will interact with our app as it were a web site. How can we do this? Using the UrlRequest and UrlLoader classes

I'm not going to cover more than the really necessary part of Google App Engine framework for making a high scores board. Let's examine these data storage examples of Google's page
from google.appengine.ext import db

class Greeting(db.Model):
    author = db.UserProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)
We can easily adapt this to our needs:
class Score(db.Model):
     name = db.StringProperty()
     points = db.IntegerProperty()  

This one is bit more confusing, but if you have some experience with web development you can understand why we can turn this
class Guestbook(webapp.RequestHandler):
    def post(self):
      guestbook_name = self.request.get('guestbook_name')
      greeting = Greeting(parent=guestbook_key(guestbook_name))

      if users.get_current_user():
        greeting.author = users.get_current_user()

      greeting.content = self.request.get('content')
      self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name}))

to this
class SendScore(webapp.RequestHandler):
    def post(self):
        score = Score()

        score.name = self.request.get('name')
        score.points = int(self.request.get('points'))


This last snippet is going to get the variables name and points from a urlrequest and save them on Score database. For more explanations on this check the original example.

And finally, we need to list the first nth players (in this case, 15):
class MainHandler(webapp.RequestHandler):
    def get(self):
        #self.response.out.write('Hello world 2!')

        scores = db.GqlQuery("SELECT * FROM Score ORDER BY points DESC LIMIT 15")

        for score in scores:

def main():
    application = webapp.WSGIApplication([('/', MainHandler),
                                          ('/sendscore', SendScore)],

if __name__ == '__main__':
It's very straightforward if you know some SQL and Python. The characters \t and \n are used to parse the data in as3. It's done. In the next part we'll learn how to communicate with this server through as3 using the flash.net API.