Author Topic: Car : Physics Test  (Read 9278 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Car : Physics Test
« on: July 15, 2017, 08:39:25 am »
Hi Everyone! :)
Here's is simple demo of Car running on a uneven road with real-time physics
in the background giving at natural effect.

Controls -
'a' & 'd' to move left & right
Space to apply brakes
'h' to toggle on/off headlights


**Code is updated to according to suggestions**

Code: QB64: [Select]
  1. '####################
  2. ' Cars By Ashish
  3. '####################
  4. ' Just for fun! :)
  5. '@KingOfCoders
  6.  
  7. _TITLE "Cars"
  8.  
  9. TYPE vec2
  10.     x AS SINGLE
  11.     y AS SINGLE
  12.  
  13. TYPE wheels
  14.     pos AS vec2 'position
  15.     vel AS vec2 'velocity
  16.     acc AS vec2 'acceleration
  17.     ang AS _FLOAT 'angle
  18.     radius AS SINGLE 'wheel radius
  19.     pos2 AS vec2
  20.  
  21. TYPE headlights
  22.     pos AS vec2
  23.     switched AS _BYTE
  24.     'power as single
  25.  
  26. TYPE bodyFrames
  27.     baseFrame AS vec2
  28.     steepFrame AS vec2
  29.     baseFrame2 AS vec2
  30.     steepFrame2 AS vec2
  31.  
  32. TYPE cars
  33.     backWheel AS wheels
  34.     frontWheel AS wheels
  35.     headlight AS headlights
  36.     body AS bodyFrames
  37.  
  38.  
  39. SCREEN _NEWIMAGE(800, 400, 32)
  40.  
  41. 'noise function related variables
  42. DIM SHARED perlin_octaves AS SINGLE, perlin_amp_falloff AS SINGLE
  43.  
  44. DIM SHARED Car AS cars, white&, roadMap(800) AS vec2
  45.  
  46. FOR i = 0 TO 800
  47.     roadMap(i).x = i
  48.     roadMap(i).y = noise(xoff, yoff, 0) * 40 + 363
  49.     xoff = xoff + .001
  50.     yoff = yoff + .01
  51.  
  52. white& = _RGB(255, 255, 255)
  53. v = RND
  54. Car.backWheel.pos.x = 50
  55. Car.backWheel.pos.y = 350
  56. Car.backWheel.ang = v
  57. Car.backWheel.radius = 13
  58.  
  59. Car.frontWheel.pos.x = 130
  60. Car.frontWheel.pos.y = 350
  61. Car.frontWheel.ang = v
  62. Car.frontWheel.radius = 13
  63.  
  64. Car.headlight.switched = -1
  65.  
  66. initCar Car
  67.  
  68.  
  69.     drawCar Car
  70.     drawMap roadMap()
  71.     IF _KEYDOWN(ASC("d")) THEN
  72.         Car.backWheel.acc.x = Car.backWheel.acc.x + .05
  73.         Car.frontWheel.acc.x = Car.frontWheel.acc.x + .05
  74.     END IF
  75.     IF _KEYDOWN(ASC("a")) THEN
  76.         Car.backWheel.acc.x = Car.backWheel.acc.x - .05
  77.         Car.frontWheel.acc.x = Car.frontWheel.acc.x - .05
  78.     END IF
  79.     IF _KEYDOWN(ASC(" ")) THEN
  80.         IF Car.backWheel.vel.x > 0 THEN Car.backWheel.vel.x = Car.backWheel.vel.x - .09
  81.         IF Car.backWheel.vel.x < 0 THEN Car.backWheel.vel.x = Car.backWheel.vel.x + .09
  82.  
  83.         IF Car.frontWheel.vel.x > 0 THEN Car.frontWheel.vel.x = Car.frontWheel.vel.x - .09
  84.         IF Car.frontWheel.vel.x < 0 THEN Car.frontWheel.vel.x = Car.frontWheel.vel.x + .09
  85.     END IF
  86.     IF _KEYHIT = (ASC("h")) THEN
  87.         IF Car.headlight.switched THEN Car.headlight.switched = 0 ELSE Car.headlight.switched = -1
  88.     END IF
  89.     updateCar Car
  90.     _LIMIT 40
  91.     _DISPLAY
  92.     CLS , 1
  93.     Car.backWheel.acc.x = 0
  94.     Car.frontWheel.acc.x = 0
  95.        
  96.     IF Car.frontWheel.pos.x > 0 AND Car.frontWheel.pos.x < 800 AND Car.backWheel.pos.x > 0 AND Car.backWheel.pos.x < 800 THEN
  97.         Car.frontWheel.pos.x = roadMap(Car.frontWheel.pos2.x).x
  98.         Car.frontWheel.pos.y = roadMap(Car.frontWheel.pos2.x).y - 13
  99.         Car.backWheel.pos.x = roadMap(Car.backWheel.pos2.x).x
  100.         Car.backWheel.pos.y = roadMap(Car.backWheel.pos2.x).y - 13
  101.     END IF
  102.  
  103. SUB drawMap (mapData() AS vec2)
  104. FOR i = 0 TO UBOUND(mapData) - 1
  105.     LINE (mapData(i).x, mapData(i).y)-(mapData(i + 1).x, mapData(i + 1).y), white&
  106.  
  107. SUB initCar (obj AS cars)
  108. obj.headlight.pos.x = obj.frontWheel.pos.x + 25
  109. obj.headlight.pos.y = obj.frontWheel.pos.y - 25
  110.  
  111. SUB drawCar (obj AS cars)
  112. 'we will draw body frames first, then, lights, followed by wheels.
  113.  
  114. LINE (obj.backWheel.pos.x - 30, obj.backWheel.pos.y - 40)-(obj.frontWheel.pos.x + 25, obj.frontWheel.pos.y - 40), white& 'up base
  115. LINE (obj.backWheel.pos.x + obj.backWheel.radius, obj.backWheel.pos.y)-(obj.frontWheel.pos.x - obj.frontWheel.radius, obj.frontWheel.pos.y), white& 'down base
  116.  
  117. '.....and some more..... little lines to cover down base.....
  118. LINE (obj.backWheel.pos.x - obj.backWheel.radius, obj.backWheel.pos.y)-(obj.backWheel.pos.x - 30, obj.backWheel.pos.y), white&
  119. LINE (obj.frontWheel.pos.x + obj.backWheel.radius, obj.frontWheel.pos.y)-(obj.frontWheel.pos.x + 25, obj.frontWheel.pos.y), white&
  120.  
  121. 'left & right of base
  122. LINE (obj.backWheel.pos.x - 30, obj.backWheel.pos.y)-(obj.backWheel.pos.x - 30, obj.backWheel.pos.y - 40), white&
  123. LINE (obj.frontWheel.pos.x + 25, obj.frontWheel.pos.y)-(obj.frontWheel.pos.x + 25, obj.frontWheel.pos.y - 40), white&
  124.  
  125. 'steeps
  126.  
  127. LINE (obj.backWheel.pos.x - 30, obj.backWheel.pos.y - 40)-(obj.backWheel.pos.x + 5, obj.backWheel.pos.y - 70), white&
  128. LINE (obj.frontWheel.pos.x + 25, obj.frontWheel.pos.y - 40)-(obj.frontWheel.pos.x - 5, obj.frontWheel.pos.y - 70), white&
  129.  
  130. 'roof
  131. LINE (obj.backWheel.pos.x + 5, obj.backWheel.pos.y - 70)-(obj.frontWheel.pos.x - 5, obj.frontWheel.pos.y - 70), white&
  132.  
  133. 'divider, to form window glasses
  134. 'line (obj.backWheel.pos.y - 70)-(obj.)
  135.  
  136. 'lights, x = cos0*r + xx, cos0 = x - xx / r
  137. IF obj.headlight.switched THEN
  138.     FOR i = 1 TO 150
  139.         h = map(i, 1, 150, 3, 30)
  140.         a = map(i, 1, 100, 200, 0)
  141.         LINE (obj.headlight.pos.x + i, obj.headlight.pos.y - h)-(obj.headlight.pos.x + i, obj.headlight.pos.y + h), _RGBA(255, 255, 255, a)
  142.     NEXT
  143. 'tyres
  144. CIRCLE (obj.backWheel.pos.x, obj.backWheel.pos.y), obj.backWheel.radius, white&
  145. CIRCLE (obj.frontWheel.pos.x, obj.frontWheel.pos.y), obj.frontWheel.radius, white&
  146.  
  147. LINE (COS(obj.backWheel.ang) * obj.backWheel.radius + obj.backWheel.pos.x, SIN(obj.backWheel.ang) * obj.backWheel.radius + obj.backWheel.pos.y)-(COS(obj.backWheel.ang + _PI) * obj.backWheel.radius + obj.backWheel.pos.x, SIN(obj.backWheel.ang + _PI) * obj.backWheel.radius + obj.backWheel.pos.y), white&
  148. LINE (COS(obj.frontWheel.ang) * obj.frontWheel.radius + obj.frontWheel.pos.x, SIN(obj.frontWheel.ang) * obj.frontWheel.radius + obj.frontWheel.pos.y)-(COS(obj.frontWheel.ang + _PI) * obj.frontWheel.radius + obj.frontWheel.pos.x, SIN(obj.frontWheel.ang + _PI) * obj.frontWheel.radius + obj.frontWheel.pos.y), white&
  149.  
  150.  
  151.  
  152. SUB updateCar (obj AS cars)
  153. obj.backWheel.pos.x = obj.backWheel.pos.x + obj.backWheel.vel.x
  154. obj.backWheel.pos.y = obj.backWheel.pos.y + obj.backWheel.vel.y
  155.  
  156. IF obj.backWheel.vel.x > 0 THEN obj.backWheel.vel.x = obj.backWheel.vel.x - .01
  157. IF obj.backWheel.vel.x < 0 THEN obj.backWheel.vel.x = obj.backWheel.vel.x + .01
  158.  
  159. obj.frontWheel.pos.x = obj.frontWheel.pos.x + obj.frontWheel.vel.x
  160. obj.frontWheel.pos.y = obj.frontWheel.pos.y + obj.frontWheel.vel.y
  161.  
  162. IF obj.frontWheel.vel.x > 0 THEN obj.frontWheel.vel.x = obj.frontWheel.vel.x - .01
  163. IF obj.frontWheel.vel.x < 0 THEN obj.frontWheel.vel.x = obj.frontWheel.vel.x + .01
  164.  
  165. obj.backWheel.vel.x = obj.backWheel.vel.x + obj.backWheel.acc.x
  166. obj.backWheel.vel.y = obj.backWheel.vel.y + obj.backWheel.acc.y
  167.  
  168. obj.frontWheel.vel.x = obj.frontWheel.vel.x + obj.frontWheel.acc.x
  169. obj.frontWheel.vel.y = obj.frontWheel.vel.y + obj.frontWheel.acc.y
  170.  
  171. IF ABS(obj.backWheel.vel.x) > 0 THEN
  172.     IF obj.backWheel.vel.x <> 0 THEN obj.backWheel.ang = obj.backWheel.pos.x / obj.backWheel.radius
  173.  
  174. IF ABS(obj.frontWheel.vel.x) > 0 THEN
  175.     IF obj.frontWheel.vel.x <> 0 THEN obj.frontWheel.ang = obj.frontWheel.pos.x / Car.frontWheel.radius
  176. obj.headlight.pos.x = obj.frontWheel.pos.x + 25
  177. obj.headlight.pos.y = obj.frontWheel.pos.y - 25
  178.  
  179. obj.backWheel.pos2.x = INT(obj.backWheel.pos.x)
  180. obj.backWheel.pos2.y = INT(obj.backWheel.pos.y)
  181.  
  182. obj.frontWheel.pos2.x = INT(obj.frontWheel.pos.x)
  183. obj.frontWheel.pos2.y = INT(obj.frontWheel.pos.y)
  184.  
  185. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  186. map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  187.  
  188. FUNCTION dist! (x1!, y1!, x2!, y2!)
  189. dist! = SQR((x2! - x1!) ^ 2 + (y2! - y1!) ^ 2)
  190.  
  191. FUNCTION noise! (x AS SINGLE, y AS SINGLE, z AS SINGLE)
  192. STATIC p5NoiseSetup AS _BYTE
  193. STATIC perlin() AS SINGLE
  194. STATIC PERLIN_YWRAPB AS SINGLE, PERLIN_YWRAP AS SINGLE
  195. STATIC PERLIN_ZWRAPB AS SINGLE, PERLIN_ZWRAP AS SINGLE
  196. STATIC PERLIN_SIZE AS SINGLE
  197.  
  198. IF NOT p5NoiseSetup THEN
  199.     p5NoiseSetup = -1
  200.  
  201.     PERLIN_YWRAPB = 4
  202.     PERLIN_YWRAP = INT(1 * (2 ^ PERLIN_YWRAPB))
  203.     PERLIN_ZWRAPB = 8
  204.     PERLIN_ZWRAP = INT(1 * (2 ^ PERLIN_ZWRAPB))
  205.     PERLIN_SIZE = 4095
  206.  
  207.     perlin_octaves = 4
  208.     perlin_amp_falloff = 0.5
  209.  
  210.     REDIM perlin(PERLIN_SIZE + 1) AS SINGLE
  211.     DIM i AS SINGLE
  212.     FOR i = 0 TO PERLIN_SIZE + 1
  213.         perlin(i) = RND
  214.     NEXT
  215.  
  216. x = ABS(x)
  217. y = ABS(y)
  218. z = ABS(z)
  219.  
  220. xi = INT(x)
  221. yi = INT(y)
  222. zi = INT(z)
  223.  
  224. xf = x - xi
  225. yf = y - yi
  226. zf = z - zi
  227.  
  228. r = 0
  229. ampl = .5
  230.  
  231. FOR o = 1 TO perlin_octaves
  232.     DIM of AS SINGLE, rxf AS SINGLE
  233.     DIM ryf AS SINGLE, n1 AS SINGLE, n2 AS SINGLE, n3 AS SINGLE
  234.     of = xi + INT(yi * (2 ^ PERLIN_YWRAPB)) + INT(zi * (2 ^ PERLIN_ZWRAPB))
  235.  
  236.     rxf = 0.5 * (1.0 - COS(xf * _PI))
  237.     ryf = 0.5 * (1.0 - COS(yf * _PI))
  238.  
  239.     n1 = perlin(of AND PERLIN_SIZE)
  240.     n1 = n1 + rxf * (perlin((of + 1) AND PERLIN_SIZE) - n1)
  241.     n2 = perlin((of + PERLIN_YWRAP) AND PERLIN_SIZE)
  242.     n2 = n2 + rxf * (perlin((of + PERLIN_YWRAP + 1) AND PERLIN_SIZE) - n2)
  243.     n1 = n1 + ryf * (n2 - n1)
  244.  
  245.     of = of + PERLIN_ZWRAP
  246.     n2 = perlin(of AND PERLIN_SIZE)
  247.     n2 = n2 + rxf * (perlin((of + 1) AND PERLIN_SIZE) - n2)
  248.     n3 = perlin((of + PERLIN_YWRAP) AND PERLIN_SIZE)
  249.     n3 = n3 + rxf * (perlin((of + PERLIN_YWRAP + 1) AND PERLIN_SIZE) - n3)
  250.     n2 = n2 + ryf * (n3 - n2)
  251.  
  252.     n1 = n1 + (0.5 * (1.0 - COS(zf * _PI))) * (n2 - n1)
  253.  
  254.     r = r + n1 * ampl
  255.     ampl = ampl * perlin_amp_falloff
  256.     xi = INT(xi * (2 ^ 1))
  257.     xf = xf * 2
  258.     yi = INT(yi * (2 ^ 1))
  259.     yf = yf * 2
  260.     zi = INT(zi * (2 ^ 1))
  261.     zf = zf * 2
  262.  
  263.     IF xf >= 1.0 THEN xi = xi + 1: xf = xf - 1
  264.     IF yf >= 1.0 THEN yi = yi + 1: yf = yf - 1
  265.     IF zf >= 1.0 THEN zi = zi + 1: zf = zf - 1
  266. noise! = r
  267.  
  268. SUB noiseDetail (lod!, falloff!)
  269. IF lod! > 0 THEN perlin_octaves = lod!
  270. IF falloff! > 0 THEN perlin_amp_falloff = falloff!
  271.  

I hope you'll enjoy it.
« Last Edit: July 16, 2017, 12:49:55 am by Ashish »
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Car : Physics Test
« Reply #1 on: July 15, 2017, 11:11:25 am »
Hello Ashish -

I like it. Thanks for posting! I noticed that maaaaybe the wheels have a little slip to them, and at low speeds below .8 they do some funny things. I see why you did that on purpose - no worries.

If (and for the moment, only if) the "noise" of the road is not *too* steep and crazy - kindof like the settings you are demonstrating, I might suggest calculating the rotation angle of each wheel not with velocity, but with position. I modified the code for the front wheel only to demonstrate this - you can see that it doesn't want to slip like that back one does.

To generalize the method for all kinds of curves that go upside down and stuff, that can be done too.

Anyway, compare the front and back wheels:

Code: QB64: [Select]
  1. '####################
  2. ' Cars By Ashish
  3. '####################
  4. ' Just for fun! :)
  5. '@KingOfCoders
  6.  
  7. _TITLE "Cars"
  8.  
  9. TYPE vec2
  10.     x AS SINGLE
  11.     y AS SINGLE
  12.  
  13. TYPE wheels
  14.     pos AS vec2 'position
  15.     vel AS vec2 'velocity
  16.     acc AS vec2 'acceleration
  17.     ang AS _FLOAT 'angle
  18.     radius AS SINGLE 'wheel radius
  19.     pos2 AS vec2
  20.  
  21. TYPE headlights
  22.     pos AS vec2
  23.     switched AS _BYTE
  24.     'power as single
  25.  
  26. TYPE bodyFrames
  27.     baseFrame AS vec2
  28.     steepFrame AS vec2
  29.     baseFrame2 AS vec2
  30.     steepFrame2 AS vec2
  31.  
  32. TYPE cars
  33.     backWheel AS wheels
  34.     frontWheel AS wheels
  35.     headlight AS headlights
  36.     body AS bodyFrames
  37.  
  38.  
  39. SCREEN _NEWIMAGE(800, 400, 32)
  40.  
  41. 'noise function related variables
  42. DIM SHARED perlin_octaves AS SINGLE, perlin_amp_falloff AS SINGLE
  43.  
  44. DIM SHARED Car AS cars, white&, roadMap(800) AS vec2
  45.  
  46. FOR i = 0 TO 800
  47.     roadMap(i).x = i
  48.     roadMap(i).y = noise(xoff, yoff, 0) * 40 + 363
  49.     xoff = xoff + .001
  50.     yoff = yoff + .01
  51.  
  52. white& = _RGB(255, 255, 255)
  53. v = RND
  54. Car.backWheel.pos.x = 50
  55. Car.backWheel.pos.y = 350
  56. Car.backWheel.ang = v
  57. Car.backWheel.radius = 13
  58.  
  59. Car.frontWheel.pos.x = 130
  60. Car.frontWheel.pos.y = 350
  61. Car.frontWheel.ang = v
  62. Car.frontWheel.radius = 13
  63.  
  64. Car.headlight.switched = -1
  65.  
  66. initCar Car
  67.  
  68.  
  69.     drawCar Car
  70.     drawMap roadMap()
  71.     IF _KEYDOWN(ASC("d")) THEN
  72.         Car.backWheel.acc.x = Car.backWheel.acc.x + .05
  73.         Car.frontWheel.acc.x = Car.frontWheel.acc.x + .05
  74.     END IF
  75.     IF _KEYDOWN(ASC("a")) THEN
  76.         Car.backWheel.acc.x = Car.backWheel.acc.x - .05
  77.         Car.frontWheel.acc.x = Car.frontWheel.acc.x - .05
  78.     END IF
  79.     IF _KEYDOWN(ASC(" ")) THEN
  80.         IF Car.backWheel.vel.x > 0 THEN Car.backWheel.vel.x = Car.backWheel.vel.x - .09
  81.         IF Car.backWheel.vel.x < 0 THEN Car.backWheel.vel.x = Car.backWheel.vel.x + .09
  82.  
  83.         IF Car.frontWheel.vel.x > 0 THEN Car.frontWheel.vel.x = Car.frontWheel.vel.x - .09
  84.         IF Car.frontWheel.vel.x < 0 THEN Car.frontWheel.vel.x = Car.frontWheel.vel.x + .09
  85.     END IF
  86.     IF _KEYDOWN(ASC("h")) THEN
  87.         IF Car.headlight.switched THEN Car.headlight.switched = 0 ELSE Car.headlight.switched = -1
  88.     END IF
  89.     updateCar Car
  90.     _LIMIT 40
  91.     _DISPLAY
  92.     CLS , 1
  93.     Car.backWheel.acc.x = 0
  94.     Car.frontWheel.acc.x = 0
  95.  
  96.     IF Car.frontWheel.pos.x < 800 AND Car.backWheel.pos.x > 0 THEN
  97.         Car.backWheel.pos.x = roadMap(Car.backWheel.pos2.x).x
  98.         Car.backWheel.pos.y = roadMap(Car.backWheel.pos2.x).y - 13
  99.  
  100.         Car.frontWheel.pos.x = roadMap(Car.frontWheel.pos2.x).x
  101.         Car.frontWheel.pos.y = roadMap(Car.frontWheel.pos2.x).y - 13
  102.     END IF
  103.  
  104. SUB drawMap (mapData() AS vec2)
  105. FOR i = 0 TO UBOUND(mapData) - 1
  106.     LINE (mapData(i).x, mapData(i).y)-(mapData(i + 1).x, mapData(i + 1).y), white&
  107.  
  108. SUB initCar (obj AS cars)
  109. obj.headlight.pos.x = obj.frontWheel.pos.x + 25
  110. obj.headlight.pos.y = obj.frontWheel.pos.y - 25
  111.  
  112. SUB drawCar (obj AS cars)
  113. 'we will draw body frames first, then, lights, followed by wheels.
  114.  
  115. LINE (obj.backWheel.pos.x - 30, obj.backWheel.pos.y - 40)-(obj.frontWheel.pos.x + 25, obj.frontWheel.pos.y - 40), white& 'up base
  116. LINE (obj.backWheel.pos.x + obj.backWheel.radius, obj.backWheel.pos.y)-(obj.frontWheel.pos.x - obj.frontWheel.radius, obj.frontWheel.pos.y), white& 'down base
  117.  
  118. '.....and some more..... little lines to cover down base.....
  119. LINE (obj.backWheel.pos.x - obj.backWheel.radius, obj.backWheel.pos.y)-(obj.backWheel.pos.x - 30, obj.backWheel.pos.y), white&
  120. LINE (obj.frontWheel.pos.x + obj.backWheel.radius, obj.frontWheel.pos.y)-(obj.frontWheel.pos.x + 25, obj.frontWheel.pos.y), white&
  121.  
  122. 'left & right of base
  123. LINE (obj.backWheel.pos.x - 30, obj.backWheel.pos.y)-(obj.backWheel.pos.x - 30, obj.backWheel.pos.y - 40), white&
  124. LINE (obj.frontWheel.pos.x + 25, obj.frontWheel.pos.y)-(obj.frontWheel.pos.x + 25, obj.frontWheel.pos.y - 40), white&
  125.  
  126. 'steeps
  127.  
  128. LINE (obj.backWheel.pos.x - 30, obj.backWheel.pos.y - 40)-(obj.backWheel.pos.x + 5, obj.backWheel.pos.y - 70), white&
  129. LINE (obj.frontWheel.pos.x + 25, obj.frontWheel.pos.y - 40)-(obj.frontWheel.pos.x - 5, obj.frontWheel.pos.y - 70), white&
  130.  
  131. 'roof
  132. LINE (obj.backWheel.pos.x + 5, obj.backWheel.pos.y - 70)-(obj.frontWheel.pos.x - 5, obj.frontWheel.pos.y - 70), white&
  133.  
  134. 'divider, to form window glasses
  135. 'line (obj.backWheel.pos.y - 70)-(obj.)
  136.  
  137. 'lights, x = cos0*r + xx, cos0 = x - xx / r
  138. IF obj.headlight.switched THEN
  139.     FOR i = 1 TO 150
  140.         h = map(i, 1, 150, 3, 30)
  141.         a = map(i, 1, 100, 200, 0)
  142.         LINE (obj.headlight.pos.x + i, obj.headlight.pos.y - h)-(obj.headlight.pos.x + i, obj.headlight.pos.y + h), _RGBA(255, 255, 255, a)
  143.     NEXT
  144. 'tyres
  145. CIRCLE (obj.backWheel.pos.x, obj.backWheel.pos.y), obj.backWheel.radius, white&
  146. CIRCLE (obj.frontWheel.pos.x, obj.frontWheel.pos.y), obj.frontWheel.radius, white&
  147.  
  148. LINE (COS(obj.backWheel.ang) * obj.backWheel.radius + obj.backWheel.pos.x, SIN(obj.backWheel.ang) * obj.backWheel.radius + obj.backWheel.pos.y)-(COS(obj.backWheel.ang + _PI) * obj.backWheel.radius + obj.backWheel.pos.x, SIN(obj.backWheel.ang + _PI) * obj.backWheel.radius + obj.backWheel.pos.y), white&
  149. LINE (COS(obj.frontWheel.ang) * obj.frontWheel.radius + obj.frontWheel.pos.x, SIN(obj.frontWheel.ang) * obj.frontWheel.radius + obj.frontWheel.pos.y)-(COS(obj.frontWheel.ang + _PI) * obj.frontWheel.radius + obj.frontWheel.pos.x, SIN(obj.frontWheel.ang + _PI) * obj.frontWheel.radius + obj.frontWheel.pos.y), white&
  150.  
  151.  
  152.  
  153. SUB updateCar (obj AS cars)
  154. obj.backWheel.pos.x = obj.backWheel.pos.x + obj.backWheel.vel.x
  155. obj.backWheel.pos.y = obj.backWheel.pos.y + obj.backWheel.vel.y
  156.  
  157. IF obj.backWheel.vel.x > 0 THEN obj.backWheel.vel.x = obj.backWheel.vel.x - .01
  158. IF obj.backWheel.vel.x < 0 THEN obj.backWheel.vel.x = obj.backWheel.vel.x + .01
  159.  
  160. obj.frontWheel.pos.x = obj.frontWheel.pos.x + obj.frontWheel.vel.x
  161. obj.frontWheel.pos.y = obj.frontWheel.pos.y + obj.frontWheel.vel.y
  162.  
  163. IF obj.frontWheel.vel.x > 0 THEN obj.frontWheel.vel.x = obj.frontWheel.vel.x - .01
  164. IF obj.frontWheel.vel.x < 0 THEN obj.frontWheel.vel.x = obj.frontWheel.vel.x + .01
  165.  
  166. obj.backWheel.vel.x = obj.backWheel.vel.x + obj.backWheel.acc.x
  167. obj.backWheel.vel.y = obj.backWheel.vel.y + obj.backWheel.acc.y
  168.  
  169. obj.frontWheel.vel.x = obj.frontWheel.vel.x + obj.frontWheel.acc.x
  170. obj.frontWheel.vel.y = obj.frontWheel.vel.y + obj.frontWheel.acc.y
  171.  
  172. IF ABS(obj.backWheel.vel.x) > .8 THEN
  173.     IF obj.backWheel.vel.x <> 0 THEN obj.backWheel.ang = obj.backWheel.ang + obj.backWheel.vel.x * 0.077
  174.  
  175. IF ABS(obj.frontWheel.vel.x) > 0 THEN
  176.     IF obj.frontWheel.vel.x <> 0 THEN obj.frontWheel.ang = obj.frontWheel.pos.x / Car.frontWheel.radius
  177. obj.headlight.pos.x = obj.frontWheel.pos.x + 25
  178. obj.headlight.pos.y = obj.frontWheel.pos.y - 25
  179.  
  180. obj.backWheel.pos2.x = INT(obj.backWheel.pos.x)
  181. obj.backWheel.pos2.y = INT(obj.backWheel.pos.y)
  182.  
  183. obj.frontWheel.pos2.x = INT(obj.frontWheel.pos.x)
  184. obj.frontWheel.pos2.y = INT(obj.frontWheel.pos.y)
  185.  
  186. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  187. map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  188.  
  189. FUNCTION dist! (x1!, y1!, x2!, y2!)
  190. dist! = SQR((x2! - x1!) ^ 2 + (y2! - y1!) ^ 2)
  191.  
  192. FUNCTION noise! (x AS SINGLE, y AS SINGLE, z AS SINGLE)
  193. STATIC p5NoiseSetup AS _BYTE
  194. STATIC perlin() AS SINGLE
  195. STATIC PERLIN_YWRAPB AS SINGLE, PERLIN_YWRAP AS SINGLE
  196. STATIC PERLIN_ZWRAPB AS SINGLE, PERLIN_ZWRAP AS SINGLE
  197. STATIC PERLIN_SIZE AS SINGLE
  198.  
  199. IF NOT p5NoiseSetup THEN
  200.     p5NoiseSetup = -1
  201.  
  202.     PERLIN_YWRAPB = 4
  203.     PERLIN_YWRAP = INT(1 * (2 ^ PERLIN_YWRAPB))
  204.     PERLIN_ZWRAPB = 8
  205.     PERLIN_ZWRAP = INT(1 * (2 ^ PERLIN_ZWRAPB))
  206.     PERLIN_SIZE = 4095
  207.  
  208.     perlin_octaves = 4
  209.     perlin_amp_falloff = 0.5
  210.  
  211.     REDIM perlin(PERLIN_SIZE + 1) AS SINGLE
  212.     DIM i AS SINGLE
  213.     FOR i = 0 TO PERLIN_SIZE + 1
  214.         perlin(i) = RND
  215.     NEXT
  216.  
  217. x = ABS(x)
  218. y = ABS(y)
  219. z = ABS(z)
  220.  
  221. xi = INT(x)
  222. yi = INT(y)
  223. zi = INT(z)
  224.  
  225. xf = x - xi
  226. yf = y - yi
  227. zf = z - zi
  228.  
  229. r = 0
  230. ampl = .5
  231.  
  232. FOR o = 1 TO perlin_octaves
  233.     DIM of AS SINGLE, rxf AS SINGLE
  234.     DIM ryf AS SINGLE, n1 AS SINGLE, n2 AS SINGLE, n3 AS SINGLE
  235.     of = xi + INT(yi * (2 ^ PERLIN_YWRAPB)) + INT(zi * (2 ^ PERLIN_ZWRAPB))
  236.  
  237.     rxf = 0.5 * (1.0 - COS(xf * _PI))
  238.     ryf = 0.5 * (1.0 - COS(yf * _PI))
  239.  
  240.     n1 = perlin(of AND PERLIN_SIZE)
  241.     n1 = n1 + rxf * (perlin((of + 1) AND PERLIN_SIZE) - n1)
  242.     n2 = perlin((of + PERLIN_YWRAP) AND PERLIN_SIZE)
  243.     n2 = n2 + rxf * (perlin((of + PERLIN_YWRAP + 1) AND PERLIN_SIZE) - n2)
  244.     n1 = n1 + ryf * (n2 - n1)
  245.  
  246.     of = of + PERLIN_ZWRAP
  247.     n2 = perlin(of AND PERLIN_SIZE)
  248.     n2 = n2 + rxf * (perlin((of + 1) AND PERLIN_SIZE) - n2)
  249.     n3 = perlin((of + PERLIN_YWRAP) AND PERLIN_SIZE)
  250.     n3 = n3 + rxf * (perlin((of + PERLIN_YWRAP + 1) AND PERLIN_SIZE) - n3)
  251.     n2 = n2 + ryf * (n3 - n2)
  252.  
  253.     n1 = n1 + (0.5 * (1.0 - COS(zf * _PI))) * (n2 - n1)
  254.  
  255.     r = r + n1 * ampl
  256.     ampl = ampl * perlin_amp_falloff
  257.     xi = INT(xi * (2 ^ 1))
  258.     xf = xf * 2
  259.     yi = INT(yi * (2 ^ 1))
  260.     yf = yf * 2
  261.     zi = INT(zi * (2 ^ 1))
  262.     zf = zf * 2
  263.  
  264.     IF xf >= 1.0 THEN xi = xi + 1: xf = xf - 1
  265.     IF yf >= 1.0 THEN yi = yi + 1: yf = yf - 1
  266.     IF zf >= 1.0 THEN zi = zi + 1: zf = zf - 1
  267. noise! = r
  268.  
  269. SUB noiseDetail (lod!, falloff!)
  270. IF lod! > 0 THEN perlin_octaves = lod!
  271. IF falloff! > 0 THEN perlin_amp_falloff = falloff!
  272.  
« Last Edit: July 15, 2017, 11:37:52 am by STxAxTIC »
You're not done when it works, you're done when it's right.

FellippeHeitor

  • Guest
Re: Car : Physics Test
« Reply #2 on: July 15, 2017, 11:12:52 am »
That works really well! Good job there, Ashish!

It's a bit hard to turn on/off the lights, as they flicker if you hold "h" a little longer.

Nice how the brakes aren't the hard-artificial type I was expecting, but a more natural "slow it down" thing.

Fellippe.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Car : Physics Test
« Reply #3 on: July 15, 2017, 02:24:56 pm »
Super job, Ashish. Thank for the link! We all here are newbie :-D

i upgrade your 3 lines of code (line 94, 95, 96) for better light switching. Is the same case as in my QB64Player:

Code: QB64: [Select]
  1.     IF _KEYDOWN(ASC("h")) AND TIMER > t THEN
  2.         IF Car.headlight.switched THEN Car.headlight.switched = 0: t = TIMER + .2 ELSE Car.headlight.switched = -1: t = TIMER + .2
  3.     END IF
  4.  
« Last Edit: July 15, 2017, 03:08:06 pm by Petr »

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Car : Physics Test
« Reply #4 on: July 16, 2017, 12:38:34 am »
@STxAxTIC
I like your modification. I was stuck at that problem and I was unable to calculate the rotation of the wheel. So, I used it to rotate with velocity ( which was not accurate!).
By the way, on the line 191, it should be
Code: QB64: [Select]
  1.     IF obj.frontWheel.vel.x <> 0 THEN obj.frontWheel.ang = obj.frontWheel.pos.x / obj.frontWheel.radius
  2.  

instead of

Code: QB64: [Select]
  1.     IF obj.frontWheel.vel.x <> 0 THEN obj.frontWheel.ang = obj.frontWheel.pos.x / Car.frontWheel.radius
  2.  

right? :)

@FellippeHeitor
Thank you. :)

@Petr
Thank you. :)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Car : Physics Test
« Reply #5 on: July 16, 2017, 01:01:29 am »
Nice catch Ashish - Yeah, I was going a little too fast when looking for the variable that contained 1/.077=13. Your new correction is probably the better form.
You're not done when it works, you're done when it's right.