help with mathy problem
Displaying 1-5 of 5 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
RageCage

okay, I'm trying to make some chase AI that acts something like a homing missle.

Heres the core code:


int x2=mouse.x*10;
int y2=mouse.y*10;

int a=x2-missle.x;
int b=y2-missle.y;
int c=sqrt(pow(a,2)+pow(b,2));
int ang=abs(atan2(b,a));

if(b<0) ang=0-ang;

missleAng-=missleAng-ang/10;

missle.x= 100 *cos(missleAng) /65536 +missle.x;
missle.y= 100 *sin(missleAng) /65536 +missle.y;


which is kept in the main game loop.

the setup should produce a predator following prey:


but, when the prey moves... I want it to do this:


which is the reason for the /10 in the missleAng-=missleAng-ang/10; equation. The idea is that we have the angle of the prey to predator stored in ang and the angle at which the predator is moving is the missleAng.

If they were the same it would be impossible to dodge the predator and it would feel unnatural, so I want the predator to turn its angle into the correct angle, but slowly. (with this in mind, I know that it isnt being controlled by timer yet... I'll get to that later)

this works.

soooooo... the problem with this is....

This is how the verge angle grid is setup as I have it now. Its kinda weird but it works most the time.

What happens when I run the game is that it works just like I want it to until the angle the predator is moving in hits around the 180 degree point because suddenly ang= -170 but missleang is at +170 and in math, -170 is not right next to +170 so it starts going all the way down to 0 and down to -170... but then by this point the desired angle is at +170 and the current angle is -170... so its back teh other way again! over and over untill the predator has run off the screen into oblivion!!!!... ahem...

so that looks like this(taken directly from verge with some of my masterful markups!):


so yeah... I dont know how I'm to fix this... can i get some help?

one idea I had was if when the prey crosses the y-axis, the grid would flip around so that 0 is where the 180 usually is... but such a concept is beyond my comprehension in application right now. >_<

Posted on 2005-09-05 01:20:33 (last edited on 2005-09-05 01:31:19)

Overkill

I was just screwing around and I figured out an effective way to do it. Again... not timer-based, but I expect you'll add timing in afterwards. Requires a min() function. Also, I felt like making missleAng into missle.ang.



struct point_type
{
int x, y, ang;
}

point_type missle;

int min(int a, int b)
{
if (a < b) return a;
return b;
}

void Homing()
{
int x2 = mouse.x;
int y2 = mouse.y;

int a = missle.x - x2;
int b = missle.y - y2;
int c = sqrt(pow(a, 2) + pow(b, 2));
int ang = atan2(b, a);

if (ang > 360 + missle.ang % 360)
{
missle.ang += min(2, ang - missle.ang);
missle.ang = 360 + missle.ang % 360;
}
else if (ang < 360 + missle.ang % 360)
{
missle.ang -= min(2, missle.ang - ang);
missle.ang = 360 + missle.ang % 360;
}

missle.x -= cos(missle.ang) * 2 >> 16;
missle.y -= sin(missle.ang) * 2 >> 16;
}

Posted on 2005-09-05 09:13:49

RageCage

well I put it in and... I'm not real sure what it's doing but its not working =p

Thanks for cleaning up my code though... I switched to the >>16 and .ang... Anyway, I'm trying to see what your doing and see where I can fix it... I'll post more later

Posted on 2005-09-05 11:58:34

Overkill

Heh... and I've just made an even better homing function! ...Er, rather copied the example of dot products in Sin & Cos: The Programmer's Pals. Copy this into a blank system.vc and test!


struct point_type
{
int x, y, angle, speed, turnspeed;
}

point_type missle;

int abs(int a)
{
if (a < 0) return -a;
return a;
}

int min(int a, int b)
{
if (a < b) return a;
return b;
}

int max(int a, int b)
{
if (a > b) return a;
return b;
}

void AutoExec()
{
missle.speed = 2;
missle.turnspeed = 10;
while(1)
{
RectFill(0, 0, ImageWidth(screen), ImageHeight(screen), 0, screen);
Render();
BlitMouse();
DotProductHoming();
Circle(missle.x, missle.y, 3, 3, RGB(255, 255, 255), screen);
Line(missle.x, missle.y, mouse.x, mouse.y, RGB(0, 0, 255), screen);
ShowPage();
}
}

void BlitMouse()
{
Circle(mouse.x, mouse.y, 3, 3, RGB(0, 255, 0), screen);
}

void DotProductHoming()
{
int target_x = mouse.x;
int target_y = mouse.y;
int dx = missle.speed * cos (missle.angle) >> 16;
int dy = missle.speed * sin (missle.angle) >> 16;
missle.x += dx;
missle.y += dy;

// Determine whether we should turn left or right
// using the dot product.
if ((dy * (target_x - missle.x)) + (-dx * (target_y - missle.y)) > 0)
{
missle.angle = 360 + (missle.angle - missle.turnspeed) % 360;
}
else
{
missle.angle = 360 + (missle.angle + missle.turnspeed) % 360;
}
}

Posted on 2005-09-05 12:29:13

RageCage

niiiiiiiice, thanks man

Posted on 2005-09-05 14:24:13


Displaying 1-5 of 5 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.