Calculating angle

About advanced scenes, and the Thyme scripting language used in Algodoo.

Calculating angle

Postby Rideg » Fri Apr 30, 2010 5:15 pm

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
    } : {}
}
Please help me out. If you have any questions please tell me if I need to translate any variable.
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Calculating angle

Postby Mr_Stabby » Fri Apr 30, 2010 7:36 pm

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

Postby Rideg » Fri Apr 30, 2010 7:48 pm

Could you motivate it a little maybe give me some kind of script to work on. Thank you :D
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Calculating angle

Postby Kilinich » Fri Apr 30, 2010 8:30 pm

Just pick code from my robin hood scene.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Calculating angle

Postby KarateBrot » Sat May 01, 2010 3:17 am

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.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Calculating angle

Postby Rideg » Sun May 02, 2010 6:45 pm

Yeas ofcourse I've turned the airfriction off. I just don't know what I've done wrong :?
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Calculating angle

Postby KarateBrot » Sun May 02, 2010 9:26 pm

If you only want to hit the mouse's x-coordinate this formula is alright:
http://en.wikipedia.org/wiki/Trajectory
Image

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.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Calculating angle

Postby Rideg » Sun May 02, 2010 9:46 pm

artilleryPos - is that the point were it turns or were the bullet spawns?
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Calculating angle

Postby KarateBrot » Sun May 02, 2010 9:49 pm

where the bullet spawns. sorry should have made it clearer
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Calculating angle

Postby Rideg » Sun May 02, 2010 10:01 pm

Hmm it work in a way but it doesn't hit at the XPos(mouse).
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Calculating angle

Postby KarateBrot » Sun May 02, 2010 10:40 pm

where does it hit then?
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Calculating angle

Postby Rideg » Sun May 02, 2010 10:42 pm

at first it hits ahead of the xpos, then the xpos then below the xpos
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Calculating angle

Postby KarateBrot » Sun May 02, 2010 10:54 pm

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.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Calculating angle

Postby Rideg » Mon May 03, 2010 5:55 pm

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?
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Calculating angle

Postby KarateBrot » Mon May 03, 2010 8:36 pm

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?
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Calculating angle

Postby Rideg » Mon May 03, 2010 11:33 pm

the would if it would hit/pass the mouse x+y pos but I don't know how to do that.
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Calculating angle

Postby KarateBrot » Tue May 04, 2010 4:42 am

so you want to "hit" the mouse with your bullet?
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Calculating angle

Postby Rideg » Tue May 04, 2010 7:56 am

yeah. But my first intetion was to hit the x cord
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Calculating angle

Postby itsallinyourhead » Thu Jul 01, 2010 6:58 am

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:

Image

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


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 1 guest