• Hey Guest. Check out your NeoGAF Wrapped 2025 results here!

Need some math help...With a handy diagram

Status
Not open for further replies.

Ghost

Chili Con Carnage!
Probably a stupid question ahead (im sure i learnt this in school but have since forgotten):

Is there a way to find a point on a line given how far you want to move down it...

example1qp.gif


I need to move the agent towards the target position at say 4 pixels a turn, preferably in a straight line.

I had a ratio sum (using the difference between the current position and the target position to divide up the 4 pixels per axis) which works but the players run in curves not straight lines.


Any help greatly appriciated, mocking also welcome.
 
I'm really confused by your question.

If you want to write the equation of a line which contains (15, -15) and (500, -300), you can do that using the point-slope formula:

(y - y0) = m(x - x0)

where m = the slope of the line (the change in y divided by the change in x) and the fixed point is (x0, y0).

In this case, you get (y + 15) = -(285/485)(x - 15). You can do a little algebra to put that into a more standard-looking form.

What confuses me is your mention of "pixels." Pixels exist only in the x- and y-directions, not in an arbitrary direction. You can't move four "pixels" down a line. You can start at a point and move some unit of distance, but then you need to "round" that ending location to correspond to the nearest pixel for display.
 
Sorry i was getting confused between math and programming when i was writing, that's why theres no minus on the 15's as well.


Thanks for the help, ill try this.
 
Wait, how does this help me find a point on a line given a distance i wish to travel?

Never mind i got it, thanks.
 
Ok thanks, thats actually what i had before but i see what the problem with it is now (cant see how to fix it, but thats another problem)
 
In general, if you start at a point and want to specify your next position by giving a distance and a direction, you want to be working in polar coordinates. (I'll be using the @ symbol for "theta," which is the angle expressed as a clockwise rotation from the polar axis.)

If you're at some point (x1, y1) and want to figure out where you'll end up after going a distance r in the direction @, you can use the Cartesian-polar transformations to help you:

x' = r cos @
y' = r sin @

(x2, y2) = (x1 + x', y1 + y')

The trouble, of course, is figuring out what @ is, but it's the best coordinate system for handling the kind of vectors you want to use.
 
you seem to be having problems with more than just the mathemtical formula... care to elaborate?
 
Its cool, the point-slope algorithm works, but it creates curves over long distances, that wont be a problem for my simulation (hopefully) because the target position will be changing all the time so curves wont really show. It's syntax also changes depending on the coordinates, so i have to have a bunch of if statements to account for the possible different slopes, so looks a mess. Which is why i thought there must be a more straight forward way.

Thinking about how to implement finding the direction of my vectors made my head hurt (It was the first thing i thought of a few days ago) and i have to demo this working monday so ill stick to the messy solution and hope inspiration strikes next week sometime.
 
Status
Not open for further replies.
Top Bottom