I’ve been struggling with a programming problem for last few days, and today I found out that it was caused by a pair of insidious parenthesis.
What I was doing was changing my code to use fixed point for the position and speed of the ball. The problem I was having is that the speed was being rounded down to zero. So when I had 64 (or anything else under 256) and bit-shifted it to the right, it came out zero. I asked on the PAlib forums and a guy directed me to a website explaining how fixed point math works. As it turns out, my problem was not a glitch or bug, that’s just how fixed point works. And after some thought, I realized that my number would either it round down to zero or up to one, and never get the answer I wanted.
So I was stuck. I needed a speed of 1/4th pixels per frame, and didn’t want to use floating point (decimals). But then as I was screwing around with another test project, I noticed that it was working exactly as I wanted it to! I looked at the code and at first didn’t notice anything different… then of course I saw the parenthesis and probably uttered a swear word.
It make sense, now that I see it. I had been bit-shifting the speed before multiplying it with the result of PA_Cos and Sin — the correct method was to multiply them and then bit-shift the whole thing.
The working code…
ball.x += PA_Cos(ball.angle) * ball.speed>>8;
ball.y -= PA_Sin(ball.angle) * ball.speed>>8;
…and the non-working code.
ball.x += PA_Cos(ball.angle) * (ball.speed>>8);
ball.y -= PA_Sin(ball.angle) * (ball.speed>>8);
And now for some stuff about my awesome, totally interesting life.
I, with a great deal of help from my dad, finished threading an Ethernet cable through 60 feet of pipe. Much more difficult then it sounds. Then, inside, I ran the cable up the wall, across the ceiling, into my room… and immediately ran out of cable. DAMMIT. So today, I’m going to have to pick up another cable as well as an RJ45 coupler. Unfortunately I live in a teeny-tiny town with no large tech stores. We have one small computer repair shop that sells some stuff, so they might have some cables… but an RJ45 coupler? Considering even I didn’t know they existed before today, I’m going to guess no. But I’m hoping they do, because I don’t want to wait a week for shipping by buying it online.
I’ve been playing StarCraft a lot recently. I’m still quite crappy, but getting better. At least I can survive a zealot rush. I remember a time when I’d get attacked by ten or so zealots and all I’d have is 6 ‘lings. Now I have at least 12 ‘lings, a couple hydras and a sunken.
But I still get killed by human players. Like today, this guy massed marines — he must of had at least 40 or 50 by the end, with a few tanks thrown in for kicks. I was slaughtered. I asked him what he thought I did wrong, and he suggested that I memorize a few build orders, which I think I shall go do right now.
Oh, and one last thing: I mentioned that I was going to watch Castle a few weeks ago? Never did, because *gasp* our TV no longer received anything because we’d never gotten a digital converter box. Just yesterday I went to wal-mart to get one, but they were out. It sounded like they’d been out for a while, and probably would be out until the end of time. If they don’t have ‘em back in stock before Friday, I’ll get one when I go to Spokane.
Recent Comments