Author Topic: Need Help With Collision  (Read 8713 times)

0 Members and 1 Guest are viewing this topic.

Offline Nathonion

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 18
  • Rating: +0/-0
    • View Profile
Need Help With Collision
« 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!

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Need Help With Collision
« Reply #1 on: January 16, 2017, 05:58:27 pm »
Mind adding the important parts in [code]-tags?

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Nathonion

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 18
  • Rating: +0/-0
    • View Profile
Re: Need Help With Collision
« Reply #2 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.

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Need Help With Collision
« Reply #3 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
« Last Edit: January 16, 2017, 06:36:39 pm by Sorunome »

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Nathonion

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 18
  • Rating: +0/-0
    • View Profile
Re: Need Help With Collision
« Reply #4 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?

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Need Help With Collision
« Reply #5 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.

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Nathonion

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 18
  • Rating: +0/-0
    • View Profile
Re: Need Help With Collision
« Reply #6 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! ???

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Need Help With Collision
« Reply #7 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.

Offline Nathonion

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 18
  • Rating: +0/-0
    • View Profile
Re: Need Help With Collision
« Reply #8 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.