How do I tether two 8Direction characters together?

0 favourites
  • 6 posts
From the Asset Store
This is a Dungeon Master tool & the 1st of 12 Combat Engines from the Building Combat Engines for Browser Games workshop
  • I am currently working on a co-op game using the 8Direction behavior, and I want the two players to be tethered together. I found an old forum post from Construct 2 on this topic, but the image that used to show how to fix the problem is no longer available.

    The gist of what I am trying to do is I have two characters and I have a chain of lightning linked between the,. I want that chain to stretch up to 300 pixels after which the characters should not be allowed to move any further from each other.

    I know I can detect how many pixels away the characters are from each other by using distance(P1.X, P1.Y, P2.X, P2.Y) but I cant think of how I would make it so that the player can only move in a direction that would make the characters closer together.

    If anyone can help me that would be highly appreciated.

  • You could move them toward each other if the distance is greater than 300.

    Roughly this. Distance-300 would be the extra distance beyond 300, and you’d divide it be two so each object goes half that.

    Var dist=0
    Every tick 
    — set dist to distance(a.x,a.y,b.x,b.y)
    
    Dist>300
    — a: move (dist-300)/2 pixels at angle angle(a.x,a.y,b.x,b.y)
    — b: move (dist-300)/2 pixels at angle angle(b.x,b.y,a.x,a.y)
  • You could move them toward each other if the distance is greater than 300.

    That was my first idea too. But it won't work very well if there are obstacles. It's better to move objects by changing velocity values, then the 8direction behavior will handle things like collisions and sliding properly.

  • Yeah I was thinking collisions could be an issue but it should mostly work. But yeah the velocity bit is probably problematic. You could also cancel the part of the velocity of the objects moving away from each other.

    Here’s one way to do that. You’d do this in addition to correcting the position in my other post. Relv is the velocity of the objects moving toward or away from each other. Then you’d subtract that from the velocity’s so it just keeps the rest of the velocity.

    Ang=angle(a.x,a.y,b.x,b.y)

    Relv= (a.vx-b.vx)*cos(ang)+(a.vy-b.vy)*sin(ang)

    A: set vx to a.vx-relv*cos(ang)

    A: set vy to a.vy-relv*sin(ang)

    … same for b

    Overall setting the position is nice in that it provides a hard limit to how far objects can move apart. Adding a velocity correction to that helps.

    Just changing the velocity to limit the positions indirectly is another idea for sure. It’s actually what the physics behavior does to resolve collisions and joints. In practice it can be a bit spongier though. A quick way is to apply an acceleration to the objects toward each other if too far apart which is a bit spongy. Another idea is to just do the velocity correction above. Probably only if the relv>0 or moving apart. However you’ll still need to add a bit more velocity to move the objects back toward each other otherwise they will drift apart

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Here's an example of it. It corrects the position and the velocity. Seems to work alright with collisions if you add them. In C3 at least if you enable wall sliding.

    dropbox.com/scl/fi/sflsjyje1eqwgtxhk9qjb/tetherObjects.capx

  • Wow, then can even pull each other. Great solution as always!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)