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

Username:   Password: 
 RegisterRegister   
 Need some help with some collision detection
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
aspire




PostPosted: Wed Oct 03, 2007 10:08 am   Post subject: Need some help with some collision detection

Hey everyone. I am back with some more questions.

Well, at the moment I am really lost lol. I need to make a pong game.
So far this is what I have

* A moving ball
* A movable paddle
* Boundarys for the ball


This is what I need some help with/cant figure out what/how to apply it with/

* Make the ball bounce off the paddle


So far here is what I have:
**Couldnt remember what the turing tags were at the time I posted this, have to look them up lol**

Turing:

setscreen ("graphics:vga")


var X, Y : int := 105
var DX, DY : int := 1
var KEY : string (1)
var chars : string (1)
var left, right : int
var leftr, rightr : int := 1

left := 100
right := 200

locate (3, 9)
put "Score"
locate (3, 25)
put "Lives"

loop
    % Boundary
    drawbox (50, 50, 600, 350, 9)

    % The Ball Draws
    drawfilloval (X, Y, 5, 5, 9)

    % Paddle Draws
    drawfillbox (left, 50, right, 65, 7)
    delay(5)
    % Ball Erases
    drawfilloval (X, Y, 5, 5, 0)

    % Paddle Erases
    drawfillbox (left, 50, right, 65, 0)
   
    % Ball Boundarys
    X := X + DX
    Y := Y + DY
    if X = 56 or X = 594 then
        DX := (-1) * DX
    end if
    if Y = 56 or Y = 344 then
        DY := (-1) * DY
    end if

    % Paddle Boundarys
    if left = 50 then
        leftr := (-1) * leftr
    elsif right = 594 then
        rightr := (-1) * rightr
    end if

    % The keyboard controls
    if hasch then
        getch (chars)
        if ord (chars) = 203 and left > 50 then
            left := left - 10
            right := right - 10
        elsif
                ord (chars) = 205 and right < 600 then
            right := right + 10
            left := left + 10
        end if

    end if

end loop
Sponsor
Sponsor
Sponsor
sponsor
Bored




PostPosted: Sat Oct 06, 2007 8:36 pm   Post subject: Re: Need some help with some collision detection

OK, there's a lot I'm gonna mention about your code here, so I'll start off with answering your question. Collision detection in this case is rather simple at the most basic levels. Once the ball has reached the height where it would hit the paddle you simply check if it is within the bounds of the paddle. So if your Y value reaches a certain spot you check if your X value is between left and right values. If so then you change the direction of travel DY.

OK, now for my comments. Firstly please for the sake of all learn to use cls and View.Update. Here's a tutorial of View.Update and View.Set, please see the section on eliminating screen flicker. Next is if you would like change the sign on a variable rather then going variable := variable * -1 you can simply use the *= command which will multiply a variable by a certain number and assign the result to the variable. So you code woul become variable *= -1. Plus brackets are not needed around the -1. You can use this trick asweel for some other operations; +=, -=, /=, div=, mod=, **=, or=, and=, not== (works not how you would expect) and possibly a few other i don't know of. Any ways, your variable right and left can simply be changed to one variable position and replace right with position + 50 and left to position - 50. And finally what is the point of leftr and rightr? The only time you use them is to change there values but you never again use them.

This isn't bad for a starter, just a few things to clean up.
cpu_hacker4.0




PostPosted: Tue Oct 09, 2007 7:33 pm   Post subject: Re: Need some help with some collision detection

Here is a really choppy example of a png game I made. It isn't finishe yet, bu the basics are thre. Try using a MathDistance and a MathDistancePointLine for the collision detection with the ball and paddles.
code:

const RADIUS : int := 10

var chars : array char of boolean
var paddle1X : int := 5
var paddle1Y : int := maxy div 2 - 50
var paddle2X : int := maxx - 5
var paddle2Y : int := maxy div 2 - 50
var ballX : int := maxx div 2
var ballY : int := maxy div 2
var xInc : int := 1
var yInc : int := 1
var p1score : int := 0
var p2score : int := 0
var p1name : string
var p2name : string
var reply : string (1)

