ID:151957
 
After looking at some movement systems I began to wonder how developers have mobs move in a straight line towards a point of the map, no matter what angle this is at. The only method I could think of is to use slopes (I won't take trig for a few years). Are there any other, better methods?
Yea, there's trig. Namely sine and cosine.
The trigonometry that you would need is pretty simple and there are plenty of things on the internet that teach you trigonometry.

George Gough

[Note]
Why does Firefox say that "internet" is spelled wrong? It is spelled correctly.
In response to KodeNerd
I'm actually trying to learn some simple trig. I quit at the segment that it started explaining radians; I need to read it another time or two!
In response to KodeNerd
KodeNerd wrote:
[Note]
Why does Firefox say that "internet" is spelled wrong? It is spelled correctly.

Only you mate :D only you.
xstep = sin theta
ystep = cos theta

Where theta is the angle from the y-axis, positive clockwise.
In response to KodeNerd
KodeNerd wrote:
Why does Firefox say that "internet" is spelled wrong? It is spelled correctly.

Off topic, but 'Internet' should be capitalized as there's only the Internet not many internets.
In response to Jp
And the angle can be found with Pythagorean theorem, correct?
In response to Jeff8500
Jeff8500 wrote:
And the angle can be found with Pythagorean theorem, correct?

No. The Pythagorean theorem finds the sides of a right triangle. You have to use inverse trig functions.
In response to Jeff8500
The easiest way to find an angle is to use the "arc tangent" -- this yields the length of an arc when given a specific fraction, which when taken out of a circle of a certain radius equates directly to a specific number of degrees. It's hard to explain the fundamental of arc tangents and such without getting into some really hardcore math, but suffice it to say that if you plug in the x offset for the "adj" and the y offset for the "opposite", then you can use the arc tangent of adjacent divided by opposite to get the angle.

angle = arctan(x/y)

Or as Lummox JR already does for you, the Arc Tangent snippet.

If it's okay with you, I'd like to plug hub://Jtgibson/jt_vectors which has a lot of trigonometric stuff built in for you.

Just remember that mathematically, angles start facing "right" and then increase as they rotate "counter-clockwise". In other words, 0 degrees is due east, 90 degrees is due north, 180 degrees is due west, and 270 degrees is due south.
In response to Jtgibson
Jtgibson wrote:
Just remember that mathematically, angles start facing "right" and then increase as they rotate "counter-clockwise". In other words, 0 degrees is due east, 90 degrees is due north, 180 degrees is due west, and 270 degrees is due south.

Although this complicates the discussion, I should point out that in navigation, 0° is due north and the angles go clockwise up from there, so 90° is east. Usually in game design I use this convention instead, because there's not really any compelling reason to use the purely mathematical convention. I'd rather use a more physical way of mapping "physical" locations.

That said, a newcomer to trig is likely to experience the same growing pains on this either way.

Lummox JR
In response to Jeff8500
I think this would be a good link: http://www-math.mit.edu/~djk/calculus_beginners/chapter07/ complement01.html

May not work for you but worked for my cousin.

George Gough
In response to Lummox JR
It's easy to transform a formula for one system into the other. You just switch all the sines with cosines, and vice-versa.
In response to Lummox JR
The funniest part is I wound up scrubbing what was going to be three large paragraphs on how to convert angles in standard position into bearings.

Short answer, of course: bearing = normalise(90-angle), where normalise adds or subtracts 360 until the result is in the range [0-360).

All of the math is generally defined from standard position, so that's how I do it -- it'd be possible to convert it to a bearing directly within the formula, which I suppose is more intuitive... I'm leaning towards leaving it as-is, however.
In response to Jtgibson
Jtgibson wrote:
Short answer, of course: bearing = normalise(90-angle), where normalise adds or subtracts 360 until the result is in the range [0-360).

abs(mod(90-angle,360)) would do the trick I think. Translate into DM as necessary.
In response to Fartmonger
Fartmonger wrote:
Jtgibson wrote:
Short answer, of course: bearing = normalise(90-angle), where normalise adds or subtracts 360 until the result is in the range [0-360).

abs(mod(90-angle,360)) would do the trick I think. Translate into DM as necessary.

abs() is incorrect there, but just 90°-angle is enough for calculation purposes.

Or, as was said elsewhere in this thread, just swap all the sines and cosines, since that has the same effect. By definition sin(90°-A)=cos(A) and cos(90°-A)=sin(A).

Lummox JR
In response to Jtgibson
Jtgibson wrote:
Short answer, of course: bearing = normalise(90-angle), where normalise adds or subtracts 360 until the result is in the range [0-360).

proc/normalise(var/angle)
if(angle < 0) angle = 360 % angle
return angle % 360
In response to CaptFalcon33035
CaptFalcon33035 wrote:
Jtgibson wrote:
Short answer, of course: bearing = normalise(90-angle), where normalise adds or subtracts 360 until the result is in the range [0-360).

> proc/normalise(var/angle)
> if(angle < 0) angle = 360 % angle
> return angle % 360
>


360 % angle will return an incorrect result. You can't reverse the two operands to get the result you want.

Lummox JR
In response to CaptFalcon33035
If I am correct and the modulus operator works as I think it does, then this would be the corrected version of the proc:
proc/normalise(var/angle)
return abs(angle % 360)

And lummox is right, you can't reverse the modulus operands like you do for addition (it's just like doing so for division). For example, let's say we have an angle measure of -30 degrees, the operation you specified would give:
360/-30=-12 (Because it divides evenly, the modulus returns 0 which is incorrect.)


I finally realize why number theory is useful in computer science (before I was like "How is that going to help me?").
In response to Kakashi24142
Even that doesn't work, however. For instance, if given the angle -30, abs(-30 % 360) is not 330 degrees.
Page: 1 2