Author Topic: A piston engine, it was a real bargain...  (Read 4641 times)

0 Members and 1 Guest are viewing this topic.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
A piston engine, it was a real bargain...
« on: January 13, 2020, 08:01:34 am »
My son, being talented in engine tech, I whipped this one off to show him the importance of higher math knowledge for a mechanic. I eventually had to cheat on the formula, getting it off of Wikipedia, as I just couldn't come up with the algorithm that held the connecting rod rigid (solving for x). With my formulas, it kept stretching in various ways. Now that I see it, it makes sense, and I was close, but no cigar...

Code: QB64: [Select]
  1. Eng& = _NEWIMAGE(500, 500, 32)
  2. SCREEN Eng&
  3. WINDOW (-500, 500)-(500, -500)
  4. cnrd = 250: ang = 0: rpm = 100: stroke = 90: dir = 1
  5.     CLS
  6.     ang = ang + dir
  7.     IF ang > 359 THEN ang = 0
  8.     IF ang < 0 THEN ang = 359
  9.     CIRCLE (0, 0), stroke + 10, &HFFFFFFFF
  10.     CIRCLE (stroke * SIN(_D2R(ang)), stroke * COS(_D2R(ang))), 6, &HFFFFFFFF
  11.     LINE (0, 0)-(stroke * SIN(_D2R(ang)), stroke * COS(_D2R(ang)))
  12.     x = stroke * COS(_D2R(ang)) + (cnrd ^ 2 - (stroke ^ 2 * SIN(_D2R(ang)) ^ 2)) ^ .5
  13.     LINE (stroke * SIN(_D2R(ang)), stroke * COS(_D2R(ang)))-(0, x), &HFFFFFFFF
  14.     CIRCLE (0, x), 8, &HFFFFFFFF
  15.     LINE (-25, x + 25)-(25, x - 25), &HFFFFFFFF, B
  16.     LINE (-27, 500)-(-27, 20 + stroke), &HFFFF00FF
  17.     LINE (27, 500)-(27, 20 + stroke), &HFFFF00FF
  18.     IF _KEYDOWN(97) THEN cnrd = cnrd + 1 '                      a increase rod length
  19.     IF _KEYDOWN(122) THEN cnrd = cnrd - 1 '                     z decrease rod length
  20.     IF _KEYDOWN(43) THEN stroke = stroke + .1 '                + increase stroke
  21.     IF _KEYDOWN(45) THEN stroke = stroke - .1 '                - reduce stroke
  22.     IF _KEYDOWN(18432) THEN rpm = rpm + 10 '                    up arrow to speed up
  23.     IF _KEYDOWN(20480) THEN rpm = rpm - 10 '                    down arrow to slow down
  24.     IF _KEYDOWN(115) THEN dir = -dir '                          s to change direction
  25.     _KEYCLEAR
  26.     IF rpm <= 0 THEN rpm = 1
  27.     PRINT "rpm="; rpm
  28.     PRINT "stroke="; stroke
  29.     PRINT "rod="; cnrd
  30.     _LIMIT rpm
  31.     _DISPLAY
  32.  

FellippeHeitor

  • Guest
Re: A piston engine, it was a real bargain...
« Reply #1 on: January 13, 2020, 09:10:18 am »
That was really cool, OldMoses!

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: A piston engine, it was a real bargain...
« Reply #2 on: January 13, 2020, 10:18:23 am »
Yes, rather nice.  But in your default condition the connecting rod crashes against the inside of the cylinder.  I suspect that your engineering son passed criticism!
« Last Edit: January 13, 2020, 11:14:31 am by Qwerkey »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: A piston engine, it was a real bargain...
« Reply #3 on: January 13, 2020, 11:04:01 am »
Well of course it was a bargain, it was a "piston" engine! By the way, who "piston" it? Was it Clippy? I bet it was!

Andy, that was pretty cool to watch! I took out the delay, and noticed something interesting about _KEYDOWN. The display speeds up more with a key held down! I switched to INKEY$ and did not have that problem. Also, the program is speed restricted at a certain point. That can be mostly eliminated by getting rid of the _DISPLAY command, but the graphics are compromised (flicker). Hey maybe just add a top RPM limit? Anyway, very cool effect, and now just maybe your son will switch to coding??? It pays more, and it's less greasy... Well, unless you're a Cheetos freak like me, of course. :D

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A piston engine, it was a real bargain...
« Reply #4 on: January 13, 2020, 11:32:01 am »
Yes, rather nice.  But in your default condition the connecting rod crashes against the inside of the cylinder.  I suspect that your engineering son passed criticism!

Fixed for +/- keydown and original width of cylinder
Code: QB64: [Select]
  1. Eng& = _NEWIMAGE(500, 500, 32)
  2. SCREEN Eng&
  3. WINDOW (-500, 500)-(500, -500)
  4. cnrd = 250: ang = 0: rpm = 100: stroke = 90: dir = 1
  5.     CLS
  6.     ang = ang + dir
  7.     IF ang > 359 THEN ang = 0
  8.     IF ang < 0 THEN ang = 359
  9.     CIRCLE (0, 0), stroke + 10, &HFFFFFFFF
  10.     CIRCLE (stroke * SIN(_D2R(ang)), stroke * COS(_D2R(ang))), 6, &HFFFFFFFF
  11.     LINE (0, 0)-(stroke * SIN(_D2R(ang)), stroke * COS(_D2R(ang)))
  12.     x = stroke * COS(_D2R(ang)) + (cnrd ^ 2 - (stroke ^ 2 * SIN(_D2R(ang)) ^ 2)) ^ .5 + .5 * stroke 'add more stroke to keep out of circle
  13.     LINE (stroke * SIN(_D2R(ang)), stroke * COS(_D2R(ang)))-(0, x), &HFFFFFFFF
  14.     CIRCLE (0, x), 8, &HFFFFFFFF
  15.     LINE (-stroke - 10, x + 25)-(stroke + 10, x - 25), &HFFFFFFFF, B ' change these with stroke
  16.     LINE (-stroke - 14, 500)-(-stroke - 14, 20 + stroke), &HFFFF00FF ' change these with stroke
  17.     LINE (stroke + 14, 500)-(stroke + 14, 20 + stroke), &HFFFF00FF ' change these with stroke
  18.     IF _KEYDOWN(97) THEN cnrd = cnrd + 1 '                      a increase rod length
  19.     IF _KEYDOWN(122) THEN cnrd = cnrd - 1 '                     z decrease rod length
  20.     IF _KEYDOWN(43) THEN stroke = stroke + .1 '                + increase stroke
  21.     IF _KEYDOWN(45) THEN stroke = stroke - .1 '                - reduce stroke
  22.     IF _KEYDOWN(18432) THEN rpm = rpm + 10 '                    up arrow to speed up
  23.     IF _KEYDOWN(20480) THEN rpm = rpm - 10 '                    down arrow to slow down
  24.     IF _KEYDOWN(115) THEN dir = -dir '                          s to change direction
  25.     IF stroke > 125 THEN stroke = 125 'put limit on stroke
  26.     IF stroke < 10 THEN stroke = 10 'put limit on stroke
  27.     _KEYCLEAR
  28.     IF rpm <= 0 THEN rpm = 1
  29.     PRINT "rpm="; rpm
  30.     PRINT "stroke="; stroke
  31.     PRINT "rod="; cnrd
  32.     PRINT "ang="; ang
  33.     IF ang > 1 AND ang < 10 THEN
  34.         FOR i = 0 TO 5 STEP .25
  35.             CIRCLE (0, 495), i, _RGB32(255, 255, 0)
  36.         NEXT
  37.     END IF
  38.  
  39.     _LIMIT rpm
  40.     _DISPLAY
  41.  
  42.  
  43.  

Update: Added spark at top of stroke

PS _Keydown is best to use as supplement to single key press picked up by INKEY$ or _KEYHIT My tests of _keydown are  getting stuck when trying -/+ AND _KEYCLEAR wont do anything for _Keydown
« Last Edit: January 13, 2020, 11:53:32 am by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: A piston engine, it was a real bargain...
« Reply #5 on: January 13, 2020, 11:51:47 am »
Great mechanical visuals OldMoses. In many ways this is likely how our future teaching methods will go. No doubt the stress fractures, engine explosion in 3D will have the students ducking.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: A piston engine, it was a real bargain...
« Reply #6 on: January 13, 2020, 12:15:06 pm »
Fixed for +/- keydown and original width of cylinder


Update: Added spark at top of stroke

PS _Keydown is best to use as supplement to single key press picked up by INKEY$ or _KEYHIT My tests of _keydown are  getting stuck when trying -/+ AND _KEYCLEAR wont do anything for _Keydown

That's a cool effect, I'll show it to my son tonight. He'll probably notice it as a two stroke by the spark timing. ;)

Thanks for the tip on _keydown, I've been playing with it recently and have been finding it to be too fast in many applications.