More math help for the math impaired plz...

Post » Tue Aug 31, 2010 2:57 pm

I was all proud of myself, thinking I wouldn't have to post on the forum for help with this.

I managed to make code to take the player's x and y coordinates, and then collect them again later after the player had moved some unprescribed distance, and determine the distance travelled (assuming a straight line). That code looks like this. (GECK's function for taking any number to the power of anything doesn't work I read, so there's a funky looking square-ing function in there).

;leet maths			set tempfloat to ( ((FinishX - StartX) * (FinishX - StartX )) + ((FinishY - StartY) * (FinishY - StartY)) )			set TravelDist to sqrt tempfloat			showmessage defaultdebugmsg TravelDist;---


That code works perfect, and I was all happy and stuff.

And then I came to realize that I need to know something more.

I will have another distance value (picked by another means) which is shorter than my result here (TravelDist). I need to create a set of X and Y coordinates that exists at this distance, and along the same straight line that theoretically exists between the player's StartX/StartY and FinishX/FinishY.

umm anyone know how to do that?
User avatar
Harry-James Payne
 
Posts: 3464
Joined: Wed May 09, 2007 6:58 am

Post » Tue Aug 31, 2010 11:28 am

Hmmm... Well you could try doing it with a proportion, perhaps. So it would be like, Distance traveled/(FinX-StartX) == Distance you want/XDis. Then you say (Dist you want squared) - (XDis squared) == (YDis squared) Then add the Xdis and Ydis to startX and startY, respectively of course. Now let me try to put this into script form. :P

Set XDistance to ((FinishX-StartX)(DistanceTravelledYouDesire)/TravelDist)Set YDistance to sqrt ((DistanceTravelledYouDesire)*(DistanceTravelledYouDesire) - (XDistance)*(XDistance))MyRef.SetPosX (StartX + XDistance)MyRef.SetPosY (StartY + YDistance)


I think this would move 'MyRef' to the point you want, along the line between the points (StartX/Y,FinishX/Y) whatever distance you want. Your effectively making a traingle, hypotenuse being the distance they travel, then scaling it down to make it into one where the hypotenuse is the distance you want them to travel instead. You can adapt this script to set variables to the X and Y of this new point for whatever purpose you need this for. I hope my math is right, and that this helps. :D
User avatar
bimsy
 
Posts: 3541
Joined: Wed Oct 11, 2006 3:04 pm

Post » Tue Aug 31, 2010 5:07 am

Thanks, I shall give this a try!
User avatar
Emma Pennington
 
Posts: 3346
Joined: Tue Oct 17, 2006 8:41 am

Post » Tue Aug 31, 2010 7:48 am

I have this working now, yay!

The long story on that is - I had a version of your code with the sqrt calculation in it, and seemed to give almost the correct results for X (but not quite) and Y was kinda out of this world. I put in a detailed debug message to really look at what all the numbers were, and as soon as I saw it, I realized just what you had said about there being a way to do this based upon percentages of change. Upon seeing that I had to try it, and it works alright.

I feel certain there is a way to do this which gives you the 'real' x and y instead of sort-of doing it like a percentage. I sort-of see the "distance" as being a circle around the player's location, and since I have the X and Y of a point outside, I know there is a formula which will tell us the X and Y intercept between the player and that point outside as it lies on that circle.

But anyway, this worked well enough - - I figure I'll roll with it for now - -

;leet maths			set tempfloat to ( ((FinishX - StartX) * (FinishX - StartX )) + ((FinishY - StartY) * (FinishY - StartY)) )			set TravelDist to sqrt tempfloat			set OldFuelStatus to PlayerHummerREF.fuelstatus			set PlayerHummerREF.fuelstatus to PlayerHummerREF.fuelstatus - TravelDist			if PlayerHummerREF.fuelstatus <= 0; WE NEED TO IN FINAL INTERRUPT TRAVEL AND DIVERT TO APPROPRIATE DISTANCE-RELATED DESTINATION				set OtherDist to TravelDist - OldFuelStatus				set TravPercent to OldFuelStatus / TravelDist				set XRedir to (FinishX - StartX) * TravPercent				set YRedir to (FinishY - StartY) * TravPercent				set XRedir to XRedir + StartX				set YRedir to YRedir + StartY							HummerDebugMapREF.enable				HummerDebugMapREF.setpos X XRedir				HummerDebugMapREF.setpos Y YRedir				showmessage HummerDebugMsg StartX StartY FinishX FinishY TravelDist OldFuelStatus XRedir YRedir				set PlayerHummerREF.fuelstatus to 0

User avatar
Monique Cameron
 
Posts: 3430
Joined: Fri Jun 23, 2006 6:30 am

Post » Tue Aug 31, 2010 6:34 am

I'm glad you got it working! I think there is a formula for what you need, but I cant honestly remember it. :shrug:
User avatar
lisa nuttall
 
Posts: 3277
Joined: Tue Jun 20, 2006 1:33 pm

Post » Tue Aug 31, 2010 1:22 pm

(xp, yp) is the position of the point that the line passes through
(x0, y0) is the center of the circle
r is the radius of the circle (it only makes sense to do it this way if r is a constant, rather than a fraction of the distance between x0, y0 and xp, yp, but reading your post it will be the distance you "determine by other means"
(x, y) is the position of the intersection, relative to the center of the circle (rather than gameworld coordinates)

y = x * (dy/dx) equation for a line passing through both points
x^2 + y^2 = r^2 equation for a circle with radius r

x^2 + x^2 * (dy/dx)^2 = r^2 substitute in for y
x=sqrt(r^2/(1 + (dy/dx)^2)) = r * sqrt(1 / (1 + (dy/dx)^2) solve for x
dy = yp - y0
dx = xp - x0

these last 5 are the equations you'd end up using:
substitute in for dy and dx to get y and x:
dydx = (yp - y0) / (xp - x0)
x = r * sqrt(1 / (1 + dydx^2))
y = x * dydx

add x0 and y0 to x and y to get the gameworld coordinates of the intersection, (xf, yf):
yf = y0 + y
xf = x0 + x


yay math :celebration: Also you know the pow function is broken, so that dydx^2 needs to be dydx * dydx
User avatar
carla
 
Posts: 3345
Joined: Wed Aug 23, 2006 8:36 am

Post » Tue Aug 31, 2010 3:11 pm

ahhhh ... interesting um.... I don't feel like I could write this out in geckscript, but it's tempting to try eh ...
User avatar
NAkeshIa BENNETT
 
Posts: 3519
Joined: Fri Jun 16, 2006 12:23 pm

Post » Tue Aug 31, 2010 7:23 am

I think this would be it. You end up with (x,y) equal to the coordinates of the intersection, with respect to the gameworld coordinate system.

set r to... ; however you're determining that shorter travel distanceset StartX to... ; player's starting Xset StartY to... ; player's starting Yset FinishX to... ; player's finishing Xset FinishY to... ; player's finishing Yset dydx to (FinishY - StartY) / (FinishX - StartX)set x to r * sqrt(1 / (1 + dydx * dydx))set y to x * dydxset x to x + StartXset y to y + StartY

User avatar
Marcia Renton
 
Posts: 3563
Joined: Fri Jan 26, 2007 5:15 am


Return to Fallout 3