最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to reduce a position (Vector2) using an direction angle? - Stack Overflow

programmeradmin3浏览0评论

Explaining what I'm trying to do in words is too complex, so I've prepared an explanatory graph.

Based on this, I need to calculate the vector2 corresponding to "reducedTarget" based on a point of origin, a target, the corresponding direction, and an amount to reduce (reductionFactor).

At first, I thought it would be as simple as adding reductionFactor to the normalized direction, but this doesn't work properly since the reduction factor must also vary depending on the direction...

public float reductionFactor = -20;
public Vector2 origin;

void Update()
{
    Vector2 target = GetIntersectionWithCameraBounds(follow);

    Vector2 direction = target - origin;
    Vector2 reducedTarget = target + (new Vector2(reductionFactor, reductionFactor) * direction.normalized);
}

Any ideas on how to solve this?

Explaining what I'm trying to do in words is too complex, so I've prepared an explanatory graph.

Based on this, I need to calculate the vector2 corresponding to "reducedTarget" based on a point of origin, a target, the corresponding direction, and an amount to reduce (reductionFactor).

At first, I thought it would be as simple as adding reductionFactor to the normalized direction, but this doesn't work properly since the reduction factor must also vary depending on the direction...

public float reductionFactor = -20;
public Vector2 origin;

void Update()
{
    Vector2 target = GetIntersectionWithCameraBounds(follow);

    Vector2 direction = target - origin;
    Vector2 reducedTarget = target + (new Vector2(reductionFactor, reductionFactor) * direction.normalized);
}

Any ideas on how to solve this?

Share Improve this question edited Mar 20 at 13:40 coantia asked Mar 20 at 13:11 coantiacoantia 377 bronze badges 10
  • 3 If this is a C# question, please show some C# code. If this is just a math problem, this is probably the wrong site. – gunr2171 Commented Mar 20 at 13:15
  • I've already added it, the code is so simple that's why I didn't add it... – coantia Commented Mar 20 at 13:25
  • Do you miss unity tag? Also see if you are not falling into XY problem, X problem might have a known unity-specific solution. As of now I have no clues of what "reduction factor" would do and why. Looking at sprite from side? Simulating TV effect when looking from a side? – Sinatr Commented Mar 20 at 13:32
  • I simply want to move a UI element away from the edges of my screen, this element is currently placed on the edges of my camera – coantia Commented Mar 20 at 13:44
  • What UI framework? Why not simply scaling the whole Canvas, where UI elements resides? – Sinatr Commented Mar 20 at 14:51
 |  Show 5 more comments

3 Answers 3

Reset to default 0

One way of getting the unknown distance from the intersection to the target is to first get the ratio of the known short distance of side A (reduction factor) compared to the total distance of side A.

Get the ratio of the length A2 compared to length A, where A2 is your reduction factor and A is the distance from point AC to point AB. For the above graphic, the ratio is 0.25 (1/4).

var scaleFactor = distanceA2 / distanceA;

Now that we have this scale factor, you can multiply side C's length by this factor to get the distance of C2. Using the above graphic, distance C2 is 1.376 (5.5.. * 0.25).

var c2Distance = distanceC * scaleFactor;

This might be harder to implement for your specific requirement, but hopefully will help guide you to a serviceable solution.

This comes in 2 steps,

  • firstly you need to deduce how much you want to scale the direction down by, a scale factor (which is unit-less)

  • Then scale the direction down by this factor and add it to the origin

(Using your own terminology for clarity, I think there are better names for these things)

The image shows that the reduced target corresponds in a "reduced direction"

The reduced direction has x and y components which are some % of directions x and y, call this the scale factor.

The picture also shows how the sum of the reduced direction's x and the reduction factor is the direction's x

double reducedDirectionX = abs(direction.x) - reductionFactor
double scaleFactor = reducedDirectionX / abs(direction.x)

Then the reduced direction is the direction scaled by this same scale Factor

Vector2 reducedDirection = scaleFactor * direction.

Finally the reduced target is this added to the origin

Vector2 reducedTarget = origin + reducedDirection 

Y case

There is one unaccounted for possibility, if the scaling needs to be determined by y instead of x

then we need a different scale factor

to cover both cases we use the the most restrictive of both x and y

double reducedDirectionX = abs(direction.x) - reductionFactor
double scaleFactorX = reducedDirectionX / abs(direction.x)
double reducedDirectionY = abs(direction.y) - reductionFactor
double scaleFactorY = reducedDirectionY / abs(direction.y)
double scale factor = min(scaleFactor, scaleFactorY)

Assuming that:

  • Your vector2 are the (x,y) coordinates of some point

  • Your reduction factor is a scaling reduction (e.g. I want to decrease my size by 20%)

  • The origin is at (0,0) and at the center

This becomes a pretty easy math problem. Direction isn't really relevant in that case.

public float reductionFactor = -20;
public Vector2 origin;

void Update()
{
    Vector2 target = GetIntersectionWithCameraBounds(follow);
    // scale the percentage to 0-1 and then look for our target size
    // (e.g. a 20% reduction means we want to be 0.8 of our original)
    float targetRatio = (100.0 - reductionFactor) / 100.0; 

    Vector2 reducedTarget = new Vector2(target.X * targetRatio, target.Y * targetRatio);
}

If you are using Unity or some framework, there may be built in ways to handle that type of transformation.

发布评论

评论列表(0)

  1. 暂无评论