Line follow the character

Here’s the scenario: 

i have a main character that walks in all directions (up, down, left, right).

What i want to do is: when the user touch the screen a line follows the character like he’s holding a rope and walking, you know? Then release that rope when the user touches the screen again.

That line has to be a physics object.

Does someone have a clue how can i achieve that?  

Do you means something like this game? Skip to about 0:30 to see what I mean

https://www.youtube.com/watch?v=dj05By78Qjg

PS - This game was made w/ Corona SDK by the way.

Actually no :frowning:

1 ) The main character is idle at some point;

2 ) The user touch the screen and start walking with the sprite; 

3 ) 

                                B) (sprite walking)
(rope/line)-----------    /|\
                                  |\

until the user touches the screen again so that rope stop following him.

Is it more clear now?

Hi @Ziad.Ali,

One important question is: does this “rope” need to be flexible, like a real-world rope? Meaning, does it need to bend/flex, or can it just be a straight rigid line?

Brent

Hi @Brent,

actually it has to be a straight rigid line…

Any idea?

Hi again,
OK, and can the line remain the same length? If so, I would just create the rope object using a very thin rectangle object (or an image, if you want), make it into a physical object, then attach it to the player using a “pivot” joint. Then, when you want to release the rope, just destroy that joint. Here’s a guide on physics joints:

http://docs.coronalabs.com/guide/physics/physicsJoints/index.html

Brent

@Brent

I did what you told me… 

in the hybrid mode i can see a line following my player just the way i wanted, but…

the rect doesn’t follow him.

Is there a way to make it big as the player is moving? or even create multiple rects as he’s moving to make the “rope” bigger.

Hi @Ziad.Ali,

Can you post the basic code that you’re using now? Please try to isolate this to the code that pertains to the sprite and rope, not anything like Composer/Storyboard scenes or anything that doesn’t matter for this thread.

Thanks,

Brent

Alright, let’s try to keep it simple…

After i do all the declarations of the sprite, sequenceData etc… (using texturepacker), 

i add physics like this:

sprite = display.newSprite( myImageSheet, sequenceData ) sprite.x = display.contentCenterX sprite.y = display.contentCenterY sprite.myName = "sprite"; physics.addBody( sprite, "static");

and the rect: 

local rope = display.newRect( 0, 0, 10, 3 ); rope:setFillColor( 0.5 ) rope:setStrokeColor( 1, 0, 0 ); rope.x = sprite.x - 50; rope.y = sprite.y; rope.myName = "rope"; physics.addBody( rope, "dynamic" );

the joint: 

local pivotJoint = physics.newJoint( "pivot", sprite, rope, sprite.x, sprite.y 

UPDATE —

See next post, please.

@Brent

Now i see your question: 

“OK, and can the line remain the same length?” 

Sorry, actually no, and i think that’s why my code is not working the way i want… 

What i want is this:

The player goes to the point A, ok?

When he reaches the point A and touch the screen, a rope is gonna attach to the player so he could move this rope/line to the point B. Keep in mind, the rope will get bigger as the player move. Each step of the player is gonna add extra length to the rope… 

When he reaches the point B that rope/line will separate from the player.

Hi @Ziad.Ali,

If you need to change the size/length of the rope as the player walks, you will probably need to add/subtract smaller units from it. So, it would be like you’re construction a “chain” of small physics objects, as shown in our “Chains” sample project, but since you don’t want this rope to flex and bend, you could attach each segment to the next using a “weld” joint (not a “pivot” joint).

Brent

Do you means something like this game? Skip to about 0:30 to see what I mean

https://www.youtube.com/watch?v=dj05By78Qjg

PS - This game was made w/ Corona SDK by the way.

Actually no :frowning:

1 ) The main character is idle at some point;

2 ) The user touch the screen and start walking with the sprite; 

3 ) 

                                B) (sprite walking)
(rope/line)-----------    /|\
                                  |\

until the user touches the screen again so that rope stop following him.

Is it more clear now?

Hi @Ziad.Ali,

One important question is: does this “rope” need to be flexible, like a real-world rope? Meaning, does it need to bend/flex, or can it just be a straight rigid line?

Brent

Hi @Brent,

actually it has to be a straight rigid line…

Any idea?

Hi again,
OK, and can the line remain the same length? If so, I would just create the rope object using a very thin rectangle object (or an image, if you want), make it into a physical object, then attach it to the player using a “pivot” joint. Then, when you want to release the rope, just destroy that joint. Here’s a guide on physics joints:

http://docs.coronalabs.com/guide/physics/physicsJoints/index.html

Brent

@Brent

I did what you told me… 

in the hybrid mode i can see a line following my player just the way i wanted, but…

the rect doesn’t follow him.

Is there a way to make it big as the player is moving? or even create multiple rects as he’s moving to make the “rope” bigger.

Hi @Ziad.Ali,

Can you post the basic code that you’re using now? Please try to isolate this to the code that pertains to the sprite and rope, not anything like Composer/Storyboard scenes or anything that doesn’t matter for this thread.

Thanks,

Brent

Alright, let’s try to keep it simple…

After i do all the declarations of the sprite, sequenceData etc… (using texturepacker), 

i add physics like this:

sprite = display.newSprite( myImageSheet, sequenceData ) sprite.x = display.contentCenterX sprite.y = display.contentCenterY sprite.myName = "sprite"; physics.addBody( sprite, "static");

and the rect: 

local rope = display.newRect( 0, 0, 10, 3 ); rope:setFillColor( 0.5 ) rope:setStrokeColor( 1, 0, 0 ); rope.x = sprite.x - 50; rope.y = sprite.y; rope.myName = "rope"; physics.addBody( rope, "dynamic" );

the joint: 

local pivotJoint = physics.newJoint( "pivot", sprite, rope, sprite.x, sprite.y 

UPDATE —

See next post, please.

@Brent

Now i see your question: 

“OK, and can the line remain the same length?” 

Sorry, actually no, and i think that’s why my code is not working the way i want… 

What i want is this:

The player goes to the point A, ok?

When he reaches the point A and touch the screen, a rope is gonna attach to the player so he could move this rope/line to the point B. Keep in mind, the rope will get bigger as the player move. Each step of the player is gonna add extra length to the rope… 

When he reaches the point B that rope/line will separate from the player.