|
atan() angles Displaying 1-18 of 18 total.
1
Overkill
|
I'm thinking of trying to make some sort of arcade shooter as a little project.
Now, alright: I want to make the enemy aim at the player no matter where the player is located. You know, like home in on the player.
I would really like to know to do this *without* an atan2() (which isn't implemented yet anyways) function. This is so I can not only use this knowledge for VERGE, but also a particular language that lacks atan2().
So can people tell me how I figure out the angle from the enemy to player?
Posted on 2004-02-27 02:47:11
|
blues_zodiakos
|
If the shots are not instant hit, just have them recalculate each page to become closer to the player. For example:
Each frame:
GetPlayerX();
GetPlayerY();
if(ShotX|GreaterThan|PlayerX)
{
ShotX-=1;
}
else
{
ShotX+=1;
}
if(ShotX|GreaterThan|PlayerY)
{
ShotY-=1;
}
else
{
ShotY+=1;
}
At least, I think that's right. I'm prone to more logic errors than George Bush's AI.
Posted on 2004-02-27 04:41:25
|
rpgking
|
Hmm, but that code right there would ensure a direct hit on the player every time, seeing as how that would create a homing missile of sorts. I doubt you'd want a space shooter to be that difficult :P
Posted on 2004-02-27 06:04:49
|
blues_zodiakos
|
No, by all means no, hehe. What you can do is only run that code once every two frames(or 20, or whatever), and maybe set a limit to how many times it can run for each shot. If you are making a one-way scrolling game, it's real easy - just make it only move x or y. :D
Posted on 2004-02-27 18:53:46
|
Overkill
|
You see, the shots that enemies fire run on angles (in degrees). I want the shots to be dodgeable, so I don't want to have a shot that constantly changes direction.
So I know need to use arctan to find the angle (Because I've already coded an angle movement thing for the engine). I just want to know what I would do to find the correct angle.
I've tried the following methods (seperately), and none of them worked:
angle=atan(enemy.x-player.x/(enemy.y-player.y +1));
angle=atan(enemy.x+player.x/(enemy.y+player.y +1));
angle=atan(player.x-enemy.x/(player.y-enemy.y +1));
angle=atan(player.x+enemy.x/(player.y+enemy.y +1));
angle=atan(enemy.y-player.y/(enemy.x-player.x +1));
angle=atan(enemy.y+player.y/(enemy.x+player.x +1));
angle=atan(player.y+enemy.y/(player.x-enemy.x +1));
angle=atan(player.y+enemy.y/(player.x+enemy.x +1));
Posted on 2004-02-27 20:38:15 (last edited on 2004-02-27 20:45:17)
|
blues_zodiakos
|
I did some searching, and found that the atan() function in C is defined somewhere in math.h, so if you were to look at that, you might be able to find out how to make an atan() function yourself. I think. I don't know all that much about the c standard libraries yet.
Posted on 2004-02-28 03:47:11
|
zonker6666
|
I think that a simpler solution to your problem would be the following ....
bullet.xspeed=target.X-bullet.x / steps;
bullet.yspeed=target.Y-bullet.y / steps;
then bullet.x+=bullet.xspeed;
bullet.y+=bullet.yspeed;
after u update the bullet's location for the right amount of
steps it will reach its target .....
(I used that technique in Battle Space Goddess and it worked
very well)
Hope that helps u out.
Posted on 2004-03-29 02:01:01
|
Zaratustra
|
if (bullet.x>target.x) bullet.xspeed-=0.1;
if (bullet.x<target.x) bullet.xspeed+=0.1;
if (bullet.y>target.y) bullet.yspeed-=0.1;
if (bullet.y<target.y) bullet.yspeed+=0.1;
bullet.x+=bullet.xspeed;
bullet.y+=bullet.yspeed;
Posted on 2004-03-29 05:47:14 (last edited on 2004-03-29 05:50:47)
|
mcgrue
|
no floats in vc yet.
but yeah.
Posted on 2004-03-29 06:17:58
|
Zaratustra
|
MULTIPLY EVERYTHING BY TEN THEN.
jesus.
Posted on 2004-03-29 06:37:11
|
Gayo
|
No, no, bitshifting! It's much faster.
Posted on 2004-03-31 00:33:21
|
anonymous
|
OK, so it's obvious atan() isn't the greatest way to calculate angles. However, if I can use atan(), I can just use a code I already have.
Alright, so does atan() take a 16.16 fixed point integer and output a normal degree integer?
Posted on 2004-04-06 13:17:41
|
Overkill
|
That was me..
Posted on 2004-04-06 20:13:11
|
Overkill
|
I don't want to seem impatient, but it would desirable to have an answer, so that I can begin compoing, otherwise, I'll have to think of another type of game.
Posted on 2004-04-08 03:16:47
|
vecna
|
Yes.
Posted on 2004-04-08 03:26:01
|
rpgking
|
I don't want to seem impatient, but it would desirable to have an answer, so that I can begin compoing, otherwise, I'll have to think of another type of game.
You did see Zara's and Zonker's posts right? They give you 2 different solutions...
Posted on 2004-04-08 05:46:44
|
Omni
|
So...atan() (arctangent) returns the...angle of a given slope, right?
But atan() doesn't return at some radian/angle in the unit circle...I forget, but it's domain sometimes doesn't work. That's why you have to calculate--is the destination coordinate above the source coordinate, to the left, or to the right, etc. Then you affix the correct sign to the result from atan()--which is the angle.
Then take this angle, and plug it into Sin() and Cos() to get the base vector length.
Then multiply these two resulting numbers by the velocity of the homing missile or whatever (which should be one number -- velocity, not vel_x or vel_y). I guess you could add a Randon() in there for a margin of error in the homing missile, or, like was said earlier, just don't process the angle of the homing missile so much.
Basically what you're dealing with is simple vector math. I can't remember everything you need to know verbatim, but that's the basic idea. You can learn in much more detail here:
Sin & Cos: The Programmer's Pals
As well as a lot more interesting information.
Posted on 2004-04-08 18:15:12
|
Overkill
|
Thanks, everyone for the help and suggestions. It turns out my ArcTan2() function that I created as a result now works for the most part. However, I'll have to wait for floats before all angles can be really accurate.
Enjoy (I hope nothing screwed up since I had to format this into html.):
int screenx=ImageWidth(screen);
int screeny=ImageHeight(screen);
void Autoexec()
{
int done;
while(!done)
{
RectFill(0,0,screenx,screeny,rgb(0,0,0),screen);
Printstring(0,0,screen,0,str(ArcTan2(mouse.x,mouse.y,screenx/2,screeny/2) ) );
Rect(mouse.x,mouse.y,mouse.x+8,mouse.y+8,rgb(255,255,255),screen);
Showpage();
}
//OVK_ShooterLoop();
}
int abs(int a)
{
if (a<0) return 0-a;
else return a;
}
//Hooray! I fixed the glitchy trigonometry problem!
// LEFT=0 RIGHT=180 DOWN=90 UP = 270
int ArcTan2(int x1, int y1, int x2, int y2)
{
int x, y;
int r, a;
x=x2-x1;
y=y2-y1;
if (x)
{
r=y/x;
}
else
{
r=y*100; // Same as dividing by 0.01
}
a = atan(abs(r)<<16);
if(x<0 && y>0)
{
a=180-a;
}
if(x<0 && y<0)
{
a=180+a;
}
if(x>0 && y<0)
{
a=0-a;
}
//This part doesn't mean anything, really...
if(x >0 && y >0)
{
a=0+a;
}
if (a<0) a=360+a;
return a;
}
Posted on 2004-04-09 16:02:37 (last edited on 2004-04-09 16:09:12)
|
Displaying 1-18 of 18 total.
1
|
|