I’ll wait for an official 3.3.1 release I think… I don’t trust those patches. Already got update 2. Wonder when the Mac will get it… they drag their heels a bit in Apple-land.
18 July 2007
Ok, discovered what was going on. It’s a little obscure. It’s buried in a bit of SPGL code, in an Interpolator. To move my gidrahs, they have a start coordinate and an end coordinate, and a move duration and tick. They smoothly interpolate between the start and end based on the tick using a LinearInterpolator, thusly:
if (moveTick > 0) {
moveTick --;
float ratio = moveTick / (float) moveDuration;
setLocation
(
(int) LinearInterpolator.instance.interpolate(tx, sx, ratio),
(int) LinearInterpolator.instance.interpolate(ty, sy, ratio)
);
}
How, you wonder, could this possibly go wrong? Well, let’s take a look at the LinearInterpolator code:
public float interpolate(float a, float b, float ratio) {
if (ratio < 0.0f) {
ratio = 0.0f;
} else if (ratio > 1.0f) {
ratio = 1.0f;
}
return a * (1f - ratio) + b * ratio;
}
Looks like it couldn’t possibly go wrong, could it? And indeed, it probably wouldn’t if I were using doubles but I’m possibly foolishly using floats, and floating point numbers behave in all kinds of peculiar ways. The problem was occuring when a == b, for example, when the gidrah was walking south and the x coordinate was not changing, so we were interpolating effectively between two identical numbers.
Except, of course, when the curiousities of floating point maths says that a * (1f - ratio) + b * ratio != a, which it does occasionally now and again due to odd rounding errors. The resulting value, instead of say, 100, would be 99.999999, truncated to 99. The gidrah would think it was one space to the left, in the middle of a wall, and squish itself.
Fixed it like this:
public float interpolate(float a, float b, float ratio) {
if (a == b) {
return a;
}
if (ratio < 0.0f) {
ratio = 0.0f;
} else if (ratio > 1.0f) {
ratio = 1.0f;
}
return a * (1f - ratio) + b * ratio;
}
One of the gidrahs now shoots at you on sight. Makes the game very slightly harder!
Instead of doing any game coding today I’ve instead been working on tools! Our GUIs are defined in XML like this: (well, have a look through TreasureTomb.jar and see for yourself)
<!-- Title screen -->
<instance
name="title.screen"
class="tomb.TitleScreen"
autocreated="true"
mousevisible="true"
registrationbounds="0,6,220,32"
colour="255,255,255,255"
versionbounds="300,305,20,15"
keyboardnavigation="true"
music="title.buffer"
>
<area id="titlescreen" position="0,0" mouseoff="spriteimage.titlescreen.01" layer="-1" noclick="true"/>
<area id="buy" position="0,170" mouseoff="spriteimage.buy.off.01" mouseon="spriteimage.buy.on.01" disabled="spriteimage.buy.disabled.01"
nextfocus="moregames"
prevfocus="credits"
rightfocus="moregames"
upfocus="help"
/>
<area id="play" position="197,72" mouseoff="spriteimage.play.off.01" mouseon="play.button.on.animation" disabled="spriteimage.play.disabled.01"
prevfocus="moregames"
nextfocus="help"
upfocus="credits"
leftfocus="moregames"
/>
...
Poor old Chaz has had to fiddle around with the XML for the last 3 or 4 games manually, tweaking a coordinate here, adjusting a size there, getting his GUIs to line up properly once he’s drawn all the sprite for them. (I set up most of the initial XML manually). It takes him ages and ages and I’ve been very mean by not giving him a tool to do it with… until now!
You can see the tool in action for yourself now by tapping F11 on any screen. You can drag the areas around (or use cursor keys), resize them (using left-shift and the cursor keys), reset the area’s bounds to the sprite image size (‘R’ key), or finally and most usefully, tap X to write out the XML to the console ready to cut and paste back into the source code file. Neat. Tap F11 again to exit. This tool will probably save Chaz hours and hours and eventually days as we reuse this particular application framework a few more times.
Hopefully this will convince him to do the new title screen
I also fixed one or two bugs in the map editor. If you haven’t yet tried the map editor, it’s the ‘M’ key on the title screen that opens it up. I’m afraid right now you won’t be able to play your maps, but it will save and load them. It writes a file called ‘map.dat’ to the current working directory though I’m not entirely sure where that will be on Webstart right now. I’ll get a better interface on loading and saving at some point.
No new build up tonight, will put one up tomorrow most likely. Must tackle “script tiles” tomorrow. Bah.
Music: Dandy Warhols - Thirteen Tales from Urban Bohemia, DJ Vadim, Primus - Frizzlefry, Primus - Sailing the Seas of Cheese