Author Topic: Scaling a sprite to reach exactly from one point to another  (Read 6438 times)

Loius

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Scaling a sprite to reach exactly from one point to another
« on: September 25, 2013, 02:58:49 PM »
I'm trying to draw a line from one point to another using a UISprite (for various reasons, it needs to be a UISprite and not, say, a Vectrosity line or a LineRenderer).

I've tried to apply my usual method of making an object touch two points:

controlTransform.position = sourcePoint;
controlTransform.LookAt(targetPoint);
sprite.transform.localScale = new Vector3(1f, (targetPoint-sourcePoint).magnitude, 1f);

Now, this would work if the sprite's 1/1/1 scale set it exactly equal to one unity unit, but of course that is not how sprites work.

What I need to do is set the scale's Y value to (target-source).magnitude * sprite.pixelsPerUnit. I determined this through just brute-forcing the scaling to match what I wanted, and it's roughly that value, but I can't hardcode any numbers in because I don't know what will change later.

Unfortunately I am horrendous at math and my searching has not turned up anything that looks promising as far as converting UISprite pixels to a "real" size. Can anyone offer insight or solutions to this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scaling a sprite to reach exactly from one point to another
« Reply #1 on: September 25, 2013, 07:23:38 PM »
LookAt is not going to work, because the default "lookat" is into the screen for sprites, even though their are drawn perpendicular to that vector. All I can say is -- math -- sin/cos. And assuming you're using NGUI 3, you wouldn't be modifying the sprite's local scale. You'd be modifying its width or height -- which is specified in pixels if your UIRoot is set to pixel-perfect. If not, then UIRoot's pixelSizeAdjustment will turn it into pixels (or back) should you have the need.

Loius

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Scaling a sprite to reach exactly from one point to another
« Reply #2 on: September 26, 2013, 02:09:34 AM »
I have the rotation working (using a control object rotated so that when I use its LookAt, the sprite child rotates as desired), but I don't know how many pixels it is from one point to another. Having phrased it that way, oh, it seems that WorldToScreenPoint for source and target will help translate them into pixels.

world points -> screen points, translate to pixel position, find distance in pixels, set sprite's height to that value

Loius

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Scaling a sprite to reach exactly from one point to another
« Reply #3 on: September 26, 2013, 01:17:55 PM »
Well that didn't work, I'm still missing something.

  1. Vector3 target =  heldSprite.transform.position.WithZ(wallSpriteControl.position.z);
  2. wallSpriteControl.LookAt(target);
  3. Vector3 source = wallSprite.transform.position;
  4. target = uiCamera.WorldToScreenPoint(target.WithZ(0));
  5. source = uiCamera.WorldToScreenPoint(source.WithZ(0));
  6. Vector3 dif = (target-source);
  7. wallSprite.height = (int)(dif.magnitude/gridScale);
  8. Debug.Log(target + " -> " + source + " : " + wallSprite.height);
  9.  

In this picture the 'wall sprite' is the long dark line, and the 'held sprite' is the lighter-brown line with the blue thing (my mouse cursor). I'm pointing directly at a corner, but the sprite's height is set to 8/5 of the length it should be (the corners are 32 pixels apart). I could just multiply by 5/8 but I don't know where that value's coming from. I'm using an ortho UICamera.

Zeroing the Z value of source and target doesn't change the results, was just a thing I tried.

The grid can be scaled; dividing the height by gridscale is the correct operation here (the sprite height has the same ratio of incorrectness regardless of scale, plus i'm testing at gridscale == 1.0).


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scaling a sprite to reach exactly from one point to another
« Reply #4 on: September 26, 2013, 03:35:19 PM »
As I mentioned, "LookAt" is not going to work in your case. Likewise, "source" and "target" both should keep their Z. Not sure why you are removing that before passing it to WorldToScreenPoint. It's only the final on-screen values that can ditch the Z component.

Loius

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Scaling a sprite to reach exactly from one point to another
« Reply #5 on: September 26, 2013, 10:21:21 PM »
Well like I said and screencapped, I've got LookAt working. I'm just missing a multiplier on the final length and I'm not sure where I went wrong.

I wound up fixing this by using some of my existing helper locations; I had to use (targetGridPosition - sourceGridPosition + cursorPositionRelativeToTargetGrid).magnitude * tileSizeInPixels

The weird thing is that the held sprite is supposed to be right at (targetGridPosition + cursorPositionRelativeToTargetGrid), at least definitely not some weird ratio of distance away from it.

Not sure why I couldn't get it straight from cursor position; I must have something weird going on somewhere.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scaling a sprite to reach exactly from one point to another
« Reply #6 on: September 27, 2013, 06:26:50 PM »
If your UIRoot is fixed size when you need to take its pixelSizeAdjustment into account.