Snippet Recording motion

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,280
This is a simple way of recording motion into an array to be used later. You could save this array and load in your game to use as raw material
for the motion of enemies or anything. Or you could could just toy around with it to see how much you can record into a tight space.

Here we use 65535 integers to record about 1092 seconds (A little bit more than 18min) of motion!

We play it back backwards from where you left off the last time recording as that is
a really nice way to get new kinds of motions.

Cerberus:
Strict
Import mojo2

Global data : Int[65535]    ' Create some space to record motion.
Global sp:Int = 32768       ' 65535 gives us 1092 seconds @ 60 fps. Set pointer in the middle.

Function Main:Int()
    New Game
    Return 0
End

Class Game Extends App

    Field canvas:Canvas

    Method OnCreate:Int()
        canvas=New Canvas            
        SetSwapInterval 1 ; SetUpdateRate 0
        Return 0
    End

    Method OnRender:Int()
        canvas.Clear 0,0,0,0
       
        ' Record ------------------
         Local xxx:Float = MouseX()
        Local yyy:Float = MouseY()
        If MouseDown(0) = False '  Record motion when not holding down mousebutton.
            canvas.SetColor 1,0,0
            If (Millisecs()/100) Mod 10 > 5 Then canvas.DrawText "Recording",0,0 ' Blink the text recording
            canvas.SetColor 1,1,1
       
            ' Save  position
            sp = sp - 1 ; data[sp] = ( (xxx Shl 14) + yyy)
            If sp = 0 Then sp = 65535 ' Make sure so stackpointer is within range
        ' --------------------------
        Else
        ' Playback ------ ' Playback motion (not from start but backwards from the moment you just recorded!) when not holding down mousebutton.
   
            ' Restore position
             xxx = (data[sp] Shr 14) & 16383 ; yyy = data[sp] & 16383 ; sp = sp + 1
             If sp = 65535 Then sp = 0 ' Make sure so stackpointer is within range
       
             canvas.SetColor 0,1,0
             canvas.DrawText "Playback",0,0
       
         ' -----------------------------
        Endif
   
        canvas.SetColor 1,1,1
        canvas.DrawRect xxx-16,yyy-16,32,32 ' Draw centered box
       
        canvas.Flush
        Return 0
    End

End
 
Last edited:

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,280
For completeness, here's a version that plays your sequence back as it was recorded, and loops it.

Cerberus:
Strict
Import mojo2

Global data : Int[65535]    ' Create some space to record motion.
Global sp:Int = 32768       ' 65535 gives us 1092 seconds @ 60 fps. Set pointer in the middle.
Global recording:Bool = False
Global length:Int = 0

Function Main:Int()
    New Game
    Return 0
End

Class Game Extends App

    Field canvas:Canvas

    Method OnCreate:Int()
        canvas=New Canvas           
        SetSwapInterval 1 ; SetUpdateRate 0
        Return 0
    End

    Method OnRender:Int()
        canvas.Clear 0,0,0.5,0
      
        ' Record ------------------
        Local xxx:Float = MouseX()
        Local yyy:Float = MouseY()
        If MouseDown(0) = True '  Record motion when not holding down mousebutton.
          If recording = False Then sp =32768 ; recording = True
            canvas.SetColor 1,0,0
            If recording = True And (Millisecs()/100) Mod 10 > 5 Then canvas.DrawText "Recording",20,0 ' Blink the text recording
            canvas.SetColor 1,1,1
            ' Save  position
            sp = sp - 1 ; data[sp] = ( (xxx Shl 14) + yyy)
            If sp = 0 Then sp = 65535 ' Make sure so stackpointer is within range
        ' --------------------------
        Else
        ' Playback ------ ' Playback motion (not from start but backwards from the moment you just recorded!) when not holding down mousebutton.
               If recording = True Then length = sp ; sp = 32768 ; recording = False
            ' Restore position

            If sp = length Then sp = 32768 ' Loop the last recording, whatever the length was.
            
             sp = sp - 1 ; xxx = (data[sp] Shr 14) & 16383 ; yyy = data[sp] & 16383
            If sp = 65535 Then sp = 0 ' Make sure so stackpointer is within range
             canvas.SetColor 0,1,0
             canvas.DrawText "Press to record, release for playback",20,0
         ' -----------------------------
        Endif
  
        canvas.SetColor 1,1,1
        canvas.DrawRect xxx-16,yyy-16,32,32 ' Draw centered box
        canvas.Flush
        Return 0
    End

End
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
726
This is interesting! Thanks for sharing your ideas and the general activity you bring to the forum.
When I read this it triggered some ideas about motion in games and maybe an animation editor.
- What about checking the data you recorded, if they could be interpolated with bezier curves or some other tweening function.
- What about recording one movement multiple times and calculate something like an average of all recordings to get to an ideal motion.
- What, if you could use those motions for particles or animations.
- In an animation editor similar to spriter or spine you could ommit a lot of the gui, if you could just influence the motion in real time. It's like going from old school 3d object creation going to sculpting objects, but for animation.
 

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,280
Thanks. Yes that's I'm looking into 3D actually.

I was thinking to do something like "additive" motion. You record something and then you add relative motion onto that multiple times as you wish That way you can "sketch" an animation.
 
Top Bottom