Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with collision detection for Space Invaders Remake.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Degensquared




PostPosted: Wed Oct 10, 2007 2:42 pm   Post subject: Help with collision detection for Space Invaders Remake.

Thanks in advance for any help I you can give me Very Happy

I fire the lazer, and it just goes right through enemy, and does not do damage to the enemy.
But, certain times, it kills enemys when you are sitting dirrectly below them, without the lazer damaging them.

Turing:

View.Set ("graphics:800;600,offscreenonly")
%Variables
var x, hitleftwall, hitrightwall, firedelay, firedelaytime, totalfired, topenemy : int := 0
var lazerx, lazery, fired : array 1 .. 10 of int
var enemyhp, enemyx, enemyy : array 1 .. 20 of int
var chars : array char of boolean
%settings
x := 400
topenemy := 10
colorback (black)
cls
%Sets up the arrays
for count : 1 .. topenemy
    enemyhp (count) := 3
    enemyx (count) := 50 + (50 * count)
    enemyy (count) := 500
end for
for count : 1 .. 10
    fired (count) := 0
end for
loop
    Input.KeyDown (chars)
    %Handles shooting the lazer
    if chars (KEY_UP_ARROW) then
        if firedelay = 0 then
            if totalfired <= 9 then
                totalfired += 1
                fired (totalfired) := 1
                firedelay := 1
                lazerx (totalfired) := x
                lazery (totalfired) := 35
            end if
        end if
    end if
    %Moves tank to the right
    if chars (KEY_RIGHT_ARROW) then
        if hitrightwall = 0 then
            x := x + 5
        end if
    end if
    %Moves tank to the left
    if chars (KEY_LEFT_ARROW) then
        if hitleftwall = 0 then
            x := x - 5
        end if
    end if
    %To be used for special move sometime in future (like a sheild or bomb)
    if chars (KEY_DOWN_ARROW) then
    end if
    %Collision detection with the let and right walls.
    if x <= 25 then
        hitleftwall := 1
    elsif x >= 775 then
        hitrightwall := 1
    else
        hitrightwall := 0
        hitleftwall := 0
    end if
    %Creates a delay so that bullets can only fire every 10 frames.
    if firedelay = 1 then
        if firedelaytime < 10 then
            firedelaytime += 1
        else
            firedelaytime := 0
            firedelay := 0
        end if
    end if
    %Checks for collision with enemy and lazer
    for count : 1 .. topenemy
        for count1 : 1 .. upper (fired)
            if fired (count1) = 1 then
                if lazery (count1) >= enemyy (count) - 10 and lazerx (count1) = enemyx (count) - 30 or lazery (count1) <= enemyy (count) - 7 and lazerx (count1) = enemyx (count) + 30 then
                    fired (count) := 0
                    totalfired := 0
                    if enemyhp (count) >= 1 then
                        enemyhp (count) -= 1
                    end if
                end if
            end if
        end for
    end for
    %Draws enemys with correct color
    for count : 1 .. topenemy
        if enemyhp (count) = 3 then
            drawoval (enemyx (count), enemyy (count), 7, 7, green)
        elsif enemyhp (count) = 2 then
            drawoval (enemyx (count), enemyy (count), 7, 7, yellow)
        elsif enemyhp (count) = 1 then
            drawoval (enemyx (count), enemyy (count), 7, 7, red)
        end if
    end for

    %Draws lazers and checks if they are off the screen.
    for count : 1 .. upper (fired)
        if fired (count) = 1 then
            if lazery (count) <= 775 then
                lazery (count) += 5
                drawline (lazerx (count), lazery (count), lazerx (count), lazery (count) + 5, white)
            else
                fired (count) := 0
                totalfired := 0
            end if
        end if
    end for
    %Checks if enemy is hit by lazer and then deletes lazer and takes away the hp.
    for count : 1 .. topenemy
        for count1 : 1 .. upper (fired)
            if fired (count1) = 1 then
                if lazery (count1) >= enemyy (count) - 10 and lazerx (count1) = enemyx (count) - 30 or lazery (count1) <= enemyy (count) - 7 and lazerx (count1) = enemyx (count) + 30 then
                    fired (count) := 0
                    totalfired := 0
                    if enemyhp (count) >= 1 then
                        enemyhp (count) -= 1
                    end if
                end if
            end if
        end for
    end for
    %Draws player
    drawoval (x, 30, 8, 8, white)
    drawline (x, 30, x, 43, white)
    View.Update
    delay (10)
    cls
end loop


Thanks Very Happy

Degen.
Sponsor
Sponsor
Sponsor
sponsor
isaiahk9




PostPosted: Tue May 06, 2008 5:34 pm   Post subject: Re: Help with collision detection for Space Invaders Remake.

In theory here is what you should do :
Make it so that when the user fires a bullet, that bullet will have coordinates - x will be the x coordinate of your tank when the bullet was fired and the y will increase so it will fly across the screen.
Put the bullet in a for loop so that it's y coordinate will be slowly increased, and thus it will be moving upward when you draw it with its y coordinate.
Make x and y coordinates for the enemies or targets.
Say that if bullet x and y coordinates equal the target x and y coordinates, then that target is damaged.
That works - I tried it - but there might be something more efficient out there. However, this is the best I can come up with.
Zampano




PostPosted: Tue May 06, 2008 5:51 pm   Post subject: Re: Help with collision detection for Space Invaders Remake.

He knows the entirety of coordinates, and used that. Theory is no object. The problem, I think, is here:
code:
if lazery (count1) >= enemyy (count) - 10 and lazerx (count1) = enemyx (count) - 30 or lazery (count1) <= enemyy (count) - 7 and lazerx (count1) = enemyx (count) + 30 then

You used x comparing conditions, you used '=' as opposed to '>' and '<', requiring the bullet to be at a specific x. And though I can't say, the addition of 30 seems more than a little arbitrary given the size of the enemies. Of course, this approach is use for collision detection for rectangles; for these circular enemies, you will know that the bullet is in the enemy is it's distance from the centre of the enemy is less than the enemy's radius. Thus, use int Distance.PointLine (int, int, int, int) to find the distance between those two points, and you are set for collision detection.
isaiahk9




PostPosted: Tue May 06, 2008 6:00 pm   Post subject: RE:Help with collision detection for Space Invaders Remake.

Oh. That works better than mine, Zampano. I just thought that mine was simpler to follow. Guess not.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: