Page 1 of 1

Trajectory Problem

PostPosted: Tue Dec 22, 2009 11:52 pm
by SpiritofRadio
In my game there are enemy tanks that try to shoot at you. When they shoot, i made it so that the bullets follow the angle that the turrets animation is in. But with the gravity on the bullets, they completely miss the target if it is far away. I have already tried several formulas to get the correct angle the bullet has to leave the turret at to hit the target, but none of them work. Can someone please help me?

Re: Trajectory Problem

PostPosted: Wed Dec 23, 2009 2:30 am
by skydereign
Just making sure, do you want the tanks to be completely accurate, or perhaps calculate just to get a certain range, the x variable? Also, can they adjust the velocity that the shot is fired at, or is it set? If so can you tell me the set directional_velocity, and also what you are using as gravity? Anything else you can provide might come in handy, but I think that is all that I would need to make a decent/relevant demo of it for you.

Re: Trajectory Problem

PostPosted: Wed Dec 23, 2009 2:48 am
by SpiritofRadio
Im just looking for the turret to aim at the correct position, I dont want it to be %100 percent accurate. The velocity can be any number it doesnt really matter, but it should be low enough that the shots are easily visable. For the gravity on the bullets i just put in the draw actor:

yvelocity+=0.5;

And in further information, the turret only has 16 animations.

Re: Trajectory Problem

PostPosted: Wed Dec 23, 2009 3:55 am
by skydereign
Okay, two questions, they should be the last ones. First, will the tanks always be on the same level as the player, or will they need to adjust for height also? Right now I haven't implemented that adjustment, but now that I think about it, you probably want that included. The second is a little more trivial, the 16 animations are just for a 90 degree span right? If the angle would go beyond the 90 degree mark, the tank would just turn around, or does the turret extend to fit 180 degrees?

Re: Trajectory Problem

PostPosted: Wed Dec 23, 2009 4:04 am
by SpiritofRadio
No, the tanks can be anywhere, not always on the same y axis. Dont worry about the animations, all i need is the angle that the bullets will leave at. If you can do that that would help me out alot.

Re: Trajectory Problem

PostPosted: Thu Dec 24, 2009 11:25 am
by skydereign
Took me long enough... You can fuse the equations back together if you want, it would use less memory that way. I broke them up to make sure I had the equation correct. To explain the demo, you press the tank2 actor, which should be the left pacman icon, and it will fire a ball at the other pacman, named player. The number on the screen is the angle required to fire, and there are two variables in global code, gravity and v0. Both are given values, though you might make them constants instead of variables. Anyways, anything doesn't work, please tell me.

Re: Trajectory Problem

PostPosted: Thu Dec 24, 2009 7:16 pm
by SpiritofRadio
Your demo works very well, but there are two problems. If the shooting tanks x and y do not equal to zero, the shots are off by alot. The second problem is that when I input it into my formula, and click ok, its says : Unexpected declaration.. for every one of the letters. Im not really sure why but I just got the newest version of game Editor and the game im working on I started in an older version.

Re: Trajectory Problem

PostPosted: Thu Dec 24, 2009 7:53 pm
by krenisis
Yes I know what you mean. That used to happen to me before. Check his commands , whatever is undeclared is problay a variable. Declare that variable and then the demo will work. If Iam unclear tell me and I will clarify

Re: Trajectory Problem

PostPosted: Thu Dec 24, 2009 10:00 pm
by skydereign
Sorry the mess up when not at 0,0 was a mess up on my part. Forgot to test it, and turns out on the setting of Y it has player.y-x... Just use this code, it should work. I made it so it can shoot in all directions, though you might want to keep the 90 degrees. As for your other problem... hopefully this fixes it.

Code: Select all
double root;

root = pow(v0,4.0) - (gravity*(gravity*pow((player.x-x),2.0)+(2.0*(-player.y+y)*pow(v0,2.0))));
if((root)<0)
{
    r=0;
    root=0;
}
else
{
    r=255;
    root = sqrt(root);
}

angle = radtodeg(atan(((pow(v0,2.0)) - root)/((gravity*(player.x-x)))));
if(player.x<x)
{
    angle+=180;
}

textdisp.textNumber=angle;
.

Oh, if you didn't know already you can use rand to make it a less accurate. For the most part the root that I used is very accurate, so you would have to give it a wide range of possible error, but that is partially because of the huge target. This is what I used, and it seemed okay.
Code: Select all
angle = radtodeg(atan(((pow(v0,2.0)) - root)/((gravity*(player.x-x))))) + rand(30) - rand(30);

Re: Trajectory Problem

PostPosted: Thu Dec 24, 2009 11:12 pm
by SpiritofRadio
K man I am so confused now. Your demo works perfectly. 100% perfect. I input the exact same thing into mine, changed player to Character (Name of the actor you control), and set gravity to equal 0.5 and v0 to equal 15. When the game started it said there was an illegal division by zero so i added 0.0000017 or something like that to the part that started that. It didnt divide byt zero, but the turret would only shoot either 0 degrees or 180 degrees. What have I done wrong???
Here is what I put:

Code: Select all
double root;

root = pow(v0,4.0) - (gravity*(gravity*pow((Character.x-x),2.0)+
(2.0*(-Character.y+y)*pow(v0,2.0))));
if((root)<0)
{
    r=0;
    root=0;
}
else
{
    r=255;
    root = sqrt(root);
}

tangle = radtodeg(atan(((pow(v0,2.0)) - root)/((gravity+0.000000000017*(Character.x-x)))));
if(Character.x<x)
{
    angle+=180;
}

animpos= angle/22.5+0.3;

Re: Trajectory Problem

PostPosted: Fri Dec 25, 2009 12:57 am
by skydereign
Well, here is an actual ged of it, it also now is protected against divisions by zero. I changed player to fit your actor's name, Character. Copying and pasting the codes from the ged should work. If you are still having trouble, can you send me the ged of it not working and I can try to fix it?

Re: Trajectory Problem

PostPosted: Fri Dec 25, 2009 9:07 am
by SpiritofRadio
It works %100 percent perfect now! All i had to do to change it was change the x and y to xscreen and yscreen. Dude thank you for all your help. The world needs more people like you man, people who help people they dont know and for no personal gain. Your %100 legit.