Be a Supporter!

How to attach segments together?

  • 291 Views
  • 10 Replies
New Topic Respond to this Topic
Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
How to attach segments together? 2013-01-04 13:51:48 Reply

What it looks like right now.

I'm having a heck of a time making line segments follow other line segments. So far, I can have each section in an array follow the one before it and make the segments rotate towards the angle of the previous segment I can even keep them spaced apart (but I'm having issues with that).

I feel as if I could designate one end of the segment as the "front" and the other the "back", it would save me a lot of trouble in the long run. That way, I could just have the front of each segment follow the back of the previous and I wouldn't even have to worry about spacing all too much.

Normally, finding the top or bottom of an object is easy if the object is static. I would just find it's position on the y-axis in comparison to another object, but since these objects would be moving all the time, the "front" wouldn't always be in the same position. I could always just add MovieClips to each end and give them instance names of front and back, but that seems like something I'd do a year ago. How can I do this dynamically?

How to attach segments together?

4urentertainment
4urentertainment
  • Member since: Aug. 1, 2008
  • Offline.
Forum Stats
Moderator
Level 13
Game Developer
Response to How to attach segments together? 2013-01-04 14:11:37 Reply

Are you trying to do something like this?

http://www.newgrounds.com/dump/item/880523a5250badda576bd23a 2b382459

Getting the front and back is really easy.

var frontX:Number = object.x + Math.cos(angle) * width
var frontY:Number = object.y + Math.sin(angle) * height

Using trignometry, you resolve the height and width, and there you go. For the back you would subtract the cos and sin instead of adding.

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to How to attach segments together? 2013-01-04 14:15:37 Reply

What 4 showed you is called polar coordinates and it is really simple, since cosine and sine will bring you all range of values between -1 and 1, you will have negative and positive, multiplying by the radius the polar transfter the coordinate to the specific angle. You can achieve the same effect running less calculations with vector normalizations.

ScrumTurd
ScrumTurd
  • Member since: Sep. 10, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to How to attach segments together? 2013-01-04 14:45:57 Reply

If you don't need those sprites / movieClips you can just use graphics.curveTo:
http://www.newgrounds.com/dump/item/8d585e638c224bcd4e015f53 d9ee1929
http://gskinner.com/blog/archives/2008/05/drawing_curved_.ht ml

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to How to attach segments together? 2013-01-04 14:55:07 Reply

At 1/4/13 02:45 PM, ScrumTurd wrote: If you don't need those sprites / movieClips you can just use graphics.curveTo:
http://www.newgrounds.com/dump/item/8d585e638c224bcd4e015f53 d9ee1929
http://gskinner.com/blog/archives/2008/05/drawing_curved_.ht ml

Although curves consume a lot more of FPS and require too many calculations with expoents, since the function used is the Bezier curve. It is a lot easier to just use some trigonometry or vector operations to achieve the effect.

Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to How to attach segments together? 2013-01-04 18:26:13 Reply

At 1/4/13 02:11 PM, 4urentertainment wrote: Are you trying to do something like this?

http://www.newgrounds.com/dump/item/880523a5250badda576bd23a 2b382459

Getting the front and back is really easy.

var frontX:Number = object.x + Math.cos(angle) * width
var frontY:Number = object.y + Math.sin(angle) * height

Using trignometry, you resolve the height and width, and there you go. For the back you would subtract the cos and sin instead of adding.

Yes, that's quite close to what I'm trying to do. The "angle" variable is just the angle of the current segment? So, "ropeSegment.angle"? Just trying to confirm what you are referring to.

So what I'm doing with these numbers is just having the frontX position of a segment follow the backX position of another segment? is that right?

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to How to attach segments together? 2013-01-04 18:31:27 Reply

At 1/4/13 06:26 PM, Barzona wrote: Yes, that's quite close to what I'm trying to do. The "angle" variable is just the angle of the current segment? So, "ropeSegment.angle"? Just trying to confirm what you are referring to.

So what I'm doing with these numbers is just having the frontX position of a segment follow the backX position of another segment? is that right?

Yeah, though you might have to add 90, 180, or 270 to the angle just in case something dumb happened, which it often does.

frontX of the current segment = backX of the previous segment, or the mouse's position if it's the first segment.

Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to How to attach segments together? 2013-01-04 19:06:23 Reply

At 1/4/13 06:31 PM, MSGhero wrote: Yeah, though you might have to add 90, 180, or 270 to the angle just in case something dumb happened, which it often does.

frontX of the current segment = backX of the previous segment, or the mouse's position if it's the first segment.

It's kinda confusing me. I'm tracing (frontX, frontY, backX, backY); the way I understood how to find them and it's only producing varying results from the one segment that's following the mouse.

"-14.064694342426407, -0.226310935164772, 14.064694342426407, 0.226310935164772" from the mouse segment.
"14, 0, -14, 0" from the rest of the segments.

The thing that confuses me is even how to bring the two points together. I don't know to even refer to two different values as two different points with their own x and y values. I mean, I tried referring to it as "rope.frontX.x", but that doesn't work. I think I'm in entirely new territory here.

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to How to attach segments together? 2013-01-04 19:22:51 Reply

At 1/4/13 07:06 PM, Barzona wrote: The thing that confuses me is even how to bring the two points together. I don't know to even refer to two different values as two different points with their own x and y values. I mean, I tried referring to it as "rope.frontX.x", but that doesn't work. I think I'm in entirely new territory here.

I believe that what are you trying to do is equalling both pieces of the same cake. If I'm getting your case right, you have to create a double loop inside your function. For example, let's say you have the running i loop, now inside this loop, create a j loop and make that if is equals to j continue and you will be able to get two different pieces of the same cake. Example:

for(var i:int = 0; i < rope.length; i++)
{
          for(var j:int = 0; j < rope.length; j++)
          {
                        if(i == j) continue;

                        trace(rope[i]);
                        trace(rope[j]);
          }
}

Of course it is just a simple example but you can get the idea. Hope that helps.

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to How to attach segments together? 2013-01-04 19:27:41 Reply

At 1/4/13 07:06 PM, Barzona wrote: The thing that confuses me is even how to bring the two points together. I don't know to even refer to two different values as two different points with their own x and y values. I mean, I tried referring to it as "rope.frontX.x", but that doesn't work. I think I'm in entirely new territory here.

You would do the inverse of what 4 said. Instead of searching for frontX, you have that value now. You would have to determine the angle somehow, that's up to you (maybe backX = old frontX and calculate the new angle or something). You have frontX, width, and angle, you're looking for object.x.

Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to How to attach segments together? 2013-01-04 20:24:04 Reply

What is messing me up is referring to the actual front and back positions that I am getting with this code:

var frontX:Number = rope.x + Math.cos(ropeAngle) * rope.width;
var frontY:Number = rope.y + Math.sin(ropeAngle) * rope.height;
			
var backX:Number = rope.x - Math.cos(ropeAngle) * rope.width;
var backY:Number = rope.y - Math.sin(ropeAngle) * rope.height;

He said to find the back, I would just subtract instead of add.

To refer to the front of rope[1] and the back of rope[2], what if I used this:

rope[1].x + rope[1].frontX = rope[2].x -rope[2]. backX;

That way, I'd be using the value of frontX and backX to find an offset of the center x-axis. That offset would be the point that would align with the other end. Assuming I'm finding the front/back/X/Y values correctly, this next step would really just be to line them up. is that the way to do it?

How to attach segments together?