Omnimaga

Calculator Community => Major Community Projects => The Axe Parser Project => Topic started by: Nathonion on January 16, 2017, 05:43:08 pm

Title: Need Help With Collision
Post 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!
Title: Re: Need Help With Collision
Post by: Sorunome on January 16, 2017, 05:58:27 pm
Mind adding the important parts in [code]-tags?
Title: Re: Need Help With Collision
Post by: Nathonion on January 16, 2017, 06:21:15 pm
Mind adding the important parts in [code]-tags?
Sure!
To draw the room (the data for the room layout/tile sprites are in prgmCSTILE):
Code: [Select]
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:
Code: [Select]
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.
Title: Re: Need Help With Collision
Post by: Sorunome on January 16, 2017, 06:33:36 pm
So in drawing you have
Code: [Select]
{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)
Code: [Select]
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
Title: Re: Need Help With Collision
Post by: Nathonion on January 16, 2017, 08:37:21 pm
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?
Title: Re: Need Help With Collision
Post by: Sorunome on January 17, 2017, 09:46:31 am
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.
Title: Re: Need Help With Collision
Post by: Nathonion on January 17, 2017, 03:54:27 pm
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! ???
Title: Re: Need Help With Collision
Post by: Xeda112358 on January 18, 2017, 02:53:22 pm
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.
Title: Re: Need Help With Collision
Post by: Nathonion on January 18, 2017, 03:22:46 pm
I just tried that, but it didn't seem to work. It does the same thing as before.