Cape simulation

Tweet 2d monkey-x side-scroller code-archives effects
(Posted 3 months ago) RonTek

Just had this crazy idea, to do a basic 2D cape simulation... found nothing on Google, so this is what I came up with: the game is a bunch of segments, as as the root segment moves, all child segments are updated accordingly.

It kinda of work... but I think I'm missing something, the movement isn't fluid enough.

Author: SLotman


Import mojo

#If TARGET="glfw"
#GLFW_WINDOW_TITLE="Cape Simulation Test"

Global gravity:Float = 0.7

Class CapePoint
Field x:Float, y:Float

Method New()
End Method

Method New(x:Float, y:Float)
Self.x = x
Self.y = y
End Method
End Class

Class CapeNode
Field p1:CapePoint
Field p2:CapePoint
Field child:CapeNode
Field length:Float
Field transmission:Float ' how much energy passes from one node to another

Method New(x:Float, y:Float, length:Float)
Self.p1 = New CapePoint(x,y)
Self.p2 = New CapePoint(x,y+length)
Self.length = length
Self.child = Null
End Method

Method draw()
For Local c:Int=0 To 19
DrawLine p1.x+c, p1.y, p2.x+c, p2.y 

If (child) Then child.draw()    
End Method

Method move(x:Float, y:Float)
' here the magic happens
p1.x +=x
p1.y +=y 
End Method

Method update(parent:CapeNode=Null)
If parent Then 
p1.x = parent.p2.x
p1.y = parent.p2.y 
End If

p2.y +=gravity*2.5
If (p1.x - p2.x) > length Then 
p2.x = p1.x - length 
p2.x += (p1.x - p2.x)*transmission
End If  

If Abs(p2.y - p1.y) > length Then 
If p1.y > p2.y Then p2.y = p1.y - length Else p2.y = p1.y + length 
If p2.y < p1.y+length Then p2.y+=gravity + 2*(0.5 + transmission)
End If  

If (child) Then child.update(Self)
End Method
End Class

Class Cape
Field root:CapeNode

Method setPos(x:Float, y:Float)
root.p1.x = x
root.p1.y = y
End Method

Method move(x:Float, y:Float)
End Method

Method draw()
End Method

Method update()
End Method
End Class

Class Player
Field x:Float, y:Float
Field speedX:Float, speedY:Float

Field cape:Cape

Method New(x:Float, y:Float)
Self.x = x
Self.y = y

Self.cape = New Cape()
Self.cape.root = New CapeNode(x-35,y-10, 10)
Self.cape.root.transmission = 0.5

Self.cape.root.child = New CapeNode(x-35,y,15)
Self.cape.root.child.transmission = 0.4

Self.cape.root.child.child = New CapeNode(x-35,y+15,20)
Self.cape.root.child.child.transmission = 0.3

Self.cape.root.child.child.child = New CapeNode(x-35,y+35,20)
Self.cape.root.child.child.child.transmission = 0.25
End Method

Method update()
x += speedX
y += speedY
cape.move(speedX, speedY)

If Abs(speedX)<0.05 Then speedX=0

If Abs(speedY)<0.05 Then speedY=0

End Method

Method draw()
'DrawCircle x, y, 30
SetColor 0,0,255
DrawRect x-25,y-15,15,80
DrawRect x-10,y+55,10,10

SetColor 208,148,0
DrawRect x-25, y-35,25,20

SetColor 255,0,0
SetColor 255,255,255
End Method
End Class

Class Game Extends App
Field player:Player

Method OnCreate()
player = New Player(100,300)
player.speedX = 0
End Method

Method OnRender()
DrawText "Cape simulation test",10,10
End Method

Method OnUpdate()

If KeyDown(KEY_RIGHT) And player.speedX<4 Then player.speedX+=1.0
If KeyDown(KEY_LEFT) And player.speedX>-4 Then player.speedX-=1.0

If KeyDown(KEY_UP) And player.speedY<4 Then player.speedY-=1.0
If KeyDown(KEY_DOWN) And player.speedY>-4 Then player.speedY+=1.0

If KeyHit(KEY_ESCAPE) Then Error ""
End Method
End Class

Function Main()
New Game()
End Function

Reply To Topic (minimum 10 characters)

Please log in to reply