Computer Science Canada

sidescroller colldetect help

Author:  JHDK [ Fri Jan 23, 2004 1:20 pm ]
Post subject:  sidescroller colldetect help

I have a problem with collision detection in a sidescroll game. player controls rocket trying to avoid enemy missile

Here is how i am drawing enemy:
code:

 Pic.Draw (enemy (1), enemyX (1) - speed, enemyY (1), picMerge)


the speed isinitial value 100 and +=15 in a for loop contained in a loop, i think that is where the problem is.

anyhow, here is the code for the collision detection:
code:
if x < (enemyX (i) - speed) + 50*i and x > (enemyX (i) - speed) - 50*i and y < enemyY(1) +50 and y> enemyY(1) - 50 then
                dead := true
                quit
            end if

Also, i made the background scroll and to draw enemy, make it start at say for example x=1000, if max x for screen is 800.
so the nemy will will go at 100+15+15+15...+15 until it passes past the x=0, but in the caluculations it cant detect it.

any sort of help is appreciated thankyou

ADDED: what i mean is that, it does work, but a crucial bug is that after the enemy passes, the player still dies if you go to coordinates that enemy missile had passed throuigh, even though enemy not there anymore.

Author:  Delos [ Fri Jan 23, 2004 10:05 pm ]
Post subject: 

Try 2 things:

Reset "dead" to false once if the condition does not compute.

If that does not work, then attempt a Distance function:

You'll need the (x,y) of the Player, and the (x,y) of the object (Enemy).
This (x,y) would most likely refer to either the centre or the touching edge of the object in question.

Then use a simple Pythagorean:
code:

if round( sqrt ( abs (( PlayerX - EnemyX) ** 2) + abs((PlayerY - EnemyY)**2))) <= 0 then % Increase from 0 to account for variable pic radii.
dead := true
end if
% At the end of the chech (if it is in a loop) do this:
dead := false


If that does not work...then try resetting your Enemy Array Vars for those that are off the screen.

Edit: Just found this:
http://www.compsci.ca/v2/viewtopic.php?t=75

Author:  JHDK [ Sat Jan 24, 2004 1:06 pm ]
Post subject: 

well thanks for your help, but i figured it out anyhow.
It was just that the range of detection for y was too great.

Hey I appreciate your trying to help Delos, heres a few bits.


: