Author Topic: Hello physics  (Read 7360 times)

0 Members and 1 Guest are viewing this topic.

Offline dajan

  • Newbie
  • Posts: 41
    • View Profile
Hello physics
« on: January 03, 2020, 03:37:13 am »
This is kind of hello world program for some basic 2D ball physics (acceleration, friction, gravity, inelastic collision) implemented with the aim of using the fewest code lines and variables possible. This of course limits it's fidelity.

Hit W, S, A, D keys to accelerate the ball.

Code: QB64: [Select]
  1.     CLS
  2.     vx = ((x < 20 OR x > 620) * 1.9 + 1) * (vx - _KEYDOWN(ASC("d")) + _KEYDOWN(ASC("a"))) * 0.99
  3.     vy = ((y < 20 OR y > 460) * 1.9 + 1) * (vy - _KEYDOWN(ASC("s")) + _KEYDOWN(ASC("w"))) * 0.99 + 10 / 60
  4.     x = x + vx - (x < 20) + (x > 620)
  5.     y = y + vy - (y < 20) + (y > 460)
  6.     CIRCLE (x, y), 20, 7
  7.     _DISPLAY
  8.     _LIMIT 60
  9.  

/Edit: further code enhancements collected

Let's keep on concise coding, now sacrificing 5 more lines to build some game logic into the "engine":

Code: QB64: [Select]
  1. t = TIMER + 90
  2.     CLS
  3.     vx = ((x < 20 OR x > 620) * 1.9 + 1) * (vx + (x < 200 AND y > 250) * _KEYDOWN(ASC("d")) / 2 - (y > 250) * _KEYDOWN(ASC("a")) / 2) * 0.99
  4.     vy = ((y > 460) * 1.9 + 1) * (vy + (x < 200 AND y > 250) * _KEYDOWN(ASC("s")) / 2 - (x < 200 AND y > 250) * _KEYDOWN(ASC("w")) / 2) * 0.99 + 0.17
  5.     x = x + vx - (x < 20) + (x > 620)
  6.     y = y + vy + (y > 460)
  7.     p = ((p = 0) + 2 * ((p = 1 OR p = 2))) * (y > 200 AND y < 250 AND ABS(580 - (x - vx / vy * (y - 200))) < 30 AND vy > 0)
  8.     s = s - (p = 1)
  9.     PRINT "Time:"; _ROUND(t - TIMER), "Score: "; s
  10.     DRAW "BM0,250C2dR200D230BM530,200C4R100C" + STR$(7 + p) + "TA-15D100H15D20H15D20H15D20TA15U100"
  11.     CIRCLE (x, y), 20, 6
  12.     _DISPLAY
  13.     _LIMIT 60
  14. LOOP UNTIL _KEYDOWN(27) OR (TIMER > t AND y > 250)
  15.  

Time to add some bit more advanced features. Ball-rim collisions would be nice:

Code: QB64: [Select]
  1. t = TIMER + 90
  2.     CLS
  3.     vx = ((x < 25 OR x > 615) * 1.9 + 1) * (vx + (x < 200 AND y > 250) * _KEYDOWN(ASC("d")) - (y > 250) * _KEYDOWN(ASC("a"))) * 0.987
  4.     vy = ((y > 455) * 1.9 + 1) * (vy + (x < 200 AND y > 250) * _KEYDOWN(ASC("s")) - (x < 200 AND y > 250) * _KEYDOWN(ASC("w"))) * 0.987 + 0.327
  5.     vd = SQR(vx ^ 2 + vy ^ 2)
  6.     rx(1) = 520: ry(1) = 200: rx(2) = 620: ry(2) = 200 'rim position
  7.     FOR i = 1 TO 2
  8.         dd = SQR((rx(i) - x) ^ 2 + (ry(i) - y) ^ 2)
  9.         nx = ((vx * (rx(i) - x) + vy * (ry(i) - y)) * -vx - (vx * (ry(i) - y) - vy * (rx(i) - x)) * -vy) / vd / dd * 0.9
  10.         ny = ((vx * (ry(i) - y) - vy * (rx(i) - x)) * -vx + (vx * (rx(i) - x) + vy * (ry(i) - y)) * -vy) / vd / dd * 0.9
  11.         vx = (dd < 25) * -nx + (dd >= 25) * -vx
  12.         vy = (dd < 25) * -ny + (dd >= 25) * -vy
  13.     NEXT i
  14.     x = x + vx - (x < 25) * 2 + (x > 615) * 2
  15.     y = y + vy + (y > 455) * 2
  16.     p = ((p = 0) + 2 * (p <> 0)) * (y > 210 AND y < 240 AND ABS(570 - (x - vx / vy * (y - 200))) < 25) * ((vy < 0) * 2 + 1)
  17.     s = s - (p = 1) + (p = -1)
  18.     net = -((x > 500 AND x < 620 AND y > 200 AND y < 300) AND (p > 0 OR ABS(net) > 0)) * _ROUND((x - 570) - ((x > 570) * 2 + 1) * (y - 250) * 0.3) * 0.9
  19.     PRINT "Time:"; _ROUND(t - TIMER), "Score: "; s
  20.     FOR i = 1 TO 20: CIRCLE (x, y), i * 25 / 20, 6: NEXT
  21.     DRAW "BM0,250C2R200D230BM520,200C4R119D1L119D1R119D20H20C7M-" + STR$(30 - net) + ",100M-10,-15M-10,15M-10,-15M-10,15M-" + STR$(30 + net) + ",-100"
  22.     _DISPLAY
  23.     _LIMIT 60
  24. LOOP UNTIL _KEYDOWN(27) OR (TIMER > t AND y > 250)
  25.  
