Ultimate Amiga

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Advice or tactic for collision detection!  (Read 6151 times)

0 Members and 1 Guest are viewing this topic.

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Advice or tactic for collision detection!
« on: May 16, 2015, 01:18:06 PM »

    Hey guys,

First I wish to thank you all guys for helping me get detection and it works now 100% when it comes to collision detection. I have phase 2 now I wish to tackle and would love your help guys on it. I will give you a brief summary of where I am, what I am doing and what advice you guys can give me to do it better. I don't want source code, or solve my coding problem, I want if you guys can help me with psuedo advice like you need to check this and check that kinda of text and then I will take it and turn it into code :D

Summary: Right now my code when the hero collides with an object it detects it. I can put a simple Boom command and it will make that explosive sound when I collide on something. Perfect, sweet!

Issue: What to do if the player collide with a wall? Require that if the player is moving right and it collides with the wall he cannot move right but can move up, down, left as long as there are no walls on those directions. Such situation remains an issue. Another issue I have, if the player holds down the L key to move left and the character moves too fast the collision detection is missed or hit and miss and will trigger the wall collision event on a key instead of a wall sometimes. The player must push L and release for a second, push L again and release for a second in order for the detection to work correctly 100%.  Finally, based on the code I am working if there is lots of objects to detect collision I go through a for loop to loop through the entire array which causes game performance to drop down heavily and make the character when moving around jumping instead of moving smoothly and game slow.

Attempted solution: I have being attempting to solve one problem at a time. My first problem to solve is make the game performance as much as possible not effected by how many objects on screen when checking for collision detection. I have declared couple of arrays below:

Global OBJECTFOUNDCOUNT

Dim HeroLocationX(361)
Dim HeroLocationY(361)
Dim LevelObjects(361)
Dim LevelObjectX(361)
Dim LevelObjectY(361)
Dim ObjectFound(361)
Dim ObjectFoundX(361)
Dim ObjectFoundY(361)

In procedure, I am checking to see which objects in the LevelObject is not SPACe and store that object (based on index) on ObjectFound. Store LevelObjectX and LevelObjectY on ObjectFoundX and ObjectFoundY and increment OBJECTFOUNDCOUNT. This procedure is called OBTAINOBJECTFOUND. Inside the procedure are shared variables such as:

XN, YN, OBJECTFOUNDCOUNT (hopefully from the Global variable at the beginning of the program), LevelObjects(), LevelObjectX(), LevelObjectY(), ObjectFound(), ObjectFoundX, and ObjectFoundY(). In this procedure is a for loop that check if LevelObject(N)<>SPACE then store the information from levelobject, levelobjectx, levelobjecty into objectfound, objectfoundx, and objectfoundy. Finally, increment OBJECTFOUNDCOUNT by using the command

Add OBJECTFOUNDCOUNT,1.

With this...I limit for the for loop from being for loop 1 to 361 each time I move a character a step that for loop have to be executed again from 1 to 361 inside the while loop of the engine which causes a huge drop in performance into for example for 1 to 100. Now the game performance have increased heavily. Problem however there are no values on OBJECTFOUND, OBJECTFOUNDX and OBJECTFOUNDY inside the procedure but they exist outside the procedure. Where did I go wrong here?


I have given you guys a summary and where I am about my problem and what attempt of solution I am working at...tell me is this is a good solution? What advice you guys can give me to make a better solution, easy memory and easy in CPU execution and game performance? What is it I am looking for?

I want a function that checks if the collision I am checking at is an obstacle return 1 and on the main engine where it have this command:

x$=inkey$

if x$="l" and <FUNCTION TO CHECK FOR OBSTACLE> <> 1
    execute code
End If

Every time I press L it executes that function and sees if I am not hitting an obstacle...returns 0 if I did return 1. Do the same for the right, up, down. This way it checks no obstacle in the way and prevents user from passing through the wall even if the user pressed up and L again it will prevent user from entering left because there is a wall on the left but can go right, down and up as long as there are no walls there too. That is eventually my main goal.

What advice you guys give in order for me to do this?

P.S "I have attached source code with resources here!"

Thanks in advance.  :-*
« Last Edit: May 16, 2015, 01:26:08 PM by xboxisfinished »
Logged

Xertese

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 47
  • Generic Amiga User
Re: Advice or tactic for collision detection!
« Reply #1 on: May 16, 2015, 05:22:07 PM »

