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

Username:   Password: 
 RegisterRegister   
 Bumper Car Game- Need Help on Collision and Knock Back
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SpagehtiSauce




PostPosted: Sat Jun 06, 2009 11:56 am   Post subject: Bumper Car Game- Need Help on Collision and Knock Back

What is it you are trying to achieve?
I'm currently making a bumper car game, and i need help on making the collision of the two cars.

What is the problem you are having?
Currently, the cars can bounce off of each other, but the faster one car goes, the more farther that same car bounces, rather than the other car bouncing further. If the 2nd car isn't moving, and the first car collides with the 2nd car, it is only the 1st car that bounces off.


Describe what you have tried to solve this problem
Right now I am using the code:
p1AX := p1AX * -1
p2AX := p2AX * -1
p1AY := p1AY * -1
p2AY := p2AY * -1

to make the bounce, and I understand why that doesn't work, but I've tried other methods and still no luck.

I've also tried the code:
p1AX := (p1AX-p2AX)/2 * -1
p2AX := (p2AX-p1AX)/2 * -1
p1AY := (p1AY-p2AY)/2 * -1
p2AY := (p2AY-p1AY)/2 * -1

That divides the total speed of both vehicles and makes it move in the opposite direction (which is the bounce), but if the vehicles are head on on the y axis, it goes through each other. So if the two vehicles are moving head on at each other vetically, they sort of go into each other. Is there any possible way to fix that? or maybe even make it if one vehicle speed is moving faster than the other, then the slower vehicle gets bounced back more than the faster one?

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing:


%Bumper Smash
%J.X.



%----------SETSCREEN----------
setscreen ("graphics:1000;650")

%----------VARIABLES----------
var keys : array char of boolean
var option : string
var select : string
var bgx, bgy : int
%---Pictures---
%P1
var p1_med : array 0 .. 35 of int
p1_med (0) := Pic.FileNew ("p1_med.gif")
Pic.SetTransparentColour (p1_med (0), white)
%P2
var p2_med : array 0 .. 35 of int
p2_med (0) := Pic.FileNew ("p2_med.gif")
Pic.SetTransparentColour (p2_med (0), white)
%Menu
var title : int := Pic.FileNew ("img_title.jpg")
var selectarcade : int := Pic.FileNew ("img_selectarcade.jpg")
var selectbattle : int := Pic.FileNew ("img_selectbattle.jpg")
var selectsurvival : int := Pic.FileNew ("img_selectsurvival.jpg")
var selectcredits : int := Pic.FileNew ("img_selectcredits.jpg")
%Stages
var battleround : int := Pic.FileNew ("smash_stage_round.gif")
var battlesquare : int := Pic.FileNew ("smash_stage_square.gif")
var survivalsquare : int := Pic.FileNew ("smash_stage_bouncesquare.gif")
var arcadecan1 : int := Pic.FileNew ("arcade_stage_canyonlvl1.jpg")
%---Constants---
%P1
const NUM_P1_IMAGES : int := 36
const P1_RADIUS : int := 30
%P2
const NUM_P2_IMAGES : int := 36
const P2_RADIUS : int := 30
%---Control---
var collide : real
%P1
var p1Angle : int
var p1X, p1Y, p1AX, p1AY : real %"A" stands for acceleration
%P2
var p2Angle : int
var p2X, p2Y, p2AX, p2AY : real

for i : 1 .. 35
    p1_med (i) := Pic.Rotate (p1_med (0), i * 10, P1_RADIUS, P1_RADIUS)
end for

for o : 1 .. 35
    p2_med (o) := Pic.Rotate (p2_med (0), o * 10, P2_RADIUS, P2_RADIUS)
end for

%---Set Variables---
%-Player's Location, Velocity, Angle
%P1
p1Angle := 0
p1X := 400
p1Y := 200
p1AX := 0
p1AY := 0
%P2
p2Angle := 0
p2X := 600
p2Y := 200
p2AX := 0
p2AY := 0
%Backgrounds
bgx := 0
bgy := 0
%-Control
option := "arcade"
select := "none"