« Last Edit: January 13, 2020, 04:35:51 am by dajan »

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: Hello physics
« Reply #1 on: January 03, 2020, 05:37:43 pm »
Thank you. This is the perfect example to get started!
My name is Michael, but you can call me Mike :)

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Hello physics
« Reply #2 on: January 03, 2020, 08:41:26 pm »
That was fun, who knew you could get so much amusement out of 11 lines of code?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Hello physics
« Reply #3 on: January 03, 2020, 08:51:18 pm »
I am betting the ball is accelerated more than once with each key press.

Offline dajan

  • Newbie
  • Posts: 41
    • View Profile
Re: Hello physics
« Reply #4 on: January 04, 2020, 03:11:24 am »
Bplus, indeed, the ball keeps accelerating as long as you hold the key.

OldMoses,  I wondered if it can be squeezed under 10 lines, without chaining commands of course, but probbably not.

Offline DANILIN

  • Forum Regular
  • Posts: 128
    • View Profile
    • Danilin youtube
Re: Hello physics
« Reply #5 on: January 04, 2020, 07:27:59 am »
Code: [Select]
CIRCLE (x, y), 20, 7
powerful washer

Code: [Select]
FOR i = 1 TO 10: CIRCLE (x, y), 10 + i, 5: NEXT
« Last Edit: January 04, 2020, 07:29:27 am by DANILIN »
Russia looks world from future. big data is peace data.
https://youtube.com/playlist?list=PLBBTP9oVY7IagpH0g9FNUQ8JqmHwxDDDB
i never recommend anything to anyone and always write only about myself

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Hello physics
« Reply #6 on: January 04, 2020, 09:22:07 am »
I really liked this demo. Crammed to pounds of physics in a 5 pound bag, no doubt.
You're not done when it works, you're done when it's right.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Hello physics
« Reply #7 on: January 04, 2020, 11:07:27 am »
I am in agreement.  Rather pleasing for seemingly concise code.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Hello physics
« Reply #8 on: January 04, 2020, 12:50:08 pm »
yeah!
welcome physics, where are  you form?
This fine example let me remember this  https://en.wikipedia.org/wiki/La_Linea_(TV_series)
Programming isn't difficult, only it's  consuming time and coffee

Offline dajan

  • Newbie
  • Posts: 41
    • View Profile
Re: Hello physics
« Reply #9 on: January 04, 2020, 01:01:54 pm »
Ok guys, glad you liked it. Let's keep on concise coding, now sacrificing 5 more lines to build some game logic into the "engine":

Try to guess the game rules on your own :o)

Code: QB64: [Select]
  1. t = TIMER + 90
  2.     CLS
  3.     vx = ((x < 20 OR x > 620) * 1.9 + 1) * (vx + (x < 200 AND y > 250) * _KEYDOWN(ASC("d")) / 2 - (y > 250) * _KEYDOWN(ASC("a")) / 2) * 0.99
  4.     vy = ((y > 460) * 1.9 + 1) * (vy + (x < 200 AND y > 250) * _KEYDOWN(ASC("s")) / 2 - (x < 200 AND y > 250) * _KEYDOWN(ASC("w")) / 2) * 0.99 + 0.17
  5.     x = x + vx - (x < 20) + (x > 620)
  6.     y = y + vy + (y > 460)
  7.     p = ((p = 0) + 2 * ((p = 1 OR p = 2))) * (y > 200 AND y < 250 AND ABS(580 - (x - vx / vy * (y - 200))) < 30 AND vy > 0)
  8.     s = s - (p = 1)
  9.     PRINT "Time:"; _ROUND(t - TIMER), "Score: "; s
  10.     DRAW "BM0,250C2dR200D230BM530,200C4R100C" + STR$(7 + p) + "TA-15D100H15D20H15D20H15D20TA15U100"
  11.     CIRCLE (x, y), 20, 6
  12.     _DISPLAY
  13.     _LIMIT 60
  14. LOOP UNTIL _KEYDOWN(27) OR (TIMER > t AND y > 250)
  15.  
« Last Edit: January 05, 2020, 01:41:59 am by dajan »

Offline dajan

  • Newbie
  • Posts: 41
    • View Profile
Re: Hello physics
« Reply #10 on: January 04, 2020, 01:23:04 pm »
TempodiBasic I'm from Slovakia, if you are asking me. I remember LaLinea, liked to watch it on TV, it has its cool minimalistic style and was pretty funny.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Hello physics
« Reply #11 on: January 04, 2020, 01:25:55 pm »
Hi dajan,

Is the game of HORSE known in Slovakia?

FellippeHeitor

  • Guest
Re: Hello physics
« Reply #12 on: January 04, 2020, 01:35:54 pm »
That's SO cool!

Offline dajan

  • Newbie
  • Posts: 41
    • View Profile
Re: Hello physics
« Reply #13 on: January 04, 2020, 03:48:34 pm »
Hi bplus, no, I didn't know of HORSE bsaketball mod. Actually it took me 1 wikipedia and 2 YT videos to understand what is that about :o)

Offline dajan

  • Newbie
  • Posts: 41
    • View Profile
Re: Hello physics
« Reply #14 on: January 04, 2020, 04:04:10 pm »
FellippeHeitor, thanks!