Search Unity

Finding "points of intersection" of two sprites

Discussion in '2D' started by Vojta Koci, Aug 16, 2014.

  1. Vojta Koci

    Vojta Koci

    Joined:
    Nov 19, 2013
    Posts:
    64
    Hello everyone!
    I have a problem and I'm sort of desperate now. I have two overlapping sprites and I try to find the "line of symmetry" just from their position and rotation, nothing else... Check out the attached picture, if you want.

    example.jpg
    (The pink one is the children of the blue one and it has localScale.x = -1, it is mirrored. The sprites are moving.)

    Basicly, I need to find the point A and B and then count the line's properties:
    a = vecA.y/vecA.x;
    b = vecB.y - a * vecB.x;

    But my way of finding those points does not work. When the sprites move (the pink object has a velocity), it is slighty latened. So, I'd be really really REALLY glad for any tips. If anyone is able to help me, I'll draw his portrait as a superhero, that desperate I am :D
    Thank you very much and have a nice day!
    Vojta Koci
     
  2. Vojta Koci

    Vojta Koci

    Joined:
    Nov 19, 2013
    Posts:
    64
    OK, solved now. The delay in my code was caused by a stupid mistake. I used FixedUpdate instead of Update... Thanks for reading!
     
  3. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    how to find the point of intersect, iam eager to know?
     
  4. Vojta Koci

    Vojta Koci

    Joined:
    Nov 19, 2013
    Posts:
    64
    Well, I solved it this way:

    Vector3 vecA = Vector3.right;
    Vector3 vecB = (sol.position + target.position)/2f;
    vecB = RotatePointAroundPivot (vecB,dif, - sol.eulerAngles.z);
    Matrix4x4 curM;
    if(xScaleSign == 1)
    curM = Matrix4x4.TRS (dif, Quaternion.Slerp(sol.rotation,target.rotation,0.5f)*Quaternion.AngleAxis(90, Vector3.forward), Vector3.one);
    else
    curM = Matrix4x4.TRS (dif, Quaternion.Slerp(sol.rotation,target.rotation,0.5f), Vector3.one).inverse;
    vecA = curM.MultiplyVector(vecA);
    if(xScaleSign == 1)
    vecA = m.MultiplyVector(vecA);
    else
    vecA = m.inverse.MultiplyVector(vecA);

    a = (vecA.y/vecA.x);
    b = ((vecB.y - dif.y + yScale/2) - a * (vecB.x - dif.x + xScale/2))/yScale;

    cola = (vecA.y/vecA.x);
    colb = ((vecB.y - dif.y) - cola * (vecB.x - dif.x))/sol.lossyScale.y;

    Does that make sense to you? vecA is the direction of the vector and vecB is a point the vector goes through...
     
  5. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    k,i will try it.thanks