Search
Rss
Links to friends
Categories
- Developement (53)
- Kickstarter (9)
- News (8)
- Patch (1)
- Real-Life (4)
- Uncategorized (2)
- Updates (14)
Contact us!
30Another week has passed, and brings us ever closer to the Kickstarter. We can’t reveal the exact date, but rest assured that it will be soon, and that if you read this blog, you will know a week in advance. This said, this week has been very productive. Many important features and assets have been started and/or finished. Here’s a list:
Speaking of which, ballistics are the subject of our blog this week. To do this I will now give my keyboard to Benoît, the newest member of our team.
Hi everybody,
Today’s lesson is about projectile ballistics. That’s right, we’re going to talk about archers and catapults!
For those of you who haven’t done their high school physics classes, I’ll try to be as simple as possible in my explanations and to assume very little background knowledge. However, the goal of this blog is also to give guidelines to other casual game programmers (or professional I suppose), so in that respect, I’ll be as thorough as possible with the underlying math.
You’ve been warned.
I’m sure all of you are familiar with this formula:

Suppose you want to travel from city A to city B which are 320 km (200 miles) away from one another. Suppose you’re going at an average speed of 80 km per hour (50 mph). Then your trip will have lasted 4 hours. So, that’s pretty simple, right?
Well, believe it or not, this simple formula accounts for the whole package of projectile ballistics (ignoring air resistance).

Here we’re using x and y as the horizontal and vertical components of the projectile travelling through the 3-D world. We can treat the world as being 2-D once the target and the projectile are aligned together.
I have to say that I’ve played a little trick on you, though. My little formula uses v̅ ( read: v-bar ), which is the average speed. That means if you’ve stopped for some gas or for a meal on your way, your average speed decreases accordingly, so really, v̅ contains an awful lot of information.
Fortunately, in the world of projectiles, v̅ is pretty simple to predict. Your final speed is your initial speed plus all the downwards acceleration that you’ve accumulated through gravity (denoted by the symbol g). Now your average speed is your initial speed plus half that same downwards acceleration.

Now that we have v̅ in terms that are easy to measure, we can replace it in the original formulas for x and y, giving :

In Castle Story, archers can fire arrows at various targets to inflict them damage or neutralize them. They can shoot at any angle they desire that would improve their chance of hitting their target. However, they’re limited by the robustness of their bows and by their own physical strength. We decided that the main mechanism behind bows and arrows would mainly have to do with choosing an appropriate angle for shooting rather than varying the tension in the string. That also allows us to make a simpler animation for said string if we know in advance how pulled it will be.
OK, back to formulas.
Suppose you’ve just fired an arrow and you’re looking at it flying. You can look at the x and y components of its velocity and describe its “orientation”. At the beginning, the arrow is pointing generally upwards, but as the gravity pulls its y speed down, the arrows starts pointing more and more towards the ground, all the while keeping its original x speed (read: horizontal speed).
The elevation angle at which an arrow is fired have a huge impact on the range it covers. Here are a few trajectories that an arrow could have:


Imagine you’re firing an arrow at an elevation angle of 30°. Let’s call that angle θ (read: theta). What does that mean in terms of x and y arrow velocity? Well, your arrow will have the same total speed regardless of where you fire it, so let’s say it has a starting speed of v. However, if you fire it at 30°, it will have the better part of its speed invested in its horizontal velocity and it will take very little time before the arrow starts pointing downwards. On the other hand, if you fire it at, say, 60°, the opposite will happen: the arrow will still start with speed v, but it fly for a longer time before hitting the ground, yet it will also have travelled at a much lower horizontal velocity. Now that sounds very vague and general. Using trigonometric formulas, our original equations become the following:

So, what is the best angle to hit a given target? As a general rule, 45° is the angle that reaches the farthest, although that’s not exactly true for terrain that’s not entirely flat. But how can we hit a precise target if it’s closer to us than that farthest point? Is there a way to predict where to aim specifically?
By looking at the last two formulas, we see that we have only two unknowns remaining, namely θ and t. Since we also have two independent equations, algebra allows us to isolate t in both equations and, with appropriate manipulations and some sweating, obtain θ. The resulting formula is big and ugly, but don’t panic, it can just be used in a plug-and-play manner in any little physics engine with amazing results (see applet below). Without further ado:

If any of you should be interested in the algebra needed to derive the formula, Wikipedia has a page dedicated to projectile trajectories that explains it in a detailed fashion here.
Let’s go back to the big formula one last time very quickly and break it apart for those programmers among you who’d like to use it. θ is the elevation angle that we’re trying to calculate. arctan is the inverse of the tangent trigonometric function, which in turn is the ratio of the sine and cosine functions applied to a given angle. Generally, all math programming libraries have an arctan or atan function ready for use. Then we have our good old v (read: velocity), which stands for the initial speed of the arrow. In our case, that would depend on the strength of the Bricktron firing the arrow, or the tension in the catapult at the moment of releasing a projectile. Finally, we have x and y, which in this case mean the horizontal range at which the target stands, and the altitude of the target relative to the shooter, respectively.
So, that’s what’s happening in the Bricktron archer’s head. Looking at the target. Estimating its range and altitude. Plugging his firing power in the equation, and computing the right firing angle. Since the arrow travels reasonably rapidly, the target will not generally have the time to move too far away from the aimed spot, but we’ve also included a small estimation of the target’s future position according to its speed at the moment of aiming. We won’t cover that last detail in today’s blog, though.
While archers can change their firing angle very quickly and easily, catapults can’t. Their mechanism is designed to brake at a very specific angle, something like 45°, and to throw any given projectile along a relatively similar trajectory. Where the catapult changes is in regards to it’s firing power. Not only can it project objects of different masses at different speeds, in can also be handled by a Bricktron to release more or less firing power at a time. Increasing the fire power makes the projectile go farther, and decreasing it makes it go closer, which makes it relatively easy to gauge where the catapult is shooting.
The following not-so-simple formula computes the initial speed that a projectile needs to have in order to reach a particular point in space (at horizontal reach x and altitude y, aiming at angle θ).

We’ve made a small applet that allows you to play a little bit with angle and firing power if you want to. Click inside the rectangle to change the target’s position. Knock yourself out!
[iframe src="http://www.freewebs.com/batiazul3627/castlestory/ballistics.html" width="555px" height="340px"]
Anyway, that’s it for this week’s blog, now to the soundtrack of the week!
Last week’s soundtrack got us a few nice comments. Few things are as rewarding to us as showing somebody a new band and getting a positive reaction. This said, a gift to those who enjoyed last week’s soundtrack: Jon Hopkins, master-genius of all things related to chillout ambient music. Close the lights, turn up the volume and enjoy.
See you next week everybody!
Peace.
Great game, great progress. i find myself looking forward to these updates every week. keep up the good work, me and my friends will be right here when its released.
Very interesting, you really want to make me look into game programming.
Nice work! I love the soundtracks :)
Love the playlist, It would feel amazing playing Castle Story with relaxing/amazing/ambient-ish music to go with it.
It could be nice to mention in the article that the soundtrack comes from the game Vessel by Strange Loop Games http://www.strangeloopgames.com
The soundtrack in question is a standalone disk. Strange loop most probably bought the rights to use in their game.
Can u guys make something more simple porfavor? :)
Oh, but that is the simple version. :)
Physics lesson for the win! I love technical posts like this, and trajectory physics was probably my favorite topic in high school physics.
The music you’re showing every week is actually the most important reason for me to read your devdiary ^^.
Love your taste!
Cool post, love that applet. Great example of simple projectile motion.
The game is looking great! You got a new member so I hope that it speeds up the process a little so we can actually play. It says something about a game if so many people are interested and its not even out yet…. good job guys:)
i understand that ballistics will be very important, but what amazed me more is this new chain-exchange mechanism -wish to see it in some future video. =)
somehow i got the applet to use the power of 2080786… i dont know why but when you lock it to angle and get it very close to 45 degrees maybe slightly over, and it trys to use an incredibly high power to hit it.
There’s only one thing I wonder:
Do you do any calculations as to wether the target is in range or not?
Also, I got to 2768188 power with a fixed angle. Breaking stuff with singularities is fun :D
Hey guys,
ive been watching yall progress on this game for a while too and yall seem to really have an amazing game being developed. I’m very excited for this game to become playable for the public. I’m so glad to see archery and catapults be put in. XD
You guys made progress this week? I call bullshit. Pics or it didnt happen. =o
Thanks,
i love you
P.S. Why no shadows in game?
You should add actual ballistas! You know, a big thing that shoots harpoons?
OMG! My idea was used :) This totally made my day. Thank you sauropod team. :D
(Idea was that bricktrons argue for a second before moving out of each others way)
Wow, looks great cant wait to play. Keep up the good work.
Let me give you this opportunity to tell you that me and my nephew cant wait to play this game.. We have been checking on your progress since last year.. We bookmarked your site, we click on it every 2 to 4 days..
We are really looking forward to it.. KEEP UP THE GOOD WORK!
Very nice work but you should also take care of elevation and how different heigths of archer and target influent the angle. That also has an impact on v because the projectile wont travel the whole arc if the target stands on a hill for example. Negative angles should be taken care of too for archers shooting down from the castle walls.
But Im sure you thought of that
I love it when you guys do these kinds of post, keep of the great work you’re doing. Fun fact though, in the projectile example program you have if you lock the angle at 45 degrees and place the dot so that the power is 2600982 no arrow is shown.
That… Music….Is so… freaking… sweeeeet!
Keep up the Awesome work guys, I can’t wait for the game
Hey, I was wondering how the kick-starter will work?
For us fellow Canadians, specifically those whom live in Ontario, will the kick-starter be available?
It’d be cool if you made it on steam or something! Because then everyone can get it.
Ohhhh physics. Cool :D.
Also, just thought i’d mention it in case, but ATM fixed angle accounts for target elevation ust fine, but fixed power does not. If your using the same code here as in game your going to need to fix that :(.