Rope thing

Discussions related to graphics (2D and 3D), animation and games programming
David Williams

Rope thing

Post by David Williams »

Code: Select all

      REM Verlet rope thing
      REM Works with BB4W

      *FLOAT 64

      ON ERROR PROCerror( REPORT$ + " at line " + STR$ERL )

      MODE 8 : OFF
      WinW% = @vdu%!208
      WinH% = @vdu%!212

      `Sleep` = SYS("Sleep")

      nPts% = 50
      DIM Point{( nPts% ) x#, y#, oldx#, oldy# }

      theta = 0
      pointSepDist% = 5
      PROCGetHeadPos( theta, x0#, y0# )
      PROCInitRope( Point{()}, nPts%, x0#, y0#, pointSepDist% )

      *REFRESH OFF

      REPEAT
  
        CLS
  
        PROCGetHeadPos( theta, headX#, headY# )
        theta += 0.005125
  
        PROCUpdateRopePoints( Point{()}, nPts%, headX#, headY#, 5, 0.01, 0.9995, 1 )
  
        GCOL 6
        VDU 23,23,2|
        FOR I% = 0 TO nPts%-2
          LINE 2*Point{(I%)}.x#, 2*Point{(I%)}.y#, 2*Point{(I%+1)}.x#, 2*Point{(I%+1)}.y#
        NEXT I%
        VDU 23,23,1|
  
        GCOL 14 : CIRCLE FILL 2*Point{(0)}.x#, 2*Point{(0)}.y#, 12
  
        *REFRESH
        SYS `Sleep`, 1
  
      UNTIL FALSE
      END

      DEF PROCUpdateRopePoints( p{()}, nPts%, headX#, headY#, sepDist%, grav#, fric#, nIter% )
      LOCAL I%, J%, vx#, vy#, dx#, dy#, dist#, diff#, perc#, offX#, offY#

      p{(0)}.x# = headX#
      p{(0)}.y# = headY#
      p{(0)}.oldx# = p{(0)}.x#
      p{(0)}.oldy# = p{(0)}.y#

      FOR I% = 0 TO nPts%-1
        vx# = (p{(I%)}.x# - p{(I%)}.oldx#) * fric#
        vy# = (p{(I%)}.y# - p{(I%)}.oldy#) * fric#
        p{(I%)}.oldx# = p{(I%)}.x#
        p{(I%)}.oldy# = p{(I%)}.y#
        p{(I%)}.x# += vx#
        p{(I%)}.y# += vy#
        p{(I%)}.y# -= grav#
      NEXT I%

      FOR J% = 1 TO nIter%
        FOR I% = 0 TO nPts%-2
          dx# = p{( I%+1 )}.x# - p{( I% )}.x#
          dy# = p{( I%+1 )}.y# - p{( I% )}.y#
          dist# = SQR(dx#*dx# + dy#*dy#)
          diff# = sepDist% - dist#
          perc# = 0.5 * diff# / dist#
          offX# = dx# * perc#
          offY# = dy# * perc#
          p{( I% )}.x# -= offX#
          p{( I% )}.y# -= offY#
          p{( I%+1 )}.x# += offX#
          p{( I%+1 )}.y# += offY#
        NEXT I%
      NEXT J%

      dx# = p{(0)}.x# - headX#
      dy# = p{(0)}.y# - headY#
      FOR I% = 0 TO nPts%-1
        p{(I%)}.x# -= dx#
        p{(I%)}.y# -= dy#
        p{(I%)}.oldx# -= dx#
        p{(I%)}.oldy# -= dy#
      NEXT I%
      ENDPROC

      DEF PROCInitRope( p{()}, nPts%, x0%, y0%, sepDist% )
      LOCAL I%
      FOR I% = 0 TO nPts%-1
        p{(I%)}.x# = x0%
        p{(I%)}.y# = y0% - I%*sepDist%
        p{(I%)}.oldx# = x0%
        p{(I%)}.oldy# = y0% - I%*sepDist%
      NEXT I%
      ENDPROC

      DEF PROCGetHeadPos( theta, RETURN x#, RETURN y# )
      x# = 0.5*WinW% + 0.5*WinW%*SIN(0.9251*theta)*SIN(1.2563*theta + 0.45)*SIN(0.7162*theta - 0.712)
      y# = 0.6*WinH% + 0.5*WinH%*COS(0.5126*theta)*COS(0.4525*theta - 1.53)*COS(1.4267*theta + 2.123)
      ENDPROC

      DEF PROCerror( s$ )
      OSCLI "REFRESH ON" : CLS : ON : VDU 7
      PRINT '"  " + s$;
      REPEAT : WAIT 2 : UNTIL FALSE
      ENDPROC

I needed this for a game project, so I had to do a refresher on so-called Verlet integration (see https://www.youtube.com/watch?v=3HjO_RGIjCU&t=691s ). Excuse the excessive number of # suffixes - it was originally in preparation for translation to C (my next task!).
MrHiggins
Posts: 19
Joined: Wed 28 Jul 2021, 13:29

Re: Rope thing

Post by MrHiggins »

Very nice! Thanks for sharing.
David Williams

Re: Rope thing

Post by David Williams »

Here's why I needed that rope!

https://youtu.be/T3YlwK4lU1Y

(That's all work-in-progress by the way, as made obvious by the awful placeholder graphics...)
DDRM

Re: Rope thing

Post by DDRM »

Possibly SyntaxBomb related? Plane takes a serpentine path on its own, and you have to choose when to fire?
David Williams

Re: Rope thing

Post by David Williams »

DDRM wrote: Fri 10 Sep 2021, 22:04 Possibly SyntaxBomb related? Plane takes a serpentine path on its own, and you have to choose when to fire?
Yeah, that's pretty much it. Long-pressing the one-and-only allowed key drops a bomb, tapping it fires a missile. It takes some getting used to.
DDRM

Re: Rope thing

Post by DDRM »

Ah, sneaky!

I'm thinking about a "Duelling cowboys" sort of game, a bit like space invaders but different. My idea about a spinning spaceship feels more interesting to write, but less interesting as a game...

:-)

D
David Williams

Re: Rope thing

Post by David Williams »

DDRM wrote: Sun 12 Sep 2021, 16:00 I'm thinking about a "Duelling cowboys" sort of game, a bit like space invaders but different. My idea about a spinning spaceship feels more interesting to write, but less interesting as a game...
So... have you made a start yet? October 24th is racing towards us all too rapidly. :shock:

If I wasn't so consumed by my own game project, I'd offer to do the graphics side of things for you (if you required some assistance in that regard), not that I'm great at graphics myself of course.

Anyway, good luck to you if you do make a go of it; we're up against some stiff competition (Stevie G and iWasAdam's work-in-progress efforts look bloody excellent, IMO - to the extent that they've got me rather worried!).
DDRM

Re: Rope thing

Post by DDRM »

Yes, I have the basics working, though the AI for the opposition is going to be tricky - finding a balance between an unbeatable and a pathetic AI is always challenging!

The graphics will be very basic - I'll probably stick to the original "lines only" approach, rather than using "sprite" images.

I'm not even trying to compete with the big boys over there - for me it's more about making an interesting game with 1-button control.

D