%----------PROGRAM----------
Pic.Draw (title, 0, 0, picCopy)
Input.Pause
loop
    %Select Play Mode
    if (option = "arcade") then
        Pic.Draw (selectarcade, 0, 0, picCopy)
        delay (150)
        Input.KeyDown (keys)
        if (keys (KEY_ENTER)) then
            select := "arcade"
        end if
        if (keys (KEY_DOWN_ARROW)) then
            option := "battle"
        end if
    end if
    if (option = "battle") then
        Pic.Draw (selectbattle, 0, 0, picCopy)
        delay (150)
        Input.KeyDown (keys)
        if (keys (KEY_ENTER)) then
            select := "battle"
        end if
        if (keys (KEY_UP_ARROW)) then
            option := "arcade"
        end if
        if (keys (KEY_DOWN_ARROW)) then
            option := "survival"
        end if
    end if
    if (option = "survival") then
        Pic.Draw (selectsurvival, 0, 0, picCopy)
        delay (150)
        Input.KeyDown (keys)
        if (keys (KEY_ENTER)) then
            select := "survival"
        end if
        if (keys (KEY_UP_ARROW)) then
            option := "battle"
        end if
        if (keys (KEY_DOWN_ARROW)) then
            option := "credits"
        end if
    end if
    if (option = "credits") then
        Pic.Draw (selectcredits, 0, 0, picCopy)
        delay (150)
        Input.KeyDown (keys)
        if (keys (KEY_ENTER)) then
            select := "credits"
        end if
        if (keys (KEY_UP_ARROW)) then
            option := "survival"
        end if
    end if
    if (select = "battle") then
        loop
            View.Set ("offscreenonly")
            %---Draw---
            %Pic.Draw (p1_med, 150, 100, picMerge)
            %Draw.FillBox (0, 0, 1000, 650, blue)
            Pic.Draw (battleround, 0, 0, picCopy)
            Pic.Draw (p1_med (p1Angle), round (p1X) - P1_RADIUS, round (p1Y) - P1_RADIUS, picMerge)
            Pic.Draw (p2_med (p2Angle), round (p2X) - P2_RADIUS, round (p2Y) - P2_RADIUS, picMerge)
            %---Update---
            View.Update
            %---Delay---
            delay (50)
            %---Controls---
            Input.KeyDown (keys)
            %P1
            if (keys (KEY_UP_ARROW)) then
                p1AX -= sind (p1Angle * 10)
                p1AY += cosd (p1Angle * 10)
            end if
            if (keys (KEY_RIGHT_ARROW)) then
                p1Angle := (p1Angle - 1) mod NUM_P1_IMAGES
            end if
            if (keys (KEY_LEFT_ARROW)) then
                p1Angle := (p1Angle + 1) mod NUM_P1_IMAGES
            end if
            %P2
            if (keys ('w')) then
                p2AX -= sind (p2Angle * 10)
                p2AY += cosd (p2Angle * 10)
            end if
            if (keys ('d')) then
                p2Angle := (p2Angle - 1) mod NUM_P2_IMAGES
            end if
            if (keys ('a')) then
                p2Angle := (p2Angle + 1) mod NUM_P2_IMAGES
            end if


            %Speed Limit
            p1AX := min (max (p1AX, -12), 12)
            p1AY := min (max (p1AY, -12), 12)
            p2AX := min (max (p2AX, -12), 12)
            p2AY := min (max (p2AY, -12), 12)
            %Change Player Position
            %if (not whatdotcolour (round (p2X), round (p2Y) + 50) = 29) then
            p2X += p2AX
            p2Y += p2AY
            %end if
            %if (not whatdotcolour (round (p1X), round (p1Y) + 50) = 29) then
            p1X += p1AX
            p1Y += p1AY
            %end if
            %Collision
            exit when (whatdotcolour (round (p2X) - P2_RADIUS, (round (p2Y) - P2_RADIUS) + 60) = 29) or (whatdotcolour (round (p2X) - P2_RADIUS, (round (p2Y) - P2_RADIUS) - 1) = 29)
            exit when (whatdotcolour (round (p1X) - P1_RADIUS, (round (p1Y) - P1_RADIUS) + 60) = 29) or (whatdotcolour (round (p1X) - P1_RADIUS, (round (p1Y) - P1_RADIUS) - 1) = 29)
           
            %--------(the problem)The Collision-------
            collide := sqrt ((p2X - p1X) ** 2 + (p2Y - p1Y) ** 2)
            if collide <= (P1_RADIUS + P2_RADIUS) - 1  then
                p2AX := p2AX * -1
                p1AX := p1AX * -1
                p1AY := p1AY * -1
                p2AY := p2AY * -1
                     
            end if
           
        end loop
        %This part is just to test the collision within boundaries
        Pic.Draw (survivalsquare, 0, 0, picCopy)
        View.Update
        delay (1000)
    end if



Please specify what version of Turing you are using
Turing Version: 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
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  [ 1 Posts ]
Jump to:   


Style:  
Search: