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

Username:   Password: 
 RegisterRegister   
 bullet collision with object
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sammi




PostPosted: Sat Nov 12, 2011 12:42 pm   Post subject: bullet collision with object

What is it you are trying to achieve?
I am trying to make the bullet and the box disappear when the bullet hits the box


What is the problem you are having?
I dont know how to remove a value in an array to make the bullet disappear


Describe what you have tried to solve this problem
<tried making sure the program actually detected the collision but I dont know where to go from there>


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


View.Set ("offscreenonly")

var keys : array char of boolean

% Number of bullets currently on the screen
var numBullets : int := 0

% used so holding space only
% fires once
var firing : boolean
firing := false

% x-location of bullets on screen
var bulletX : array 1 .. 10 of int
var bulletY : array 1 .. 10 of int

%player location
var playerX : int := 100
var playerY : int := 200

% box location
var boxX : int := 350
var boxY : int := 200

loop
    Input.KeyDown (keys) % update keyboard info

    %%%%%%% MOVING %%%%%%%%%%%%

    % Move Bullets
    % 2) PUT CODE HERE TO MOVE BULLETS TO THE RIGHT
    for i : 1 .. numBullets
        bulletX (i) := bulletX (i) + 2
    end for

    if keys ('s') = true then
        playerY := playerY - 2
    end if

    if keys ('w') = true then
        playerY := playerY + 2
    end if

    if keys ('d') = true then
        playerX := playerX + 2
    end if

    if keys ('a') = true then
        playerX := playerX - 2
    end if

    %%%%%%% FIRE CHECK  %%%%%%%%%%

    % Fire Pressed?

    if keys (' ') = true and firing = false and numBullets < 10 then

        numBullets := numBullets + 1
        bulletX (numBullets) := playerX
        bulletY (numBullets) := playerY
        firing := true
    end if

    if keys (' ') = false then
        firing := false
    end if


    %%%%%%% REMOVE CHECK  %%%%%%%%

    % this check is only if the furthest bullet
    % has reached the wall, will modify later
    if numBullets > 0 then
        if bulletX (1) > maxx then
            % 3) PUT CODE HERE TO REMOVE BULLET IN SLOT 1

        end if
    end if

    %%%%%%%% DRAWING  %%%%%%%%%%
    cls ()         % clear screen

    % Draw Character
    Draw.FillOval (playerX, playerY, 10, 10, 7)

    % Draw Bullets
    for i : 1 .. numBullets
        Draw.FillOval (bulletX (i), bulletY (i), 4, 4, 10)
    end for

    % Draw Boxes
    Draw.FillBox (boxX, boxY, 300, 300, blue)

    for i : 1 .. numBullets
    if numBullets > 0 then
        if bulletX (i) > boxX - 55 then
            put "they have collided"
        end if
    end if
    end for

    delay (25)
    View.Update
end loop




Please specify what version of Turing you are using
<Answer Here>
Sponsor
Sponsor
Sponsor
sponsor
Aange10




PostPosted: Sat Nov 12, 2011 1:08 pm   Post subject: Re: bullet collision with object

Hmm, I'll share some code with you that I made one day when I got bored Cool ; Try to compare it to what you have, and see how my code works. Look at what I've made, and try to base yours off of it.



DO NOT copy and paste my code. That's counter productive. Use it as a guide.



Turing:

View.Set ("offscreenonly")
var chars1 : array char of boolean


% ________Bullet Data________
type BulletData :
    record
        xv, yv : real % Bullet Velocities
        x, y, width, height, time_made, time_past : int % Positioning & Timer
        used : boolean % If the bullet is in use (has been fired)
    end record
var bullets : array 1 .. 10 of BulletData % 10 bullets


% ________Bullets on screen________
var bullet_reload, bullet_reload2 : int % Delay for shooting
var bullets_on_screen : boolean


% _______Define Bullet Values_______
for i : lower (bullets) .. upper (bullets)
    bullets (i).xv := 8
    bullets (i).yv := 4
    bullets (i).x := 0
    bullets (i).y := 0
    bullets (i).width := 0
    bullets (i).height := 0
    bullets (i).used := false
end for
bullet_reload2 := Time.Elapsed ()
bullet_reload := Time.Elapsed ()
bullets_on_screen := false

% ________________ Procedures ________________


% ________ Shooting _________
proc Shoot
    if chars1 ('s') = true or chars1 ('s') = true then % If you hit s
        bullets_on_screen := true % Bullets are on the screen
        % Defining the Bullet Values
        for i : lower (bullets) .. upper (bullets)
            % if the bulelt isn't used, and 300 milisceonds has passed since the last shot
            % Then it defines the bullet Vlaues
            if bullets (i).used = false and bullet_reload > 300 then
                bullets (i).used := true
                bullets (i).x := 20
                bullets (i).y := 20
                bullets (i).width := 10
                bullets (i).height := 5
                bullets (i).time_made := Time.Elapsed ()
                bullets (i).time_past := Time.Elapsed ()
                bullet_reload2 := Time.Elapsed % Bullet was just shot
                exit
            end if
        end for
    end if
    % This is the reload timer. Bullet_reload2 is the time the last bullet was
    % shot, and bullet_reload is the current time.
    bullet_reload := Time.Elapsed ()
    bullet_reload := bullet_reload - bullet_reload2
end Shoot


% _________ Draw Bullets ________
proc DrawBullet
    for i : lower (bullets) .. upper (bullets)
        if bullets (i).used = true then % Only draw bullets that are shot/ in use
            drawfilloval (bullets (i).x, bullets (i).y, bullets (i).width, bullets (i).height, 13)
        end if
    end for
end DrawBullet


% _________ Calculating _________
proc Calculate
    for i : lower (bullets) .. upper (bullets)
        % If the bullet is in use
        if bullets (i).used = true then
            % Move the bullet
            bullets (i).x += round (bullets (i).xv)
            % If you activate the following line, the bullets will fluctuate!
            %bullets (i).height += 1
           
            % Updates the current time
            bullets (i).time_past := Time.Elapsed ()
            % Updates how long the bullet has been on the screen
            bullets (i).time_past := bullets (i).time_past - bullets (i).time_made
            % If the bullet has been on the screen for 5 seconds, then it
            % defaults the bullet's values, and unactivates it.
            % **TIP: Instead of 5 seconds, collision could be the time when a
            % bullet is deactivated.
            if bullets (i).time_past > 5000 then
                bullets (i).xv := 8
                bullets (i).yv := 4
                bullets (i).x := 0
                bullets (i).y := 0
                bullets (i).width := 0
                bullets (i).height := 0
                bullets (i).used := false % No longer in use
            end if
        end if
    end for
end Calculate


% ________ Drawing the Screen ________
proc DrawScreen
    drawfillbox (0, 0, maxx, maxy, grey)
    drawfilloval (0, 40, 15, 15, black)
    Draw.ThickLine (0, 39, 0, 0, 3, black)
    Draw.ThickLine (0, 15, 20, 27, 2, black)
    put " PRESS S "
    DrawBullet
end DrawScreen


% ________Main Loop_________
loop
    cls
    Input.KeyDown (chars1)
    Shoot
    Calculate
    DrawScreen
    View.Update
    delay (100)
end loop





Also, for your collision:

Turing:

if bulletX (i) > boxX - 55 then


What are you saying here? You're telling the computer to look for the bullet to be to the right of boxX - 55. So, anytime it's to the right of the box, whether it's touching the box or not, it is true. That is why the collision message is off.
sammi




PostPosted: Sat Nov 12, 2011 1:26 pm   Post subject: RE:bullet collision with object

i cant follow your're code, its confusing me
Aange10




PostPosted: Sat Nov 12, 2011 1:27 pm   Post subject: Re: RE:bullet collision with object

sammi @ 12/11/2011, 12:26 pm wrote:
i cant follow your're code, its confusing me


What parts?
sammi




PostPosted: Sat Nov 12, 2011 1:43 pm   Post subject: RE:bullet collision with object

i just need to know how to remove a bullet from the array so that it disappears when it hits the box.
Aange10




PostPosted: Sat Nov 12, 2011 1:44 pm   Post subject: Re: RE:bullet collision with object

Aange10 @ 12/11/2011, 12:27 pm wrote:
sammi @ 12/11/2011, 12:26 pm wrote:
i cant follow your're code, its confusing me


What parts?





sammi wrote:

i just need to know how to remove a bullet from the array so that it disappears when it hits the box.



Right. What part of the coding I showed you are you confused about? I can help you understand it; hence solving your problem. Smile
Tony




PostPosted: Sat Nov 12, 2011 2:10 pm   Post subject: Re: RE:bullet collision with object

sammi @ Sat Nov 12, 2011 1:43 pm wrote:
how to remove a bullet from the array

you... don't. Because you can't. The type is array 1 .. 10 of int, so you'd always have a block of 10 integers. Someone will probably say something about resizing flexible arrays, but that's not necessary if we have a reasonable bound on the count of bullets.

You don't need to "remove" bullets, just to not draw them. In fact, you've already solved this problem elsewhere in your own code. When you fire first 3 shots, you still have an array block of 10, but you draw only 3 (and not all 10). What did you do to those other 7 slots?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
sammi




PostPosted: Sat Nov 12, 2011 5:24 pm   Post subject: Re: RE:bullet collision with object

Aange10 @ Sat Nov 12, 2011 1:44 pm wrote:
Aange10 @ 12/11/2011, 12:27 pm wrote:
sammi @ 12/11/2011, 12:26 pm wrote:
i cant follow your're code, its confusing me


What parts?





sammi wrote:

i just need to know how to remove a bullet from the array so that it disappears when it hits the box.



Right. What part of the coding I showed you are you confused about? I can help you understand it; hence solving your problem. Smile


Your variables names are really hard to follow.........
Sponsor
Sponsor
Sponsor
sponsor
Aange10




PostPosted: Sat Nov 12, 2011 5:32 pm   Post subject: RE:bullet collision with object

Bullet reload, x,y, xv, yv, width, height, used (boolean), time_made / time_past are all hard to follow?

Also they are comented, but here, I'll help you decipher it.


Turing:

bullet ().xv        % Bullet's x velocity
bullet ().yv        % Bullet's y velocity
bullet ().x         % Bullet's x cordinate
bullet ().y         % Bullet's y cordinate
bullet ().width     % Bullet's width
bullet ().height    % Bullet's height
bullet ().used      % If the bullet is fired
bullet ().time_made % Time the bullet was fired
bullet ().time_past % Time past since the bullet was fired
bullet_reload       % Time the last bullet was shot
bullet_reload       % Time past since the last bullet was shot
bullets_on_screen   % If bullets are on the screen
sammi




PostPosted: Sat Nov 12, 2011 6:38 pm   Post subject: Re: RE:bullet collision with object

Aange10 @ Sat Nov 12, 2011 5:32 pm wrote:
Bullet reload, x,y, xv, yv, width, height, used (boolean), time_made / time_past are all hard to follow?

Also they are comented, but here, I'll help you decipher it.


Turing:

bullet ().xv        % Bullet's x velocity
bullet ().yv        % Bullet's y velocity
bullet ().x         % Bullet's x cordinate
bullet ().y         % Bullet's y cordinate
bullet ().width     % Bullet's width
bullet ().height    % Bullet's height
bullet ().used      % If the bullet is fired
bullet ().time_made % Time the bullet was fired
bullet ().time_past % Time past since the bullet was fired
bullet_reload       % Time the last bullet was shot
bullet_reload       % Time past since the last bullet was shot
bullets_on_screen   % If bullets are on the screen



alright so where the part where you make the stuff disappear off the screen once you hit it?
Aange10




PostPosted: Sat Nov 12, 2011 6:49 pm   Post subject: RE:bullet collision with object

If you can't read the comments, then you really aren't trying. I'm not getting paid to do this; I'm not going to do it for you.
Zren




PostPosted: Sat Nov 12, 2011 7:57 pm   Post subject: RE:bullet collision with object

Okay, full blown source code didn't work. Thinking by yourself didn't work. Theory Time! Huzzah!

Take a gander at this program:

Turing:

View.Set ("offscreenonly")

var arr : array 1 .. 10 of boolean
var currentIndex := lower (arr) % first index of the array arr (1)

% Initialize
for i : lower (arr) .. upper (arr)
    arr (i) := false
end for

loop
    % Logic

    % Randomly (1 in 5 chance) turn off an index if it's on.
    for i : lower (arr) .. upper (arr)
        if arr (i) then
            if Rand.Int (1, 5) = 1 then
                arr (i) := false
            end if
        end if
    end for


    % Input

    % Check current index, if it's off, turn it on. Then increment current index to the next spot.
    if not arr (currentIndex) then
        arr (currentIndex) := true
        currentIndex += 1

        % Wrap around to begining
        if currentIndex > upper (arr) then
            currentIndex := lower (arr)
        end if
    end if


    % Output
    cls

    for i : lower (arr) .. upper (arr)
        color (white)
        if arr (i) then
            colorback (green)
        else
            colorback (red)
        end if
        put i, " " ..
    end for
    colorback (white)
    color (black)
    put "" % Finish line

    put "Current Index: ", currentIndex

    View.Update
    delay (100)
end loop


This is the basic logic for having a fixed amount of bullets.

Turing:

loop
    % Logic
    % Randomly (1 in 5 chance) turn off an index if it's on.
    % -> Move all bullets, if it collides or goes off screen, turn it off (stop drawing)

    % Input
    % Check current index, if it's off, turn it on. Then increment current index to the next spot.
    % -> If 'fire button' is pressed, 'spawn a new bullet'. This means reseting
    %    the data at the current index. Then setting it up normally according to your player, etc.
    %    You then turn the bullet 'on' or signal that you're now drawing this bullet.

    % Output
    % -> Cycle through the entire array of bullets, but only draw those that are 'on'.
end loop


As you might of noticed before in the sample code, currentIndex would pause even though we we're trying to 'fire'. This is because the value at the currentIndex was already on. That would mean it's still on screen. You could deem to either:

1) Don't continue (like the sample code) and wait until it turns off (leaves the screen).
2) Continue and overwrite the data. This causes the effect of disappearing bullets when the limit is reached.
3) A combination of 1 and 2. Basically you look for an ready (off state) index. Set that as the currentIndex, and continue. Be warned, make sure to only run through the array once when checking or you might generate an infinite loop in the condition that all indexes are on.

*) In all cases, you can minimize the chance of catching up to the tail and hitting an 'on state' by increasing the size of the array. Be careful not to make it too big though. Counting (for looping) over large amounts, even when not doing anything else, can slow your program down. Don't believe me? Try this:

Turing:

for e : 0 .. 10
    var limit := 10 ** e
    var start := Time.Elapsed
    for i : 0 .. limit

    end for
    var timeTaken := Time.Elapsed - start
    put "0 .. ", limit, " took ", timeTaken, " ms"
end for
sammi




PostPosted: Sun Nov 13, 2011 12:03 pm   Post subject: RE:bullet collision with object

ok sorry Aange10 i just had to read it over some more but i understand it now a little more
Aange10




PostPosted: Sun Nov 13, 2011 1:06 pm   Post subject: Re: RE:bullet collision with object

sammi @ 13/11/2011, 11:03 am wrote:
ok sorry Aange10 i just had to read it over some more but i understand it now a little more



Oh you mean

Turing:

            % If the bullet has been on the screen for 5 seconds, then it
            % defaults the bullet's values, and unactivates it.
            % **TIP: Instead of 5 seconds, collision could be the time when a
            % bullet is deactivated.


Yeah, that must have been hard to decipher.
sammi




PostPosted: Sun Nov 13, 2011 7:42 pm   Post subject: Re: RE:bullet collision with object

Aange10 @ Sun Nov 13, 2011 1:06 pm wrote:
sammi @ 13/11/2011, 11:03 am wrote:
ok sorry Aange10 i just had to read it over some more but i understand it now a little more



Oh you mean

Turing:

            % If the bullet has been on the screen for 5 seconds, then it
            % defaults the bullet's values, and unactivates it.
            % **TIP: Instead of 5 seconds, collision could be the time when a
            % bullet is deactivated.


Yeah, that must have been hard to decipher.


no lol i meant the entire code
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  [ 15 Posts ]
Jump to:   


Style:  
Search: