•      Sign In
• Create Account

### #ActualKhatharr

Posted 27 December 2012 - 02:26 AM

I didn't understand what you did the second time there (I need to copy it into SciTE and look at it more closely), but this is what I came up with (just because I like messing with this kind of problem).

struct Vector2 {
int x;
int y;
};

Vector2 findTargetAlignedBoundaryIntersection(Vector2 obsever, Vector2 target) {
Vector2 targetDelta = { //vector to the target from the obserer
target.x - observer.x,
target.y - observer.y
};

//position of the relevant boundaries (will also be abused as a retval later)
//start by assuming max bounds (lower right)...
Vector2 boundary = {
X_MAXIMUM_BOUND,
Y_MAXIMUM_BOUND
};
//and then correct by axis when wrong
if(targetDelta.x < 0) {boundary.x = X_MINIMUM_BOUND;}
if(targetDelta.y < 0) {boundary.y = Y_MINIMUM_BOUND;}

//vector from observer to relevant map corner
Vector2 boundaryDelta = {
boundary.x - observer.x,
boundary.y - observer.y
};

//find ratio of boundary vs target deltas per-axis
//(Edit - corrected against div-by-zero)
//(Note - behavior is undefined if observer and target are equal)
float xRatio = std::numeric_limits<float>::max();
if(targetDelta.x) {xRatio = (float)(boundaryDelta.x) / targetDelta.x;}
float yRatio = std::numeric_limits<float>::max();
if(targetDelta.y) {yRatio = (float)(boundaryDelta.y) / targetDelta.y;}

if(xRatio > yRatio) { //slope collides with y bound first
boundary.x = observer.x + (targetDelta.x * yRatio);
}
else { //slope collides with x bound first
boundary.y = observer.y + (targetDelta.y * xRatio);
}

return boundary;
}


Edit - Ah, I see. Same solution. Cool.

@P0jahn - He's just using 30.0f and 20.0f as map bounds there. You'd have to fill in the actual bounds where he uses the constants.

### #5Khatharr

Posted 26 December 2012 - 04:45 PM

I didn't understand what you did the second time there (I need to copy it into SciTE and look at it more closely), but this is what I came up with (just because I like messing with this kind of problem).

struct Vector2 {
int x;
int y;
};

Vector2 findTargetAlignedBoundaryIntersection(Vector2 obsever, Vector2 target) {
Vector2 targetDelta = { //vector to the target from the obserer
target.x - observer.x,
target.y - observer.y
};

//position of the relevant boundaries (will also be abused as a retval later)
//start by assuming max bounds (lower right)...
Vector2 boundary = {
X_MAXIMUM_BOUND,
Y_MAXIMUM_BOUND
};
//and then correct by axis when wrong
if(targetDelta.x < 0) {boundary.x = X_MINIMUM_BOUND;}
if(targetDelta.y < 0) {boundary.y = Y_MINIMUM_BOUND;}

//vector from observer to relevant map corner
Vector2 boundaryDelta = {
boundary.x - observer.x,
boundary.y - observer.y
};

//find ratio of boundary vs target deltas per-axis
float xRatio = (float)(boundaryDelta.x) / targetDelta.x;
float yRatio = (float)(boundaryDelta.y) / targetDelta.y;

if(xRatio > yRatio) { //slope collides with y bound first
boundary.x = observer.x + (targetDelta.x * yRatio);
}
else { //slope collides with x bound first
boundary.y = observer.y + (targetDelta.y * xRatio);
}

return boundary;
}


Edit - Ah, I see. Same solution. Cool.

@P0jahn - He's just using 30.0f and 20.0f as map bounds there. You'd have to fill in the actual bounds where he uses the constants.

### #4Khatharr

Posted 26 December 2012 - 04:43 PM

I didn't understand what you did the second time there (I need to copy it into SciTE and look at it more closely), but this is what I came up with (just because I like messing with this kind of problem).

struct Vector2 {
int x;
int y;
};

Vector2 findTargetAlignedBoundaryIntersection(Vector2 obsever, Vector2 target) {
Vector2 targetDelta = { //vector to the target from the obserer
target.x - observer.x,
target.y - observer.y
};

//position of the relevant boundaries (will also be abused as a retval later)
//start by assuming max bounds (lower right)...
Vector2 boundary = {
X_MAXIMUM_BOUND,
Y_MAXIMUM_BOUND
};
//and then correct by axis when wrong
if(targetDelta.x < 0) {boundary.x = X_MINIMUM_BOUND;}
if(targetDelta.y < 0) {boundary.y = Y_MINIMUM_BOUND;}

//vector from observer to relevant map corner
Vector2 boundaryDelta = {
boundary.x - observer.x,
boundary.y - observer.y
};

//find ratio of boundary vs target deltas per-axis
float xRatio = (float)(boundaryDelta.x) / targetDelta.x;
float yRatio = (float)(boundaryDelta.y) / targetDelta.y;

if(xRatio > yRatio) { //slope collides with y bound first
boundary.x = observer.x + (targetDelta.x * yRatio);
}
else { //slope collides with x bound first
boundary.y = observer.y + (targetDelta.y * xRatio);
}

return boundary;
}


Edit - Ah, I see. Same solution. Cool.

### #3Khatharr

Posted 26 December 2012 - 04:33 PM

I didn't understand what you did the second time there (I need to copy it into SciTE and look at it more closely), but this is what I came up with (just because I like messing with this kind of problem).

struct Vector2 {
int x;
int y;
};

Vector2 findTargetAlignedBoundaryIntersection(Vector2 obsever, Vector2 target) {
Vector2 targetDelta = { //vector to the target from the obserer
target.x - observer.x,
target.y - observer.y
};

//position of the relevant boundaries (will also be abused as a retval later)
//start by assuming max bounds (lower right)...
Vector2 boundary = {
X_MAXIMUM_BOUND,
Y_MAXIMUM_BOUND
};
//and then correct by axis when wrong
if(targetDelta.x < 0) {boundary.x = X_MINIMUM_BOUND;}
if(targetDelta.y < 0) {boundary.y = Y_MINIMUM_BOUND;}

//vector from observer to relevant map corner
Vector2 boundaryDelta = {
boundary.x - observer.x,
boundary.y - observer.y
};

//find ratio of boundary vs target deltas per-axis
float xRatio = (float)(boundaryDelta.x) / targetDelta.x;
float yRatio = (float)(boundaryDelta.y) / targetDelta.y;

if(xRatio > yRatio) { //slope collides with y bound first
boundary.x = observer.x + (targetDelta.x * yRatio);
}
else { //slope collides with x bound first
boundary.y = observer.y + (targetDelta.y * xRatio);
}

return boundary;
}


### #2Khatharr

Posted 26 December 2012 - 04:15 PM

I didn't understand what you did the second time there, but this is what I came up with (just because I like messing with this kind of problem).

struct Vector2 {
int x;
int y;
};

Vector2 findTargetAlignedBoundaryIntersection(Vector2 obsever, Vector2 target) {
Vector2 targetDelta = { //vector to the target from the obserer
target.x - observer.x,
target.y - observer.y
};

//position of the relevant boundaries (will also be abused as a retval later)
//start by assuming max bounds (lower right)...
Vector2 boundary = {
X_MAXIMUM_BOUND,
Y_MAXIMUM_BOUND
};
//and then correct by axis when wrong
if(targetDelta.x < 0) {boundary.x = X_MINIMUM_BOUND;}
if(targetDelta.y < 0) {boundary.y = Y_MINIMUM_BOUND;}

//vector from observer to relevant map corner
Vector2 boundaryDelta = {
boundary.x - observer.x,
boundary.y - observer.y
};

//find ratio of boundary vs target deltas per-axis
float xRatio = (float)(boundaryDelta.x) / targetDelta.x;
float yRatio = (float)(boundaryDelta.y) / targetDelta.y;

if(xRatio > yRatio) { //slope collides with y bound first
boundary.x = observer.x + (targetDelta.x * yRatio);
}
else { //slope collides with x bound first
boundary.y = observer.y + (targetDelta.y * xRatio);
}

return boundary;
}


### #1Khatharr

Posted 26 December 2012 - 04:13 PM

struct Vector2 {
int x;
int y;
};

Vector2 findTargetAlignedBoundaryIntersection(Vector2 obsever, Vector2 target) {
Vector2 targetDelta = { //vector to the target from the obserer
target.x - observer.x,
target.y - observer.y
};

//position of the relevant boundaries (will also be abused as a retval later)
//start by assuming max bounds (lower right)...
Vector2 boundary = {
X_MAXIMUM_BOUND,
Y_MAXIMUM_BOUND
};
//and then correct by axis when wrong
if(targetDelta.x < 0) {boundary.x = X_MINIMUM_BOUND;}
if(targetDelta.y < 0) {boundary.y = Y_MINIMUM_BOUND;}

//vector from observer to relevant map corner
Vector2 boundaryDelta = {
boundary.x - observer.x,
boundary.y - observer.y
};

//find ratio of boundary vs target deltas per-axis
float xRatio = (float)(boundaryDelta.x) / targetDelta.x;
float yRatio = (float)(boundaryDelta.y) / targetDelta.y;

if(xRatio > yRatio) { //slope collides with y bound first
boundary.x = observer.x + (targetDelta.x * yRatio);
}
else { //slope collides with x bound first
boundary.y = observer.y + (targetDelta.y * xRatio);
}

return boundary;
}


PARTNERS