Hi, I want to cause an object to roam.
I was thinking in a figure of 8, but with straight edges.
_
|_|
|_|
How could I go about mplementing such a roaming pattern?
Hi, I want to cause an object to roam.
I was thinking in a figure of 8, but with straight edges.
_
|_|
|_|
How could I go about mplementing such a roaming pattern?
What are some of your ideas and then we can discuss how they may or may not work?
Hi,
I basically have an character that moves only in one direction at a time. Basically, I want it to have random movements within the game screen but perhaps some pattern to it. I thought a figure of 8 would be nicely suited.
I just dont know where to start in coding this one
*edit* How does a figure eight go? right, down, left, down, right, up, left, up? + -' - -' + +' - +' ?Code:enum dir { up, down, left, right }; extern void object::move(dir which_way); void object::make_move( . . .) { //if nothing interesting happens... dir direction = (std::rand() >> 2) % 4; //random mod 4, but first shifted //to use higher-order bits. They're //sometimes "more random." move(direction); }
Last edited by CodeMonkey; 03-24-2009 at 12:06 AM.
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Thanks.
P.S. Nice quote
This code shows how to implement a very simple vertical zigzag pattern. The int array zigzag contains pairs of XY offsets used to apply the pattern to the alien. The patStep variable is an integer representing the current step in the pattern. When this pattern is applied, the alien moves in a vertical direction while zigzagging back and forth horizontally.Code:int[][] zigzag = {{1, 1}, {-1, 1}}; aX += zigzag[patStep][0]; aY += zigzag[patStep][1];
I found this on the net (Internet Games Tutorial - Softlookup.com)
It seems a novel idea. How would I implement patStep without immediately having a call to draw afterwards?
You probably don't want to generate a new random direction every time make_move() is called though. Have some kind of counter or timer that allows some time to pass before a new random direction is generated. That way he is not flipping directions every frame of motion....for if he did he would probably go nowhere.
Random movement usually looks pretty odd as DavidP points out. You may also consider hard-coding a bunch of different patterns (like the figure 8 using a method like strokebow has shown), then randomly choose a pattern from the set every x steps.
You could just store movements as paths. Stick them in strings, and just run through the string until you get to the end. Then make something parse the current character.Then just throw in any other neat exceptions you want, as I did with *. Naturally you will want to do some boundary checks.Code:you.path = "enwseswn"; switch( you.path[ you.here++ ] ) { case 'n': you.y--; break; case 'e': you.x++; break; case 's': you.y++; break; case 'w': you.x--; break; case '*': { switch( rand() % 4 ) { case 0: you.y--; break; case 1: you.x++; break; case 2: you.y++; break; case 3: you.x--; break; } } break; } randomsoutheastroamer.path = "s*e*";
Last edited by quzah; 03-30-2009 at 09:00 PM. Reason: randomsoutheastroamer added
Hope is the first step on the road to disappointment.