|
Dumb Newb |
Cerpin Taxt
Member #8,304
February 2007
|
I started using Allegro (and...coding) a few days ago, and yesterday I was just messing around, and went to write a little snippet to make a pixel buzz around the screen at random. It seemed real simple then, but I could never get it to work quite right. It's been bugging me since then, and I can't figure it out. What was meant to be a fun little nothing has turned into a headache. It's not so much that I want this to work, as I want to know what I'm messing up. No matter what I do, it's always biased to a certain direction, and never makes any major turns. I apologize in advance for all the weird, wrong, badly coded pieces of this source, but I'm sure I'm doing something fundamentally wrong when working with the random numbers.
Oh, and it'd be awesome if someone could post the source of something like a pixel moving around the screen, making more...graceful (but still random) turns. It's not exactly what I wanna do with this, but I've been wondering about the best way to do that. |
LennyLen
Member #5,313
December 2004
|
I'm not familiar with the functions you're using to generate random numbers, but I noticed this: Quote:
if( rY != 1 ) if( rX == 1 ) chanceX = chanceX + 10; if( rX != 1 ) if( rY == 1 ) chanceY = chanceY + 10; if( rY == 1 ) if( rX != 1 ) chanceX = chanceX - 10; if( rX == 1 ) if( rY != 1 ) chanceY = chanceY - 10;
This is identical to: if(( rY != 1 ) && ( rX == 1 )) chanceX = chanceX + 10; if(( rX != 1 ) && ( rY == 1 )) chanceY = chanceY + 10; if(( rY == 1 ) && ( rX != 1 )) chanceX = chanceX - 10; if(( rX == 1 ) && ( rY != 1 )) chanceY = chanceY - 10; The first and fourth conditions are identical, as are the second and third, so this is the same as: if(( rY != 1 ) && ( rX == 1 )) { chanceX += 10; chanceY -= 10; } if(( rX != 1 ) && ( rY == 1 )) { chanceY += 10; chanceX -= 10; } Which is a lot more readable.
|
Cerpin Taxt
Member #8,304
February 2007
|
Thank you. I know that the number generator and all that are just fine, and the problem is with the way I'm handling the output. |
Audric
Member #907
January 2001
|
You are restarting a new number generator every 10ms, seeding it with 1, then 2, then 3, etc. -> Every time you run the game, it will give the same series of results. If that's not what you want, start the RNG once at the beginning of your program, seeding it with the system time. edit: I think you should printf() the series of numbers you get, to check where the results are going. |
Cerpin Taxt
Member #8,304
February 2007
|
That was the first way I tried it, and for some reason, it would change random values veeeery slowly. (maybe three times a second) The values it's generating are random enough for me (I tested them with scattering some random pixels before setting up the rest of the stuff). |
BAF
Member #2,981
December 2002
|
Your avatar looks strangely familiar. Anyway, try adding a random number between -x and x to the x and y values. Right now, it looks like you are just either moving by 10 1 or 0, which isn't very random. |
Cerpin Taxt
Member #8,304
February 2007
|
What I'm working on fixing right now is the direction of movment. I want the coordinates to be changed by 1 (or zero) because what I'm trying to randomize is the direction of movement, not the length or distance of the movement. |
CosmicR
Member #6,889
February 2006
|
one way to do it is like this:
|
Cerpin Taxt
Member #8,304
February 2007
|
Thank you very much. That's pretty much what I had in mind. That being said, for some reason, the movement from my original program looked more...organic(?), I think. I was looking more for this type of movement than the very precise, very square movements. I tried to find a way to influence the next movement by the previous one, so that there would be as little sharp, awkward movements as possible (doubt it worked). If anyone could help me out with this, it would be greatly appreciated. [EDIT] I modified it a bit and and got pretty much what I wanted. Thank you very much!
[/EDIT] |
Audric
Member #907
January 2001
|
Then use floats for coordinates, for dx and for dy.(fixed are fine, but usually not popular) case 3: dx += 0.0; dy += -0.5; break; // up case 4: dx += 0.35; dy += 0.35; break; Better put some limits to stop it from becoming too fast: if (dx > 2.0) dx = 2.0; else if (dx < -2.0) dx = -2.0;
|
Onewing
Member #6,152
August 2005
|
Just to put in my two cents (and didn't read the whole thread), from what I can tell the above methods are for just random movement. Rarely ever is this sort of movement implemented on some kind of object. Take for example if you applied the above on a NPC in like an RPG type game, the character would appear to be having a ceasure (sp?). A more practical method is to determine a "step" value when you randomly decide the direction. For each movement, you decrease the "step" value. However, you do not allow a new direction until the "step" is less than 0. Now, it appears the object is moving towards a path, when in fact, it is still just as random. Once again, my two cents. Quote: Your avatar looks strangely familiar. It's almost identical to Kibizor's (sp?)! ------------ |
LennyLen
Member #5,313
December 2004
|
Quote: It's almost identical to Kibizor's (sp?)!
Kibiz0r's: Cerpin Taxt's:
|
Johan Halmén
Member #1,550
September 2001
|
They probably can keep their avatars, since they are crawling at the same direction and never will collide and wipe out each other. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
BAF
Member #2,981
December 2002
|
What is that avatar inspired by? Kibiz0r, is it you? |
Cerpin Taxt
Member #8,304
February 2007
|
I hope I'm not divulging some massive secret here, but it's a 'glider' from Conway's Game of Life. |
Jakub Wasilewski
Member #3,653
June 2003
|
It's also considered the Hacker Emblem by some. Not in the "break into a computer" sense of the word, but rather the "very skilled with computers" sense. --------------------------- |
|