put "Please enter the name for player 1: " ..
get p1name

put "Please enter the name for player 2: " ..
get p2name

setscreen ("offscreenonly")

function MathDistance (x1, y1, x2, y2 : real) : real
    var dx : real := x1 - x2
    var dy : real := y1 - y2
    result sqrt (dx * dx + dy * dy)
end MathDistance


function MathDistancePointLine (px, py, x1, y1, x2, y2 : real) : real
    var lineSize : real := MathDistance (x1, y1, x2, y2)
    if lineSize = 0 then
        result MathDistance (px, py, x1, y1)
    end if

    var u : real := ((px - x1) * (x2 - x1) +
        (py - y1) * (y2 - y1)) / (lineSize * lineSize)

    if u < 0.0 then
        result MathDistance (px, py, x1, y1)
    elsif u > 1.0 then
        result MathDistance (px, py, x2, y2)
    else
        var ix : real := x1 + u * (x2 - x1)
        var iy : real := y1 + u * (y2 - y1)
        result MathDistance (px, py, ix, iy)
    end if
end MathDistancePointLine

loop
    loop
        cls
        ballX += xInc
        ballY += yInc
        drawfillbox (0, 0, maxx, maxy, yellow)
        drawline (paddle1X, paddle1Y, paddle1X, paddle1Y + 100, black)
        drawline (paddle2X, paddle2Y, paddle2X, paddle2Y + 100, black)
        drawfilloval (ballX, ballY, RADIUS, RADIUS, black)
        Input.KeyDown (chars)
        if chars ('w') then
            paddle1Y += 1
        elsif chars ('s') then
            paddle1Y -= 1
        end if
        if chars (KEY_UP_ARROW) then
            paddle2Y += 1
        elsif chars (KEY_DOWN_ARROW) then
            paddle2Y -= 1
        end if
        if paddle1Y + 100 >= maxy then
            paddle1Y -= 1
        elsif paddle1Y <= 0 then
            paddle1Y += 1
        end if
        if paddle2Y + 100 >= maxy then
            paddle2Y -= 1
        elsif paddle2Y <= 0 then
            paddle2Y += 1
        end if
        if ballY + RADIUS >= maxy then
            yInc *= -1
        elsif ballY - RADIUS <= 0 then
            yInc *= -1
        end if
        if MathDistancePointLine (ballX, ballY, paddle1X, paddle1Y, paddle1X, paddle1Y + 100) <= RADIUS or MathDistancePointLine (ballX, ballY, paddle2X, paddle2Y, paddle2X, paddle2Y + 100) <= RADIUS
                then
            xInc *= -1
        end if
        if ballX - RADIUS <= 0 then
            p2score := p2score + 1
            cls
            ballX := maxx div 2
            ballY := maxy div 2
            drawfillbox (0, 0, maxx, maxy, yellow)
            drawline (paddle1X, paddle1Y, paddle1X, paddle1Y + 100, black)
            drawline (paddle2X, paddle2Y, paddle2X, paddle2Y + 100, black)
            drawfilloval (ballX, ballY, RADIUS, RADIUS, black)
            put p2name, " scored!"
            View.Update
            delay (1000)
        elsif ballX + RADIUS >= maxx then
            p1score := p1score + 1
            cls
            ballX := maxx div 2
            ballY := maxy div 2
            drawfillbox (0, 0, maxx, maxy, yellow)
            drawline (paddle1X, paddle1Y, paddle1X, paddle1Y + 100, black)
            drawline (paddle2X, paddle2Y, paddle2X, paddle2Y + 100, black)
            drawfilloval (ballX, ballY, RADIUS, RADIUS, black)
            put p1name, " scored!"
            View.Update
            delay (1000)
        end if
        if p1score = 5 then
            put p1name, " wins!"
            cls
            exit
        elsif p2score = 5 then
            put p2name, " wins!"
            exit
        end if
        View.Update
    end loop
    cls
    put "Would you like to play again? (y/n)"
    View.Update
    getch (reply)
    if reply = 'n' then
        exit
    end if
end loop
[/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  [ 3 Posts ]
Jump to:   


Style:  
Search: