Omnimaga
Calculator Community => Major Community Projects => The Axe Parser Project => Topic started by: Nathonion on January 16, 2017, 05:43:08 pm
-
So I'm making a game and for the life of me, I can't figure out how to implement collision. It's a game inspired by counter-strike (and will eventually actually BE more like it later on) with a top-down view of a person with a gun, and when you press left or right, the player will rotate, and when you press forward or back, the world moves away, making it seem that the player is moving. I made it so that a tilemap will draw relative to the x,y of the player as well. Now all I need is collision with the walls. Every way I go about it doesn't work. So I need help, or some sort of tip or something. I'll include a file with the source code and game if you want to see it. Thanks!
-
Mind adding the important parts in [code]-tags?
-
Mind adding the important parts in [code]-tags?
Sure!
To draw the room (the data for the room layout/tile sprites are in prgmCSTILE):
For(B,0,11
For(A,0,19
{B*20+A+GDB1}->T
If T
T--
Pt-On(A*8-(X//4)+44,B*8-(Y//4)+28,T*8+Pic2
End
End
End
Moving/rotating:
If getKey(55) (Controls speed of movement/rotation)
11->S
Else
22->S
End
If getKey(2) (Turn Left)
Z+S->Z
If Z>255
0->Z
End
End
If getKey(3) (Turn Right)
Z-S->Z
If Z<<0
254->Z
End
End
If getKey(4) (Move forward)
-sin(Z)//(264/S)+X->X
-cos(Z)//(264/S)+Y->Y
End
If getKey(1) (Move backward (slower))
sin(Z)//(528/S)+X->X
cos(Z)//(528/S)+Y->Y
End
Note: All this happens in a Repeat loop.
-
So in drawing you have
{B*20+A+GDB1}->T
I guess that is to fetch the tile ID and if it is non-zero then it is a wall, so that only blank stuff is walkable?
The basic idea is to do those checks before moving, so maybe something like this: (Assuming A and B are free)
If getKey(4) (Move forward)
-sin(Z)//(264/S)+X->A
-cos(Z)//(264/S)+Y->B
{B*20+A+GDB1}->T
If T
A->X
B->Y
End
End
The other movements would have to be adapted in the same way
-
I tried to implement this, but it didn't seem to line up right. The player would stop moving in random spots, and in some places you could still walk through walls. Could you maybe explain what the code means exactly?
-
The idea is to calculate the new positions and if those new positions are inside of a wall or something just revert to the old ones.
I only showed in the code how to do that for moving forward, however, i currently assume that X/Y were tile coordinates, else you'll have to adapt "{B*20+A+GDB1}->T" to translate the pixel coordinates to tile coordinates.
-
I wish I could show you a video of what's happening, because I have no idea why it is doing this. I've tried to convert the xy coordinates to tile coordinates, and many other things, but it never works! ???
-
Have you tried swapping X and Y ? I haven't really dissected the code so this is a shot in the dark, but it's an issue I've frequently encountered.
-
I just tried that, but it didn't seem to work. It does the same thing as before.