Trajectory Problem

You must understand the Game Editor concepts, before post here.

Trajectory Problem

Postby SpiritofRadio » Tue Dec 22, 2009 11:52 pm

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?
SpiritofRadio
 
Posts: 8
Joined: Tue Dec 22, 2009 6:03 am
Score: 0 Give a positive score

Re: Trajectory Problem

Postby skydereign » Wed Dec 23, 2009 2:30 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Trajectory Problem

Postby SpiritofRadio » Wed Dec 23, 2009 2:48 am

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.
SpiritofRadio
 
Posts: 8
Joined: Tue Dec 22, 2009 6:03 am
Score: 0 Give a positive score

Re: Trajectory Problem

Postby skydereign » Wed Dec 23, 2009 3:55 am

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?
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Trajectory Problem

Postby SpiritofRadio » Wed Dec 23, 2009 4:04 am

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.
SpiritofRadio
 
Posts: 8
Joined: Tue Dec 22, 2009 6:03 am
Score: 0 Give a positive score

Re: Trajectory Problem

Postby skydereign » Thu Dec 24, 2009 11:25 am

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.
Attachments
trajectory.zip
(22.82 KiB) Downloaded 122 times
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Trajectory Problem

Postby SpiritofRadio » Thu Dec 24, 2009 7:16 pm

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.
SpiritofRadio
 
Posts: 8
Joined: Tue Dec 22, 2009 6:03 am
Score: 0 Give a positive score

Re: Trajectory Problem

Postby krenisis » Thu Dec 24, 2009 7:53 pm

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
Tutorial Database for all beginners click this link
viewtopic.php?f=4&t=8680
krenisis
 
Posts: 606
Joined: Sat Jul 25, 2009 3:27 pm
Score: 51 Give a positive score

Re: Trajectory Problem

Postby skydereign » Thu Dec 24, 2009 10:00 pm

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);
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Trajectory Problem

Postby SpiritofRadio » Thu Dec 24, 2009 11:12 pm

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;
SpiritofRadio
 
Posts: 8
Joined: Tue Dec 22, 2009 6:03 am
Score: 0 Give a positive score

Re: Trajectory Problem

Postby skydereign » Fri Dec 25, 2009 12:57 am

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?
Attachments
trajectory.zip
(20.76 KiB) Downloaded 121 times
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Trajectory Problem

Postby SpiritofRadio » Fri Dec 25, 2009 9:07 am

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.
SpiritofRadio
 
Posts: 8
Joined: Tue Dec 22, 2009 6:03 am
Score: 0 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest