### Author Topic: Need Help With Collision  (Read 1042 times)

0 Members and 1 Guest are viewing this topic.

#### Nathonion

• LV1 Newcomer (Next: 20)
• Posts: 18
• Rating: +0/-0
##### 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!

#### Sorunome

• Fox Fox Fox Fox Fox Fox Fox!
• Support Staff
• LV13 Extreme Addict (Next: 9001)
• Posts: 7913
• Rating: +372/-13
• Derpy Hooves
##### Re: Need Help With Collision
« Reply #1 on: January 16, 2017, 05:58:27 pm »
Mind adding the important parts in [code]-tags?
 THE GAMEAlso, check out my websiteIf OmnomIRC is screwed up, blame me!Click here to give me an internet!

#### Nathonion

• LV1 Newcomer (Next: 20)
• Posts: 18
• Rating: +0/-0
##### 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,11For(A,0,19{B*20+A+GDB1}->TIf TT--Pt-On(A*8-(X//4)+44,B*8-(Y//4)+28,T*8+Pic2EndEndEndMoving/rotating:
Code: [Select]
If getKey(55)            (Controls speed of movement/rotation)11->SElse22->SEndIf getKey(2)              (Turn Left)Z+S->ZIf Z>2550->ZEndEndIf getKey(3)              (Turn Right)Z-S->ZIf Z<<0254->ZEndEndIf getKey(4)              (Move forward)-sin(Z)//(264/S)+X->X-cos(Z)//(264/S)+Y->YEndIf getKey(1)             (Move backward (slower))sin(Z)//(528/S)+X->Xcos(Z)//(528/S)+Y->YEndNote: All this happens in a Repeat loop.

#### Sorunome

• Fox Fox Fox Fox Fox Fox Fox!
• Support Staff
• LV13 Extreme Addict (Next: 9001)
• Posts: 7913
• Rating: +372/-13
• Derpy Hooves
##### 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}->TI 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}->TIf TA->XB->YEndEndThe other movements would have to be adapted in the same way
« Last Edit: January 16, 2017, 06:36:39 pm by Sorunome »
 THE GAMEAlso, check out my websiteIf OmnomIRC is screwed up, blame me!Click here to give me an internet!

#### Nathonion

• LV1 Newcomer (Next: 20)
• Posts: 18
• Rating: +0/-0
##### 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?

#### Sorunome

• Fox Fox Fox Fox Fox Fox Fox!
• Support Staff
• LV13 Extreme Addict (Next: 9001)
• Posts: 7913
• Rating: +372/-13
• Derpy Hooves
##### 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 GAMEAlso, check out my websiteIf OmnomIRC is screwed up, blame me!Click here to give me an internet!

#### Nathonion

• LV1 Newcomer (Next: 20)
• Posts: 18
• Rating: +0/-0
##### 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!

#### Xeda112358

• Xombie. I am it.
• Coder Of Tomorrow
• LV12 Extreme Poster (Next: 5000)
• Posts: 4396
• Rating: +696/-6
• meow :3
##### 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.

#### Nathonion

• LV1 Newcomer (Next: 20)
• Posts: 18
• Rating: +0/-0
##### 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.