Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411433 Posts in 69363 Topics- by 58418 Members - Latest Member: Pix_RolleR

April 20, 2024, 07:25:07 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Unity2D - Checking intersection between a triangle and a rectangle
Pages: [1]
Print
Author Topic: Unity2D - Checking intersection between a triangle and a rectangle  (Read 1310 times)
Pampattitude
Level 0
**



View Profile WWW
« on: September 01, 2015, 04:44:25 PM »

Hey y'all!

I'm stuck on a Unity 2D problem I can't figure out.

Basically, I'm working on a Worms-like ninja rope for my current project, and I'm stuck.
The algorithm goes as follows (pseudo-code):

Code: (c#)
public List<Vector3> pointList;

void FixedUpdate() {
  /* Create point */
  Vector3 lastPoint = this.getLastPoint();
  RaycastHit2D rch = Physics2D.Raycast(this.player.transform.position, lastPoint - this.player.transform.position, Vector3.Distance(lastPoint, this.player.transform.position) - 0.01f, layerMask);

  if (null != rch.collider) // Point found
    this.addPoint();
  /* !Create point */

  /* Delete point */
  Vector3 preLastPoint = this.getPreLastPoint();
  RaycastHit2D preRch = Physics2D.Raycast(this.player.transform.position, preLastPoint - this.player.transform.position, Vector3.Distance(preLastPoint, this.player.transform.position) - 0.01f, layerMask);
  if (null == preRch.collider) // Point found
    this.removeLastPoint();
  /* !Delete point */
}

Basically, if there's an obstacle between the player position and the last stored point, add a point there. If there is no obstacle between the player and the second-last point, remove the last point (because it's not needed anymore).

The problem arises when a platform is too small but there's no collision with the second-last point. Here is an example:


As you can see, before, the rope is below, but since there's no obstacle between the player and the second-last point, the last point gets deleted.

Now, the last image shows what I'm trying to detect: whether there is an obstacle in the show triangle.
And man, I've tried everything I know. Physics2D.CircleCastAll and Physics2D.BoxCastAll give me nothing because, most of the time, they find inaccurate results (more like all the time, really) and, even if they do, I wouldn't necessarily get points in the platforms closest to be inside the triangle.

I don't want to iterate through all the platforms to check the triangle/platform collision because it will really mess with my game performance (especially since it's a mobile game). But I'm stuck here, I don't know why the circle and box raycasting don't work and I can't find an elegant solution to my issue.

Does anyone have any kind of idea how to detect this?
Thanks!
Logged
Zorg
Level 9
****



View Profile
« Reply #1 on: September 02, 2015, 12:01:58 AM »



Maybe you could release the last rope point based on angles? If the difference between A and B passes zero, remove the last point (you would have to take into account to remove multiple, up to all, points at once or create new points inbetween). But i never wrote a rope physics script!

Joar posted a rope script in the Rainworld thread.
« Last Edit: September 02, 2015, 12:07:34 AM by zorg » Logged
Afinostux
Level 2
**

Forgotten Beats


View Profile
« Reply #2 on: September 02, 2015, 12:08:09 AM »

Instead of angles, you could also work in vectors.


A, which is the right angle to the direction of the second to last point from the last point, and
B, which is the direction between the last point and the player.

Then, if A dot B is greater than one, delete the last point.

If you just do a raycast to the second to last point, you can get into situations where the angle is right for the point to be removed, but the player is still blocked by some other object and it doesn't let go. As an example, picture that the rope is attached where it is in the picture, but that the player somehow ended up under the platform on the left.
Logged

Pampattitude
Level 0
**



View Profile WWW
« Reply #3 on: October 04, 2015, 11:43:04 PM »

Hey!

Sorry for revamping the thread, but I finally got an adapted implementation. It's still not bug-free, but it's good enough.
It's available on Github, for anyone who may need a Worms-like ninja rope implementation!

Here it is.

Hope it helps!

[EDIT]: formatting. Also, the code is licensed under LGPL 3.0, so you may use it in any kind of project but improvements must be shared with the community.
« Last Edit: October 05, 2015, 06:30:47 AM by Pampattitude » Logged
diegzumillo
Level 10
*****


This avatar is so old I still have a some hair


View Profile WWW
« Reply #4 on: October 08, 2015, 11:32:35 AM »

I don't know if you got it working, but I suggest changing your algorithm a bit like this. Start checking for intersection between A and B, once it finds (refine to get closer to the edge) you call this contact point A' and associate with it the angle it made when you got there. Repeat the procedure for A' and B. If the current angle ever becomes smaller than the one associated with A' (the previous contact point) it means the rope is now free from that edge and you repeat the procedure for A and B.

The procedure works for as many contact points as you like, you just need an array-like structure to hold the information. Also take care with sign of the angle;
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic