2
\$\begingroup\$

I have this 2d platformer made out of box colliders. I have the player connected to a point by a LineRenderer and a DistanceJoint2D, for use in physics puzzles. This tool lets him link to a location and then do grappling hook things with it, like retract the rope at speed to spiderman around.

What I want to do is move the distance joint around the object, thus changing the angle at which the player is pulled. Like in this image.

enter image description here

Very similar to this more general, unanswered question that doesn't not give any specific math.

I know I'll be making a list of positions and then move the joint's anchor around whenever it wraps/unwraps, but I'm not sure how to tell when and where it wraps on something.

\$\endgroup\$
0
\$\begingroup\$

When the BoxCollider2D that is the rope, collides with the BoxCollider2D that is the platform, a collision callback will occur, whose argument will be a Collision2D containing the pertinent contact data. You'll know which platform collided with the rope from the Collision2D, and then check that body's 4 corners against the collision point using Vector2.magnitude, to see which is the nearest corner. That's where you'll create the joint. If it is created too close, don't worry, as any inter-penetration will be resolved automatically by the physics engine pushing the rope and its associated (new) joint(s) away from the platform box itself.

You also need to know, as the rope swings back down again, the point in time at which you should remove extraneous joints. This will happen the moment the angle of any sub-joint rope sections angles reach or exceed ("cross over") the angle they had at the moment when they split off. You could just check the subsection's angle against its parent rope section's angle to know for sure.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You appear to be making some assumptions, Right now my rope is just a DistanceJoint2D, I plan to add visuals later. Can you explain why you would give the rope a collision box, and perhaps how you'd find thecorner-rope impact point? \$\endgroup\$ – D'Arcy Hamilton Feb 21 '18 at 1:48
  • \$\begingroup\$ @D'ArcyHamilton Oh, I am making them, because they are what's necessary to solve your problem! (Unless you want to write the collision code yourself, which would be pointless given you have a physics engine at your disposal.) The rope's collision box is required to know when you have contact with the platform's collision box. BoxCollider2D is not a visual, it is a physics entity. I've edited the answer somewhat to clarify. \$\endgroup\$ – Engineer Feb 21 '18 at 3:42
  • \$\begingroup\$ Sure, but you're stilling leaving out steps. \$\endgroup\$ – D'Arcy Hamilton Feb 27 '18 at 2:09
  • \$\begingroup\$ For one, as far as I can tell there isn't a way to ask a collision object where it's vertices are. You can ask it for local scale, and bounds, but that doesn't convert into global positions easily and god help you if it is a polygon. Second, why did you choose BoxCollider for the rope over an EdgeCollider or raycasting? I'd give you 'Answered' if you could tell me how to get the Vector2 of where the new bend is. (even just what the line of code to reference a PolygonCollider2D's list of corners is!) \$\endgroup\$ – D'Arcy Hamilton Feb 27 '18 at 2:16
0
\$\begingroup\$

Right, so.

How I ended up doing this was by switching all my platform/wrapping around objects from BoxCollider2Ds to PolygonCollider2Ds. Boxes can't be asked for their corners as they are defined as a center point with height and width, but poly colliders will return a list of Vector2 if you use polycollider.points.

Then, in Update I just sent a raycast from my character to the last point, and asked the object hit for the .points, and compared them to the raycast collision coordinates, and added the closest fit to the list of points.

and to unwrap you calculate the angle of the last three points in the linerender, if it is bigger than 180, snip that last point from the list!

TL;DR

  • Only use polycollider2D for your platforms
  • On update send raycast along rope
  • On collision use .points to get list of corners
  • compare corners to raycast collision point
  • add closest corner to list/linerender/joint/whatever else you need

I was greatly aided by the tutorial found here: www.raywenderlich.com/171250/make-2d-grappling-hook-game-unity-part-1

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.