Using tiles for your objects all you need to check for is a collision between your hero and an object tile then you just need to perfom a check to see which tile was collided with and execute code associated with that tile.

This way you can take out that huge for:next loop in your main game loop also you should be using sub routines which lie outside of your main game loop and are only executed when certain conditions are met this way you can keep your game loop as small as possible increasing perfomance
Logged

Lonewolf10

  • AMOS Extensions Developer
  • AMOS Dev
  • A2000
  • *****
  • Karma: 3
  • Offline Offline
  • Gender: Male
  • Posts: 618
    • http://www.aliensrcooluk.com
Re: Advice or tactic for collision detection!
« Reply #2 on: May 16, 2015, 08:00:44 PM »

It sounds to me like the collision detection isn't working because your code is running too fast. Collision detection must be a once per VBL (vertical blank) thing, if I remember correctly.

I can't remember what kind of game you are making, but sometimes turning the level into a grid can be helpful for detecting what objects have been 'hit'. Storing the grid values in a dimensioned variable where 0 is nothing, 1=wall, 2=something else and so on (e.g. LevelGrid(5,5)=1).

I'll download your source code and have a quick look in a few minutes...


Edit:

Ok, I think your first issue is your controls. Never EVER use Inkey$ for game controls. It always causes difficulty with movement. You need to have precise controls - switch to using the Key State function (hint: write a small 5 line routine to print inkey$ and scancode values, by using the Inkey$ and Scancode function. Keep it incase you need to add or change controls later, but it never becomes part of the actual game).
« Last Edit: May 16, 2015, 09:03:46 PM by Lonewolf10 »
Logged

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: Advice or tactic for collision detection!
« Reply #3 on: May 17, 2015, 01:41:17 AM »

Using tiles for your objects all you need to check for is a collision between your hero and an object tile then you just need to perfom a check to see which tile was collided with and execute code associated with that tile.

This way you can take out that huge for:next loop in your main game loop also you should be using sub routines which lie outside of your main game loop and are only executed when certain conditions are met this way you can keep your game loop as small as possible increasing perfomance

 I am limited only to 63 sprites or blob that really restricts me a lot to the amount of items I can put. I am forced to use blob 1 to past everything. That means all the bobs that bob walls, etc have bob ID 1.

For example: wall is bob 1, key is bob 1, chair is bob 1, and so on. The reason is, I am using paste bob 1, x, x, levelobjects(n) where n represents the image.

Then I an array levelobjects (361) where I store the identity of the objects so I can tell if it is a wall or not.  Then I declared levelobjectx(361), and y(361) to compare the x,y coordinated of that object with the hero's location and so on.  :)

If I could do this:

for xn=0 to 14
  for yn=0 to 19
     bob (xn+yn),x,y, levelobjects(n)
  next yn
next xn

then I would and my problem will be solved. But I know I am limited to 63 bob or sprite and I know I cannot go more than 63. So this is where I stand on the situation.
« Last Edit: May 17, 2015, 01:45:06 AM by xboxisfinished »
Logged

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: Advice or tactic for collision detection!
« Reply #4 on: May 17, 2015, 01:52:42 AM »

It sounds to me like the collision detection isn't working because your code is running too fast. Collision detection must be a once per VBL (vertical blank) thing, if I remember correctly.

I am using Wait Vbl actually :).

I can't remember what kind of game you are making, but sometimes turning the level into a grid can be helpful for detecting what objects have been 'hit'. Storing the grid values in a dimensioned variable where 0 is nothing, 1=wall, 2=something else and so on (e.g. LevelGrid(5,5)=1).

I did exactly what you suggested, except instead of using two dimensional array (which really look easy in the eye) I multiplied 19 X 14 and added 95 to get one dimensional array of 361. I used 361 because I want to make sure I have complete 100% maximum space in array without reaching difficulty. Then the story of foreloop went there. But everything else like levelgrid(5,5)=1 where 1 is wall is already being done :). I called them Wall=1, redkey=2, etc and instead of storing 1 in an array I store the variable wall and so on for easy reading and coding :)

I'll download your source code and have a quick look in a few minutes...

Thank you so much! Thank you!


Edit:

Ok, I think your first issue is your controls. Never EVER use Inkey$ for game controls. It always causes difficulty with movement. You need to have precise controls - switch to using the Key State function (hint: write a small 5 line routine to print inkey$ and scancode values, by using the Inkey$ and Scancode function. Keep it incase you need to add or change controls later, but it never becomes part of the actual game).
[/quote]

