Slope Equation on a 2D Plane

Started by
2 comments, last by Frozen8950 19 years, 10 months ago
Hey guys I have a question. I just feel dumb today lol because i cant figure this out and i know i have it on the tip of my tongue. Ok.. So i have a 2D Plane from (0,0) top left to (800,600) bottom right. i have a person that can move around on this plane and then I have an AI person that is supposed to move directly towards the other person. Ive been trying to achive this with a slope equation and if the AI is lower right to the regular person it works out ok.. but as soon as i run my guy in cirles around the AI it freaks out and does somthing its not supposed to do. It seems that i have to check and find out which side the person is on relitive to the AI before i do any calcuations? that doesnt seem right.. anyone done this before? anyone know how to do it? Thanks
Advertisement
The direction of the AI''s movement (toward the person) is just a vector subtraction between the Person point and the AI point. But this is only direction. I don''t know how you''re moving the point.
Person<---------AI
Person at A=(x, y), AI at B=(x1, y1)
A-B = (x-x1, y-y1). So if your AI moves from his current point, in the A-B direction, he''s going straight to the person.
"I study differential and integral calculus in my spare time." -- Karl Marx
The simple way is like this, lets say your program tells the direction of the enemy in 4 directions, represented by 1 north, 2 east, 3 south and 4 west.
You take the value of HeroX - EnemyX and see if its bigger than HeroY - EnemyY, if it is bigger, our direction most needed is X, so we figure out which direction we're going, either east or west by seeing if HeroX is less or greater than EnemyX.

Simple.

If I were working in slopes, I'd be moving into degrees, y=mx+b, so you've probably got a problem in your math, but I don't know, it might help if you elaborate.

[edited by - valles on June 1, 2004 12:13:28 AM]
Take note of what quadrant your enemy is in vs. your hero. If you are in the quadrant I and e is in quadrant III then the slope you want will be positive. If you are in quadrant IV and e is in quadrant II, slope will be negetive.

[edited by - PhaseSpace on June 5, 2004 8:31:46 AM]
"Hard Paths Need Hard Ways"- Othium in Frank Herbert''s ''Dune''

This topic is closed to new replies.

Advertisement