5 Replies Latest reply on Apr 26, 2012 9:59 AM by neal204

# Creating a 'rope' connecting two sprites which rotates dynamically

Hi, I'm working on a game which involves the character shooting a hook which is connected to the character by a rope. the character follows the hook once it lands and hangs beneath it. The trouble is getting a rope to connect the rope and the sprite and rotate dynamically with the changing position. I already have code changing the length of the rope sprite to fit and position it between the hook and the character, the problem is getting the sprite to rotate properly so it is always connected to the character and the hook. any ideas? already tried calculating the angle and adding that to the sprite.rotation value, pretty stuck.

• ###### 1. Re: Creating a 'rope' connecting two sprites which rotates dynamically

Did you set the registration point of the rope bmp to the end that is "connected" to the hook?  I'm guessing that is the point you want to rotate about.

Calculating the angle and using sprite.rotation sounds like the way to do it. Someone might be able to help you with your code if you post it.

1 person found this helpful
• ###### 2. Re: Creating a 'rope' connecting two sprites which rotates dynamically

i have been trying that, but the problem seems to be lingos math functions. using atan(o/a) to find the angle always results in zero. the rope is set to be in the mid point of the character and the hook, as the length changes dynamically to stretch between them. i set the .height of the sprite to the distance between the char and hook. ill post the code when i get on director.

• ###### 3. Re: Creating a 'rope' connecting two sprites which rotates dynamically

----   using atan(o/a) to find the angle always results in zero----

you must not use integers in the division.

Maybe that's why the result is zero.

e.g.

integer:       2/3 = 0

float:            2.0 / 3.0 = 0.66666666666

• ###### 4. Re: Creating a 'rope' connecting two sprites which rotates dynamically

Here's a sample behavior that may do what you are trying to do. I used a black horizontal line for my "rope" with its reg point set to the left side. The code changes the rope's width, but you can change that to height.

--  Rope Connecter Behavior

property  pMe

property  pRotationPointSprite

property  pSpriteToTrack

On GetPropertyDescriptionList me

Props = [:]

Props[#pRotationPointSprite] = [#default:sprite(1), #format:#sprite,  #comment:"Sprite that rope rotates about."]

Props[#pSpriteToTrack] = [#default:sprite(1), #format:#sprite,  #comment:"Sprite that the free moving end of the rope tracks."]

return Props

end GetPropertyDescriptionList

on beginSprite me

pMe = sprite(me.spriteNum)

me.enterFrame()

end beginSprite

on enterFrame me

AnchorLoc = pRotationPointSprite.loc

TrackLoc = pSpriteToTrack.loc

pMe.loc = AnchorLoc

DeltaH = TrackLoc.locH - AnchorLoc.locH

DeltaV = TrackLoc.locV - AnchorLoc.locV

RopeLength = sqrt(DeltaH * DeltaH + DeltaV * DeltaV)

pMe.width = RopeLength

pMe.rotation = me.getAngle(DeltaH, DeltaV)

end enterFrame

on getAngle me, deltaH, deltaV

if deltaH <> 0 then

slope = float (deltaV) / deltaH

angle = atan (slope)

if deltaH  < 0 then

angle = angle + pi

end if

if angle < 0 then

angle = angle + (2 * pi)

end if

else if deltaV > 0 then

angle = pi / 2

else if deltaV < 0 then

angle = (3 * pi) / 2

else

angle = 0

end if

return angle * 180 / pi

end getAngle

1 person found this helpful
• ###### 5. Re: Creating a 'rope' connecting two sprites which rotates dynamically

thanks alot, this seems to have fixed most of the issues, the rope takes a little while to update its rotation on longer distances, but other than that its great, thanks alot