Thank you for this tip! I will get rid of inkey$ immediately for faster game performance and input key detection. Maybe it will even help better in collision detection too!! :) :) :)
Logged

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: Advice or tactic for collision detection!
« Reply #5 on: May 18, 2015, 03:19:55 PM »

Any update? :) I am curious.
Logged

Lonewolf10

  • AMOS Extensions Developer
  • AMOS Dev
  • A2000
  • *****
  • Karma: 3
  • Offline Offline
  • Gender: Male
  • Posts: 618
    • http://www.aliensrcooluk.com
Re: Advice or tactic for collision detection!
« Reply #6 on: May 19, 2015, 07:29:28 PM »

I can't remember what kind of game you are making, but sometimes turning the level into a grid can be helpful for detecting what objects have been 'hit'. Storing the grid values in a dimensioned variable where 0 is nothing, 1=wall, 2=something else and so on (e.g. LevelGrid(5,5)=1).

I did exactly what you suggested, except instead of using two dimensional array (which really look easy in the eye) I multiplied 19 X 14 and added 95 to get one dimensional array of 361. I used 361 because I want to make sure I have complete 100% maximum space in array without reaching difficulty. Then the story of foreloop went there. But everything else like levelgrid(5,5)=1 where 1 is wall is already being done :). I called them Wall=1, redkey=2, etc and instead of storing 1 in an array I store the variable wall and so on for easy reading and coding :)

I suggested a 2-dimensional array for a reason.

Let's say you character is at X-coord 45, Y-coord 66. With a few instructions you can take the coords, apply a modulo (Mod) to them based on the size of the level tiles and work out exactly which location you need to check.
Yes, you can do it with a 1 dimensional array, but it makes things more complicated and you need to apply more maths to find the tile underneath him/her.

Example:
Code: [Select]
XPOS=45 : YPOS=66
X_GRID=XPOS / 32 : Rem 32 comes from width of level tiles. X_grid should contain 1
Y_GRID=YPOS / 32 : Rem 32 comes from height of level tiles. Y_grid should contain 2
X_CHECK=XPOS mod 32 : If X_CHECK>0 Then X_GRID=X_GRID+1
Y_CHECK=YPOS mod 32 : If Y_CHECK>0 Then Y_GRID=Y_GRID+1
TILE_STANDING_ON=LEVELGRID(X_GRID,Y_GRID)
« Last Edit: May 19, 2015, 07:45:26 PM by Lonewolf10 »
Logged

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: Advice or tactic for collision detection!
« Reply #7 on: May 19, 2015, 10:39:40 PM »

I have done the code and reworked it and did it in two dimensional array. NOT only does it work 100% NOW without any difficult...but there is zero performance in game speed. In fact, it is SOOOO fast and the character moves sooo fast I need to slow the game down a bit to make it playable. I am thinking if I just add monsters with their AI's it..in itself will slow down. I am hoping.

Another thing...I do not need to use ANY and I mean ANY of the build in AMOS primitive collision detection command. I AM HAVING 100% full control of the collision detection and I am doing it the way I want to do it and I LOVE IT!

I can finally have my dream of making my 1st AMOS game...FINALLY I can be like the other people out there. Why should THEY DO it and not me??! I am human much like they are!!

So if anything..this game I am doing if not for fun..but to proof a point that I should be able to make an AMOS game like everyone else. I want to get rid of it out of my system or it will always bother me and eat on me from the inside.
Logged

Lonewolf10

  • AMOS Extensions Developer
  • AMOS Dev
  • A2000
  • *****
  • Karma: 3
  • Offline Offline
  • Gender: Male
  • Posts: 618
    • http://www.aliensrcooluk.com
Re: Advice or tactic for collision detection!
« Reply #8 on: May 21, 2015, 05:17:44 PM »

I have done the code and reworked it and did it in two dimensional array. NOT only does it work 100% NOW without any difficult...but there is zero performance in game speed. In fact, it is SOOOO fast and the character moves sooo fast I need to slow the game down a bit to make it playable. I am thinking if I just add monsters with their AI's it..in itself will slow down. I am hoping.

Don't worry, it will slow down once you add more code (monsters with AI and other graphical effects). It's a good sign that it is running fast, it means your code is good ;)
Logged
Pages: [1]   Go Up
 

TinyPortal 2.2.2 © 2005-2022