|
|
| Collision detection, Arrays and the state of the union |
|
joshua.tilson
Member #7,552
July 2006
|
Good morning! I have a couple of questions, one simple, one not so simple. Any way, I have been working with arrays to represent tile maps. int map[32][32]; for (int i=0;i<32;i++){ for (int j=1;j<32;j++){ map<i>[j] = rand()%2;}} Now here i can easily set all my array elements to random numbers (this reflects my next question), But How do i initialize the array with all 1024 elements set as I want them? IE: trying to plan out a tilemap that isn't random?? I have seen examples like int map[32][32] = {{1,1,1,1,1,1,1,1,1,1}} {{1,1,1,1,1,1,1,1}} {{.........}} But that has never worked for me i always get a compile error! The next subject i have come to is collision detection, this seems to be the hardest for me so far. LELete clarify, When I adhad aet map in my pong game, i could easily do the cocollisionetection because i always knew where the walls and paddles would be, but with a random tileset like my above example, how do i tell where the tiles are in my code? Thanks for stopping by and taking a look! Josh |
|
Archon
Member #4,195
January 2004
|
Quote: But How do i initialize the array with all 1024 elements set as I want them? 'new' them and then iterate through them. Quote: but with a random tileset like my above example, how do i tell where the tiles are in my code? Use position variables to represent where you are colliding. Then match the position of your chracter against the tilemap. |
|
LennyLen
Member #5,313
December 2004
|
Quote: But that has never worked for me i always get a compile error! Well, in the example you just gave, you missed the commas between the sets. Try compiling the following and see what happens.
By the way, this is a valid way of assing values to arrays, but it means you have to recompile your game every time you change a map. It's better to load the values from a file.
|
|
joshua.tilson
Member #7,552
July 2006
|
Lenny, Perfect thank you, The previous examples i had seen had shown ; instead of , So that explains my problem there. well let me show, whoever is interested, My lame attempt at collision detection, it works about 15%...
in particular it is this bit of code that does 'it' wwhateverit is ballCollisionX = ballX / 32; ballCollisionY = ballY /32; if (brickMap[ballCollisionX][ballCollisionY] == 1) {dy = -dy;} I take my ball's x and y and divide them by 32, in theory this gives me a whole number 1,2 etc since my tile size is 32x32. I then plug the divdividedy into the tilemap to see if it equals a collidable tile, if it does i change the direction of the ball. Obviously this is very rudimentary and imperfect... I atatched the BMPs i am using so you can compile and check out exactly what it is doing... thanks again! EDIT: i guess one solution would to be ta make sure that the ballCOllisionX and Y are divisible by 1 to make sure they are whoel numbers? i can't use !/ i get a compile error /! works untill i run the program then it crashes! EDIT 2: wait an int has to be a whol enumber doesn't it, thats why we have floating point variables.... Hmmm so then back to the drawing board on that one! |
|
|