You are not logged in or registered. Please login or register to use the full functionality of this board...
SIGN IN Join Our Community For FREE


Collision Ball and Paddle
08-23-2017, 05:19 PM
Post: #1
 (Print Post)
Collision Ball and Paddle
While discussing Collisions, here is updated version of Ball versus Paddle.
Code Snippet: [Select]
'  Collision Study Mouse and Ball.bas for QB64 fork (B+=MGA) 2017-08-23
'updated from: Collision study mouse and ball.bas for QB64 fork (B+=MGA) trans 2017-08-20
'translated from "collision mouse study.bas"  SmallBASIC version 2017-01-10
'which was modify the yab version from six posts to one mouse

CONST xmax = 800
CONST ymax = 600
SCREEN _NEWIMAGE(xmax, ymax, 32)
_TITLE "Collision Study: Mouse versus Ball - bplus"

CONST RADIUS = 30 ' radius for both ball and angle
CONST SPEED = 5
ba = _PI(1 / 6) 'ball angle in radians
bx = xmax / 2 'ball x
by = ymax / 2 'ball y


WHILE 1
    CLS , _RGB(30, 70, 48)
    IF _KEYHIT = 27 THEN END 'pressed escape goodbye!

    'paddle px, py location
    DO WHILE _MOUSEINPUT
        px = _MOUSEX
        py = _MOUSEY
    LOOP
    pbDistance = SQR((px - bx) ^ 2 + (py - by) ^ 2)
    'LOCATE 1, 1: PRINT px, py, pbDistance  'debug

    'show paddle
    FOR i = RADIUS TO 0 STEP -1
        COLOR _RGB(255 - i * 5, 0, 0)
        CircleFill px, py, i
    NEXT

    'update ball hit paddle
    IF SQR((px - bx) ^ 2 + (py - by) ^ 2) < RADIUS * 2 THEN ba = _ATAN2(by - py, bx - px)

    'handle ball hit border
    IF bx < 0 + RADIUS THEN ba = _PI(1) - ba: bx = RADIUS
    IF bx > xmax - RADIUS THEN ba = _PI(1) - ba: bx = xmax - RADIUS
    IF by < 0 + RADIUS THEN ba = -ba: by = RADIUS
    IF by > ymax - RADIUS THEN ba = -ba: by = ymax - RADIUS

    'ball angle adjustments to keep between 0 and 2*pi
    IF ba > 2 * _PI(1) THEN ba = ba - _PI(2)
    IF ba < 0 THEN ba = ba + _PI(2)

    'OK the ball is here
    bx = bx + COS(ba) * SPEED
    by = by + SIN(ba) * SPEED

    'show ball
    FOR i = RADIUS TO 0 STEP -1
        COLOR _RGB(255 - i * 6, 255 - i * 6, 255 - i * 6)
        CircleFill bx, by, i
    NEXT

    'update screen and keep loops under 61 per second
    _DISPLAY
    _LIMIT 60
WEND

'Steve McNeil's  copied from his forum
SUB CircleFill (CX AS LONG, CY AS LONG, R AS LONG)
    DIM subRADIUS AS LONG, RadiusError AS LONG
    DIM X AS LONG, Y AS LONG

    subRADIUS = ABS(R)
    RadiusError = -subRADIUS
    X = subRADIUS
    Y = 0

    IF subRADIUS = 0 THEN PSET (CX, CY): EXIT SUB

    ' Draw the middle span here so we don't draw it twice in the main loop,
    ' which would be a problem with blending turned on.
    LINE (CX - X, CY)-(CX + X, CY), , BF

    WHILE X > Y
        RadiusError = RadiusError + Y * 2 + 1
        IF RadiusError >= 0 THEN
            IF X <> Y + 1 THEN
                LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
                LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
            END IF
            X = X - 1
            RadiusError = RadiusError - X * 2
        END IF
        Y = Y + 1
        LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
        LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
    WEND
END SUB

I know it's not perfect but can it be improved without getting into vector stuff?

B += _
Find all posts by this user
Like Post



Forum Jump:


User(s) browsing this thread: 1 Guest(s)




QB64 Member Project - ARB Checkers
QB64 Member Project - Kings Vallery version two
QB64 Member Project - Overboard
QB64 Member Project - Connect Four
QB64 Member Project - Swirl
QB64 Member Project - Spiro Roses
QB64 Member Project - Martin Fractals version four
QB64 Member Project - Kobolts Monopoly
QB64 Member Project - Bowditch curve
QB64 Member Project - Pivet version one
QB64 Member Project - Blokus
QB64 Member Project - Input
QB64 Member Project - Dakapo
QB64 Member Project - Touche
QB64 Member Project - Pivot version two
QB64 Member Project - 9 Board
QB64 Member Project - RGB Color Wheel
QB64 Member Project - Martin Fractals version one
QB64 Member Project - Line Thickness
QB64 Member Project - Rotating Background
QB64 Member Project - Martin Fractals version two
QB64 Member Project - Red Scrolling LED Sign
QB64 Member Project - MAPTRIANGLE
QB64 Member Project - Full Color LED Sign
QB64 Member Project - Spinning Color Wheel
QB64 Member Project - STxAxTIC 3D World
QB64 Member Project - Basic Dithering
QB64 Member Project - Dreamy Clock
QB64 Member Project - OpenGL Triangles
QB64 Member Project - Color Rotating Text
QB64 Member Project - Quarto
QB64 Member Project - Inside Moves
QB64 Member Project - Martin Fractals version three
QB64 Member Project - Kings Valley verion one
QB64 Member Project - Qubic
QB64 Member Project - Exit
QB64 Member Project - Foursight
QB64 Member Project - Algeria Weather
QB64 Member Project - Amazon
QB64 Member Project - Score 4
QB64 Member Project - Splatter
QB64 Member Project - Isolation
QB64 Member Project - Sabotage
QB64 Member Project - Point Blank
QB64 Member Project - Rubix's Magic
QB64 Member Project - Othello
QB64 Member Project - Domain
QB64 Member Project - Color Triangles
QB64 Member Project - Kings Court