Calculating angle
19 posts • Page 1 of 1
Calculating angle
Hello, I've recently tried to work on my old project. It's a mouse operated artillery that calculates the angle the barrel should have to hit a target at the mouse's x-coord. It's not entire correctly and I don't know how to fix this.
- Code: Select all
(e)=>{
app.mousepos(0) > 66.7 ? {
scene.my.calc1 = scene.my.g * (scene.my.d - scene.my.h) * scene.my.vel ^ 2 * app.mousepos(0) ^ 2 + scene.my.vel ^ 4 * (2 * (scene.my.d - scene.my.h) ^ 2 + app.mousepos(0) ^ 2);
scene.my.vel ^ 6 * (-2 * scene.my.g * scene.my.d + 2 * scene.my.g * scene.my.h + scene.my.vel ^ 2) * app.mousepos(0) ^ 4 > scene.my.g ^ 2 * scene.my.vel ^ 4 * app.mousepos(0) ^ 6 ? {scene.my.calc2 = scene.my.vel ^ 6 * (-2 * scene.my.g * scene.my.d + 2 * scene.my.g * scene.my.h + scene.my.vel ^ 2) * app.mousepos(0) ^ 4 - scene.my.g ^ 2 * scene.my.vel ^ 4 * app.mousepos(0) ^ 6} : {scene.my.calc2 = 0};
scene.my.calc2 = scene.my.calc2 ^ (0.5);
scene.my.calc3 = scene.my.vel ^ 4 * ((scene.my.d - scene.my.h) ^ 2 + app.mousepos(0) ^ 2);
app.mousepos(0) >= 1 ? {scene.my.angle = math.acos(((scene.my.calc1 + scene.my.calc2) / scene.my.calc3) ^ 0.5 / 2 ^ 0.5)} : {};
scene.my.angle2 = math.asin(e.normal(1));
scene.my.angle >= (math.pi / 2 - 0.1) ? {scene.my.angle = (math.pi / 2 - 0.1)} : {};
scene.my.raddiff = scene.my.angle - scene.my.angle2
} : {}
}
-

Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Calculating angle
way overcomplicated, remember functions from 10th/11th grade? never thought you'd have a "real world" application for them eh
all you need is take a few shots, mark up angle/bullet distance and produce a y=f(x)
- Mr_Stabby
- Posts: 155
- Joined: Wed Dec 16, 2009 12:16 am
Re: Calculating angle
Could you motivate it a little maybe give me some kind of script to work on. Thank you 
-

Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Calculating angle
Just pick code from my robin hood scene.
Dream of Algodoo as game development engine...
-

Kilinich - [Best bug reporter 2010]
- Posts: 2098
- Joined: Mon Aug 31, 2009 8:27 pm
- Location: South Russia
Re: Calculating angle
But make sure to turn off air friction. Otherwise it will get overcomplicated to calculate the exact angle. I know because I made an (buggy and not very advanced) artillery computer for enabled air friction.

-

KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Calculating angle
Yeas ofcourse I've turned the airfriction off. I just don't know what I've done wrong 
-

Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Calculating angle
If you only want to hit the mouse's x-coordinate this formula is alright:
http://en.wikipedia.org/wiki/Trajectory

And that's it. You don't need anything else for the target angle.
http://en.wikipedia.org/wiki/Trajectory

- Code: Select all
scene.my.angle = 0.5*math.asin(sim.gravitystrength*(app.mousepos(0)-"artillery pos")/"your desired initial bullet velocity"^2)
And that's it. You don't need anything else for the target angle.

-

KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Calculating angle
artilleryPos - is that the point were it turns or were the bullet spawns?
-

Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Calculating angle
Hmm it work in a way but it doesn't hit at the XPos(mouse).
-

Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Calculating angle
at first it hits ahead of the xpos, then the xpos then below the xpos
-

Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Calculating angle
I can't say exactly what's causing this but it sounds like it's a mechanical problem. Because if your bullets hit different spots without changing the mouse's position or any other conditions it's not the code's fault. Do you use e.normal to give the bullets their angle? If yes, it COULD be a problem of your motor that's rotating the barrel. For very long ranges even the smallest wiggle can cause big inaccurateness.

-

KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Calculating angle
no, sorry was a little tired when I wrote that. I do move the mouse but I think It has something to do with the height of the spawned bullet?
-

Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Calculating angle
Yes the height of the bullet spawn point is your zero-level. if you want to hit at a specific height you need a different code. but that's not that hard. i can make one.
maybe i got you wrong before. you want put the mouse to any x-pos and you want the bullet to hit at the height of any target?
maybe i got you wrong before. you want put the mouse to any x-pos and you want the bullet to hit at the height of any target?

-

KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Calculating angle
the would if it would hit/pass the mouse x+y pos but I don't know how to do that.
-

Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Calculating angle
yeah. But my first intetion was to hit the x cord
-

Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Calculating angle
I believe the entire equation you need is:
To hit a target at range x and altitude y when fired from (0,0) and with initial velocity v the required angle(s) of launch θ are:

Additionally, leave the air friction of the sim on, and set the airFrictionMult of the shell to 0.
Just to remember to adjust for the TRUE position of the shell's origination point. It won't be 0,0 like stated. All you need do is find the difference in x and y between the origination point and the target. That should treat the origination point like it was (0,0) and give you the relative position of the target (x, y) to the origination point of the shell.
To hit a target at range x and altitude y when fired from (0,0) and with initial velocity v the required angle(s) of launch θ are:

Additionally, leave the air friction of the sim on, and set the airFrictionMult of the shell to 0.
Just to remember to adjust for the TRUE position of the shell's origination point. It won't be 0,0 like stated. All you need do is find the difference in x and y between the origination point and the target. That should treat the origination point like it was (0,0) and give you the relative position of the target (x, y) to the origination point of the shell.
- itsallinyourhead
- Posts: 3
- Joined: Thu Jul 01, 2010 5:26 am
19 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest




