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

Username:   Password: 
 RegisterRegister   
 rectangle picture collision
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rcbhs




PostPosted: Thu Dec 06, 2007 8:58 am   Post subject: rectangle picture collision

if my coordinates are variables because both object are constantly moving, how do I get the collision for all sides? My variables are manx, many, ghostx, ghosty for the 2 objects I need to collide. What I have now is totaly screwed and works quite strangely.

if (manx <= ghostX + Pic.Width (ghostID)) and
(many >= ghostY - Pic.Height (manID)) and
(many <= ghostY + Pic.Height (ghostID)) then
put "collision"
end if

is the code i currently have for the collision and my collision is weird (I'll try to draw it out here)
------------------------------------------------------------------
---------------------------------------------------------lGHOSTl
------------------------------------------------------------------
Now with what I have, whenever my man crosses those dotted lines in any way it sees collision, instead of just colliding with the ghost box.

Any help at all as to how I can fix my collision so it works properly would be great. (the explination above didn't help much) I tried getting it to work based on what was said earlier but I failed horribly.
Sponsor
Sponsor
Sponsor
sponsor
Silent Avenger




PostPosted: Thu Dec 06, 2007 9:40 am   Post subject: Re: rectangle picture collision

All you have to do is think of what parts of your objects will hit. I'm used to VB but the concept is the same.

If object.left < object2.left + object.width then
if object.left + object.width > object2.left then
if object.top + object. height > object2.top then
if object.top < object2.top + object2.height then
"put your operation here"
end if
end if
end if
end if

I believe that is right, for it to work you will need to have 4 decision statements for it to work and all of them have to be written properly. Hope this helps and good luck.
rcbhs




PostPosted: Thu Dec 06, 2007 10:03 am   Post subject: Re: rectangle picture collision

Ok so I have this sort of working. I got the collision working but am lost. I now need it to end the program as soon as there is collision.

code:
var manID, manID2, background, background2, ghostID, newmanID, background3 : int
var manx, many : int
var stepsize : int := 10
var key : string (1)
var ghostX, ghostY : int

procedure loadpictures
    manID := Pic.FileNew ("Pictures\\man.bmp")
    manID := Pic.Scale (manID, 35, 35)
    manID2 := Pic.FileNew ("Pictures\\man2.bmp")
    manID2 := Pic.Scale (manID, 35, 35)
    ghostID := Pic.FileNew ("Pictures\\ghost.bmp")
    ghostID := Pic.Scale (ghostID, 30, 25)
    background := Pic.FileNew ("Pictures\\desktop.bmp")
    background := Pic.Scale (background, maxx, maxy)
    background2 := Pic.FileNew ("Pictures\\desktop.bmp")
    background2 := Pic.Scale (background2, maxx, maxy)
    background3 := Pic.FileNew ("Pictures\\desktop.bmp")
    background3 := Pic.Scale (background3, maxx, maxy)
    Pic.Draw (background, 0, 0, picCopy)
end loadpictures

procedure newgame
    manx := maxx div 2 - Pic.Width (manID) div 2
    many := maxy div 2 - Pic.Width (manID) div 2
    ghostX := 100
    ghostY := 100
    Pic.Draw (background, 0, 0, picCopy)
    background2 := Pic.New (manx, many, manx + Pic.Width (manID), many + Pic.Height (manID))
    background3 := Pic.New (ghostX, ghostY, ghostX + Pic.Width (ghostID), ghostY + Pic.Height (ghostID))
    Pic.Draw (manID, manx, many, picMerge)
    Pic.Draw (ghostID, ghostX, ghostY, picMerge)
end newgame


procedure collisioncheck

    % if ghostX > manx and manx < ghostX and many > ghostY and ghostY < many then

    if (manx <= ghostX + Pic.Width (ghostID)) and
            (many >= ghostY - Pic.Height (manID)) and
            (many <= ghostY + Pic.Height (ghostID)) and
            (manx >= ghostX - Pic.Width (manID)) and
            (manx <= ghostX + Pic.Width (manID)) then
            put "collision" %this is here to let me know there is collision for now
    end if

end collisioncheck


procedure movement

    loop
        if hasch = true then
            getch (key)

            %left key
            if ord (key) = 203 then
                View.Set ("offscreenonly")
                if manx - stepsize < 0 then
                    manx := 0
                else
                    manx := manx - stepsize
                end if
                Pic.Free (background2)
                Pic.Draw (background, 0, 0, picCopy)
                newmanID := Pic.Mirror (manID)
                background2 := Pic.New (manx, many, manx + Pic.Width (manID), many + Pic.Height (manID))
                Pic.Draw (newmanID, manx, many, picMerge)
                View.Update

                %right key
            elsif ord (key) = 205 then
                View.Set ("offscreenonly")
                if manx + Pic.Width (manID) + stepsize > maxx then
                    manx := maxx - Pic.Width (manID)
                else
                    manx := manx + stepsize
                end if
                Pic.Free (background2)
                background2 := Pic.New (manx, many, manx + Pic.Width (manID), many + Pic.Height (manID))
                Pic.Draw (background, 0, 0, picCopy)
                Pic.Draw (manID, manx, many, picMerge)
                View.Update

                %up key
            elsif ord (key) = 200 then
                View.Set ("offscreenonly")
                if many + Pic.Height (manID) + stepsize > maxy then
                    many := maxy - Pic.Height (manID)
                else
                    many := many + stepsize
                end if
                Pic.Free (background2)
                background2 := Pic.New (manx, many, manx + Pic.Width (manID), many + Pic.Height (manID))
                Pic.Draw (background, 0, 0, picCopy)
                newmanID := Pic.Rotate (manID, 90, -1, -1)
                Pic.Draw (newmanID, manx, many, picMerge)
                View.Update

                %down key
            elsif ord (key) = 208 then
                View.Set ("offscreenonly")
                if many - stepsize < 0 then
                    many := 0
                else
                    many := many - stepsize
                end if
                Pic.Free (background2)
                background2 := Pic.New (manx, many, manx + Pic.Width (manID), many + Pic.Height (manID))
                Pic.Draw (background, 0, 0, picCopy)
                newmanID := Pic.Rotate (manID, 270, -1, -1)
                Pic.Draw (newmanID, manx, many, picMerge)
                View.Update
            end if
        end if
        collisioncheck
    end loop
end movement

procedure ghostmove
    loop
        if manx < ghostX then
            Pic.Draw (background3, ghostX, ghostY, picMerge)
            View.Set ("offscreenonly")
            ghostX := ghostX - stepsize
            %move right
        elsif manx > ghostX then
            Pic.Draw (background3, ghostX, ghostY, picMerge)
            View.Set ("offscreenonly")
            ghostX := ghostX + stepsize
            %move up
        end if
        if many > ghostY then
            Pic.Draw (background3, ghostX, ghostY, picMerge)
            View.Set ("offscreenonly")
            ghostY := ghostY + stepsize
            %move down
        elsif many < ghostY then
            Pic.Draw (background3, ghostX, ghostY, picMerge)
            View.Set ("offscreenonly")
            ghostY := ghostY - stepsize
        end if
        delay (150)
        background3 := Pic.New (ghostX, ghostY, ghostX + Pic.Width (ghostID), ghostY + Pic.Height (ghostID))
        Pic.Draw (ghostID, ghostX, ghostY, picMerge)
        View.Update
    end loop
    collisioncheck
end ghostmove

%MAIN PROGRAM%

loadpictures
newgame
process Move
    ghostmove
end Move
fork Move
movement


Now I know what to do (sort of) but don't know exactly how to do it. I know I would need to put my collision into a boolean variable (srry if im not explaining this right) that is false. Then I can use an exit when statment that exits the program when it is true.

Now I don't have a clue how to do that properly though...any help would be great.
Silent Avenger




PostPosted: Thu Dec 06, 2007 10:15 am   Post subject: Re: rectangle picture collision

As I am not used to Turing this may not be right but you can try it. Try puttin your exit when statement after your

put "collision" %this is here to let me know there is collision for now

line and it should end the program. Hope this helps.
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: