Posted: Mon Sep 08, 2008 5:06 pm Post subject: Finding lowest common denominator
For a game I'm working on, a koopa (yes, from mario) has to move randomly around the screen. I want him to pick a point, randomly, on the screen, then move there. I figured I can do this with slope, but the problem is, the slope has to be in its lowest form. How can I do this?
My procedure:
Turing:
proc move_koopa
var count :=200
%checks if the location of the koopa is the same as it's destination, if returns true then chooses a new destination if koopa_X = koopa_dest_X and koopa_Y = koopa_dest_Y then
koopa_dest_X := Rand.Int (1, maxx)
koopa_dest_Y := Rand.Int (1, maxy) endif
%my attempt at finding the lowest common denominator loop exitwhen koopa_ver mod count =0and koopa_hor mod count =0
count -=1 exitwhen count =0 endloop
koopa_Y += koopa_ver
koopa_X += koopa_hor
end move_koopa
Sponsor Sponsor
gitoxa
Posted: Mon Sep 08, 2008 8:08 pm Post subject: Re: Finding lowest common denominator
I'd start at 2, and find the lowest common number (incrementing by 1) that divides into both with a remainder of 0. First number found would be the lowest, if none are found by the time the number is half the denominator, then it's already in lowest form.
That said, I'm not quite sure I understand your reasoning behind needing it in lowest form.
Insectoid
Posted: Mon Sep 08, 2008 8:32 pm Post subject: RE:Finding lowest common denominator
I need the slope in lowest terms because otherwise I might get something like 20/10, which would cause my koopa to move 20 pixels up and 10 pixels right. I would want it moving at 2/1.
Anyways, I got it working...sort of...It is inconsistent...
Turing:
proc move_koopa
var koopa_pic :int var count :=10000 if koopa_X = koopa_dest_X and koopa_Y = koopa_dest_Y then
koopa_dest_X := Rand.Int (1, maxx)
koopa_dest_Y := Rand.Int (1, maxy) endif
I don't know turing but hopefully the example code in the article will be easy enough to understand.
Insectoid
Posted: Mon Sep 08, 2008 9:17 pm Post subject: RE:Finding lowest common denominator
Thanks, I have implemented that, but now have the error 'overflow in integer expression'. I completely forget what this means. So, I'm posting my new, revised code!
Turing:
function gcd (a, b :int):int if b =0then result a
else result gcd (b, a mod b) endif end gcd
proc move_koopa
var koopa_pic :int var count :=10000 if koopa_X = koopa_dest_X and koopa_Y = koopa_dest_Y then
koopa_dest_X := Rand.Int (1, maxx)
koopa_dest_Y := Rand.Int (1, maxy) endif
Posted: Mon Sep 08, 2008 9:50 pm Post subject: Re: RE:Finding lowest common denominator
insectoid @ Mon Sep 08, 2008 8:32 pm wrote:
I need the slope in lowest terms because otherwise I might get something like 20/10, which would cause my koopa to move 20 pixels up and 10 pixels right. I would want it moving at 2/1.
I figured that much, but what happens when you get a slope of something like 23/7 ?
Insectoid
Posted: Tue Sep 09, 2008 11:39 am Post subject: RE:Finding lowest common denominator
Then I would divide it by 2 and round, and the target would be represented by a box perhaps 8X8 pixels, that would account for any error caused by rounding. I'd just have to find the greatest possible error and make my box that big.
Tony
Posted: Tue Sep 09, 2008 11:48 am Post subject: Re: RE:Finding lowest common denominator
insectoid @ Mon Sep 08, 2008 8:32 pm wrote:
I might get something like 20/10, which would cause my koopa to move 20 pixels up and 10 pixels right. I would want it moving at 2/1.
Ugghhh... no. You are making your koopa travel at different speeds, depending on the slope.
Keep your vector as direction (angle) and magnitude (float point value). Keep the position of the koopa in real values as well, and round to integers only to draw the frame.
Posted: Tue Sep 09, 2008 12:12 pm Post subject: RE:Finding lowest common denominator
Could you provide an example? I've never used Math beyond basic operators in programming before. I figure I'd better learn sooner than later.
Saad
Posted: Tue Sep 09, 2008 1:40 pm Post subject: RE:Finding lowest common denominator
Read this for 2 methods to achieve what you want to do.
Insectoid
Posted: Tue Sep 09, 2008 3:38 pm Post subject: RE:Finding lowest common denominator
Thank you! I couldn't make heads or tails of the tutorial (I only just started Gr. 11 functions this month, all we've done is linear and quadratic review), but I managed to rip off his code (I know, I know...) and have it work in my game. (I did give The_Bean credit in the code). I understand what everything does, just not how the cod does it.
Saad
Posted: Tue Sep 09, 2008 3:51 pm Post subject: Re: RE:Finding lowest common denominator
insectoid @ Tue Sep 09, 2008 3:38 pm wrote:
Thank you! I couldn't make heads or tails of the tutorial (I only just started Gr. 11 functions this month, all we've done is linear and quadratic review), but I managed to rip off his code (I know, I know...) and have it work in my game. (I did give The_Bean credit in the code). I understand what everything does, just not how the cod does it.
There is another one at the bottom which you might find easy to understand.
Insectoid
Posted: Tue Sep 09, 2008 4:11 pm Post subject: RE:Finding lowest common denominator
Well, that is a lot simpler, but what the heck, it's working now, I don't want to fiddle around with the koopa any more. I'd rather look at the complicated trig later on after learning advanced trig in school.
P.S. This week we (my class) start learning java and I start learning Python!
Tony
Posted: Tue Sep 09, 2008 5:33 pm Post subject: Re: RE:Finding lowest common denominator
insectoid @ Tue Sep 09, 2008 4:11 pm wrote:
P.S. This week we (my class) start learning java and I start learning Python!
That doesn't at all matter, since your problem is with Math