How can I restrict the movement of two players within a certain radius from each other?

0 favourites
  • 11 posts
From the Asset Store
Basic Rounded Vector Geometry Player Design with Glow for 3 player games
  • Hello! The best way to explain what im trying to do is. Imagine two sprites "Pined by Rope style" to each other... so both of them are limited by distance and can slide at the radius after that range limit reached. They can turn back to each other with no speed limitation

    In the examples below you can see Version 1 and Version 2, and they work almost perfect. The problem with them is that one of the players is drugging the second one, and I don't want that. In version 7, it is almost what I want to achieve...

    https://www.dropbox.com/scl/fi/fn83oweqyru8fukdsvgxd/My-Movement.c3p?rlkey=86utyf9ng2ppjfnj9tb0bgbkv&st=0kx1gkun&dl=0

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi.

    I've already had someone ask me about such a rope between players, I've put together a minimal example, maybe someone else will find it useful.

    File c3p

    P.S..

    I've changed the source, the version that came up with user R0J0hound is more cool.

  • The no dragging part seems like an interesting twist. You can limit the position of one player or the other with:

    Dist = distance(p1.x,p1.y,p2.x,p2.y)
    Ang = angle(p1.x,p1.y,p2.x,p2.y)
    P1: move max(0, dist-radius) at angle ang
    Or
    P2: move -max(0, dist-radius) at angle ang

    Which works well if only p1 is moving or p2 is moving. If both are moving then both should only move apart by a percentage. I’d think using the speed at which each is moving away from each other should give a good percentage.

    Dist = distance(p1.x,p1.y,p2.x,p2.y)
    Ang = angle(p1.x,p1.y,p2.x,p2.y)
    W1 = max(0,p1.velocityx*cos(ang+180)+p1.velocityY*sin(ang+180))
    W2 = max(0,p2.velocityx*cos(ang)+p2.velocityY*sin(ang))
    P1: move max(0, dist-radius)*w1/(w1+w2) at angle ang
    P2: move -max(0, dist-radius)*w2/(w1+w2) at angle ang

    Id have to test it though. Also it doesn’t cancel the velocity going away from the radius. I’m pretty sure that could be done with:

    p1 set velocityx to velocityx-w1*cos(ang+180)
    p1 set velocityy to velocityy-w1*sin(ang+180)
    p2 set velocityx to velocityx-w2*cos(ang)
    p2 set velocityy to velocityy-w2*sin(ang)

    But again I’ve yet to test it.

  • igortyhon I appreciate you trying to assist me, but there is a problem with that "code".

    Example #1: if player 1 is moving to the left and at that time player 2 start moving to the right, than player 2 will drug player 1 to the right

    Example #2: if player 2 moving to the right and player 1 starts moving to the left than they both are moving to the right

  • R0J0hound I also appreciate you trying to help me. Before I even try to add it to the construct and test it, I need some time to understand what's happening with those formulas.. I wish there would be the way to turn off drugging for Pin behavior...

  • I ended up implementing it and simplifying the math a bit.

    No dragging:

    dropbox.com/scl/fi/3dpr622etyygcxk4cuom7/tetherObjects_noDragging.capx

    Older example with dragging:

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

    As to understanding the formulas, maybe this will help a bit. The w1,w2 formulas are basically the calculated speed along an angle.

    speedAlongAngle = velocityX*cos(angle)+velocityY*sin(angle)

    It's positive if moving along the angle, and negative if moving the other way. We only want the speed moving away so we clamp it with -min or max so we only have positive speeds.

    Then we calculate the distance for the two objects to move back toward each other with (d-radius). That needs to be split between the two objects, and I'm using w1/(w1+w2) and w2/(w1+w2). They add together and give one so it's a complete split. It's mostly so if only one object is moving only it will be moved and vice versa.

    The last part is to cancel out any velocity trying to move the objects further apart. We already have the speeds moving apart w1,w2 and we just need to convert the angle, speed to xy components with: x=speed*cos(a), y=speed*sin(a).

    The negatives are just to invert the direction. Anyways, maybe some of that explination helps. It's a combination of a lot of simple things.

  • Example #1

    I made this in construct 3 and if P1 line is active he is drugging, of P2 he is active and if both lines, than then both are drugging. (interesting)

    Dist = distance(p1.x,p1.y,p2.x,p2.y)

    Ang = angle(p1.x,p1.y,p2.x,p2.y)

    P1: move max(0, dist-radius) at angle ang

    Or

    P2: move -max(0, dist-radius) at angle ang

    Example #2

    I can't find the way to add "velocityx" to P1 or P2 there is no such expression (or I can't find it?)

    W1 = max(0,p1.velocityx*cos(ang+180)+p1.velocityY*sin(ang+180))

    W2 = max(0,p2.velocityx*cos(ang)+p2.velocityY*sin(ang))

    Example #3 same thing with "velocityx"

    R0J0hound, wow thank you for your time explaining to me how that works. That much detailed information will help me understand how to handle it in future (I hope so)

    I also checked both files:

    #1 tetherObjects.capx works perfectly

    #2 tetherObjects_noDragging.capx have a strange bug: when any of the players are moving at 45° angle (keys A+W, S+D, LeftArrow+UpArrow, or DownArrow+RightArrow sprite starts shaking in some degree to left and to the right

  • I understand what was the problem with (p1.velocityx) it's (p1.8Direction.VectorX), but i still don't understand why sprite is shaking if we are setting angle here (angle(p1.x,p1.y,p2.x,p2.y)) ?

  • I was more focused on getting the math right than the expressions write when I wrote it in the previous post. So velocityX should have been self.8direction.vectorX but the idea is the same.

    We aren't setting the angle with that; we are only measuring it.

    A quick fix for the shaking could be to multiply w1 and w2 by 0.5 or something when correcting the velocity.

    A more correct fix would be to calculate the relative velocity between the two objects in the angle between them and using the weights (w1,w2) in the same way.

    Here's an iteration of that:

    dropbox.com/scl/fi/jrr6hko1kq0xwy77mqxr4/tetherObjects_noDragging2.capx

    The angle can still shake though. That mostly has to do with how the angle is set from the angle of motion. Instead, you probably want the angle to be set to the angle the player is trying to go.

  • Here's an iteration of that:

    https://www.dropbox.com/scl/fi/jrr6hko1kq0xwy77mqxr4/tetherObjects_noDragging2.capx?rlkey=bbk65nj0emtv594scaklirpa2&st=l280easa

    R0J0hound You got a very cool example!!!

    I put exactly your variant in my source, thank you!

  • R0J0hound Thank you very much

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