08-23-2017, 05:19 PM
Post: #1
(Print Post)
 bplus Posting Freak Posts: 1,127 Likes Given: 329 Likes Received: 192 in 164 posts Joined: Apr 2017 Country of Origin::
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?