Hi all,
if this isn't the correct place to post this, I apologize. I just thought I'd share my for those who might find this useful in their current projects.
I haven't really played around 2D physics related projects that much but I have always wanted to implement some sort of rope physics in my games and I thought I'd experiment a little with it.
Below is the code I have currently, feel free to copy and use as you please. What the code does is that it create a small amount of rope sprites connected with distance joints with each other, and one of the sprites is connected to a static sprite. This makes the rope hang freely. You can drag any part of the rope with a mouse and move the rope freely. I'll upload the rope image as well.
Here's the code:
// set window properties
SetWindowTitle( "RopeTest" )
SetWindowSize( 1024, 768, 0 )
// set display properties
//SetDisplayAspect( 1024.0/768.0 )
SetIntendedDeviceSize( 1024, 768 )
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 0, 0, 1, 0 )
SetSyncRate(0, 1)
SetPhysicsScale(0.01)
_Anchor = CreateSprite(0)
SetSpriteSize(_Anchor, 10, 10)
SetSpritePosition(_Anchor, GetDeviceWidth()/2 - GetSpriteWidth(_Anchor)/2, 0)
SetSpritePhysicsOn(_Anchor, 1)
_Rope = CreateSprite(LoadImage("rope.png"))
SetSpriteScale(_Rope, 0.3, 0.05)
SetSpritePosition(_Rope, GetDeviceWidth()/2 - GetSpriteWidth(_Rope)/2, 15)
SetSpritePhysicsMass(_Rope, 0.0001)
SetSpritePhysicsOn(_Rope, 2)
// Create distance joint between the anchor sprite and first rope section
CreateDistanceJoint(1, _Anchor, _Rope, GetSpriteX(_Anchor) + GetSpriteWidth(_Rope)/2, GetSpriteY(_Anchor) + GetSpriteHeight(_Anchor)/2, GetSpriteX(_Rope) + GetSpriteWidth(_Rope)/2, GetSpriteY(_Rope), 0)
// rest of the rope pieces
id_prev = _Rope
for i = 0 to 10
id = CloneSprite(id_prev)
SetSpritePosition(id, GetSpriteX(id_prev), GetSpriteY(id_prev) + GetSpriteHeight(id_prev)+2)
SetSpritePhysicsOn(id, 2)
CreateDistanceJoint(i+2, id_prev, id, GetSpriteX(id_prev) + GetSpriteWidth(id_prev)/2, GetSpriteY(id_prev) + GetSpriteHeight(id_prev), GetSpriteX(id) + GetSpriteWidth(id)/2, GetSpriteY(id), 0)
id_prev = id
next i
//SetPhysicsDebugOn()
do
px# = GetPointerX()
py# = GetPointerY()
// register initial mouse click
if GetPointerPressed() = 1
id = getSpriteHit(px#, py#)
mouseJoint = CreateMouseJoint(id, px#, py#, 10000)
else
if mouseJoint > 0
if GetPointerState() = 1
SetJointMouseTarget(mouseJoint, px#, py#)
else
DeleteJoint(mouseJoint) : mouseJoint = 0
endif
endif
endif
Print( ScreenFPS() )
Sync()
loop
Mah machine: Intel Xeon E3-1231v3 4x3.40GHz | CM Hyper 212 Evo | ASRock H97 Pro4 | 240GB SanDisk SSD | Sapphire Radeon R9 270X Dual-X 2048MB | Seagate HDD 1000GB |
8GB Crucial Ballistix Sport DDR3-1600