It is currently Tue Dec 10, 2013 2:08 am

 All times are UTC

 Page 1 of 1 [ 9 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: I need some maths help :PPosted: Mon Nov 24, 2008 4:10 am
 Level 1 Cleric

Joined: Mon Nov 24, 2008 3:51 am
Posts: 13
Location: Argentina
hello to everyone!

im working in a game in python...
and i need some help coding an function...

I need a function that calculates strength and angle of an moving object...
i mean,. a turret that shoots to the mouse position.. .I get this. but this needs to be realistic!... so i need a function that calculates an arc between those two points.

this problem is too dificult for my head
if someone can help me, i will be very grateful for that

PD: I added gravity to the proyectile... but I want to the proyectile pass through the cursor

please tell me if this can be done..

Top

 Post subject: Posted: Mon Nov 24, 2008 6:13 am
 Game Programming Guru

Joined: Sun Aug 15, 2004 6:20 pm
Posts: 1091
Code:
dx = mouse.x - turret.x
dy = mouse.y - turret.y
angle = atan2(dy,dx)

Gives you the angle at which you need to rotate the turret to "look at" the mouse.

Are you asking for a way to make it "rotate" towards the mouse, rather than just snap to directly looking at it?

_________________
Heard in #gpwiki: <wzl> is there some serious grammar fail in your line or am i just too intelligent for your logic

Top

 Post subject: Posted: Mon Nov 24, 2008 11:46 am
 Corpse Bride

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2221
Location: England
Want the turret firing at a fixed velocity?

I can tell you that there isn't always a solution, because sometimes the target can be out of range. And if it is in range, there isn't necessarily a unique solution.

These are your base equations:

z' = -(g*t) + sin(phi)*V
h' = cos(phi)*V

V is your fixed turret velocity, g is gravity constant, z is height of the projectile, h is ground distance from turret, phi is elevation angle of turret.

Integrate to give:

z = (-g/2)*t^2 + sin(phi)*V*t + z0
h = cos(phi)*V*t

z0 is the height of the turret gun above... erm... sea level.

Let L be the ground distance to target L=(dx^2+dy^2)^.5

So when we hit the target when h=L, z=z1.
z1 = height of target above sea level. So z1=z0 on a flat level playing field.

So, L = cos(phi)*V*T

T = impact time

So z1 = (-g/2)*T^2 + sin(phi)*V*T + z0

z1-z0 = (-g/2) * T^2 + sin(phi) * V * T

substitute in for T = L / (cos(phi)*V)

z1-z0 = (-g/2) * L^2 / [cos(phi)^2 * V^2]+ sin(phi) * V * [L/(cos(phi)*V)]

and solve this for phi.

To bring some consistency to the symbols, let z1-z0 = dz

So,

dz = (-g/2) * L^2/V^2 * sec(phi)^2 + L*tan(phi)

dz * cos(phi) = (-g/2) * L^2/V^2 * sec(phi) + L*sin(phi)

dz * cos(phi)^2 + (g/2)*(L/V)^2 = L * sin(phi)cos(phi)

use these identities:
sin(x).cos(x) = sin(2x) / 2
cos(x)^2 = [1+cos(2x)] / 2

dz * [1+cos(2*phi)] / 2 + (g/2)*(L/V)^2 = L * sin(2*phi)/2

dz + dz*cos(2*phi) + (g*L^2 / V^2) = L * sin(2*phi)

dz + (g*L^2 / V^2) - L * sin(2*phi) + dz * cos(2*phi) = 0

which is an equation of the form:

a + b.sin(x) + c.cos(x) =0

we want an equation of the form

d + e.sin(x+f) = 0

so that sin(x+f) = -d/e and x = invsin(-d/e) - f

Of course, if dz=0, then this part is redundant. Is dz=0 ??

I can't be bothered working that out in ascii Maybe someone else pick up here, else I'll finish it later.

Hope this is what you wanted

Top

 Post subject: Posted: Mon Nov 24, 2008 2:04 pm
 Funky Monkey

Joined: Mon Aug 16, 2004 2:48 pm
Posts: 1608
Location: Minneapolis, MN USA
Jasmine, you just gave me a flashback to my physics prof at uni -- Dr. Oz (his real last name was Osopowski, but he went by Oz).

His usual end-of-class routine was to put up some generic mechanics or other typical physics problem and then have the class work together to solve it. He'd guide us to list out all of the variables and setup the equations ... then he'd smile and say "and it's just calculus from there ... have a great day!" and he'd leave.

Of course, it was the calc I had problems with.

-Bryk

_________________
www.mwgames.com ... Dicey Curves, Space Mission, Jump Gate, Gem Raider, DareBase, Castle Danger, Keeps & Moats Chess

Top

 Post subject: Posted: Mon Nov 24, 2008 3:54 pm
 Level 1 Cleric

Joined: Mon Nov 24, 2008 3:51 am
Posts: 13
Location: Argentina
thank you Jasmine! you just give me the solution I was looking for.
now im going to implement this in the game to see if this works

and thanks for the help of the others.

sdw: i have the rotation, thanks also

Last edited by python_guy on Mon Nov 24, 2008 4:02 pm, edited 1 time in total.

Top

 Post subject: Posted: Mon Nov 24, 2008 3:58 pm
 Corpse Bride

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2221
Location: England
python_guy wrote:
thank you Jasmine! you just give me the solution I was looking for.
now im going to implement this in the game to see if this works

and thanks for the help of the others.

Okay

You know you don't need to work out the angle don't you? All you want are cos(phi) and sin(phi) as coefficients in:

z = (-g/2)*t^2 + sin(phi)*V*t + z0
h = cos(phi)*V*t

There are ways of getting these without going through phi. I never like (or advise) calculating angles unless it's really necessary.

So, for the rotation,

x= x0 + h * dx/L
y= y0 + h * dy/L

where

dx = mouse.x - turret.x
x0 = turret.x
etc

Brykovian wrote:
Jasmine, you just gave me a flashback to my physics prof at uni [...] He'd guide us to list out all of the variables and setup the equations ... then he'd smile and say "and it's just calculus from there ... have a great day!" and he'd leave.

One of my professors said "If I just told you the answer it would be neither here nor there." So he never worked anything out for us. Must be a professor thing.

Dictionary of Definitions of Terms Commonly Used in Math. lectures.

The following is a guide to terms which are commonly used but rarely defined. In the search for proper definitions for these terms we found no authoritative, nor even recognized, source. Thus, we followed the advice of mathematicians handed down from time immortal: "Wing It."

CLEARLY: I don't want to write down all the "in- between" steps.
TRIVIAL: If I have to show you how to do this, you're in the wrong class.
OBVIOUSLY: I hope you weren't sleeping when we discussed this earlier, because I refuse to repeat it.
RECALL: I shouldn't have to tell you this, but for those of you who erase your memory tapes after every test...
WLOG (Without Loss Of Generality): I'm not about to do all the possible cases, so I'll do one and let you figure out the rest.
IT CAN EASILY BE SHOWN: Even you, in your finite wisdom, should be able to prove this without me holding your hand.
CHECK or CHECK FOR YOURSELF: This is the boring part of the proof, so you can do it on your own time.
SKETCH OF A PROOF: I couldn't verify all the details, so I'll break it down into the parts I couldn't prove.
HINT: The hardest of several possible ways to do a proof.
BRUTE FORCE (AND IGNORANCE): Four special cases, three counting arguments, two long inductions, "and a partridge in a pair tree."
SOFT PROOF: One third less filling (of the page) than your regular proof, but it requires two extra years of course work just to understand the terms.
ELEGANT PROOF: Requires no previous knowledge of the subject matter and is less than ten lines long.
SIMILARLY: At least one line of the proof of this case is the same as before.
CANONICAL FORM: 4 out of 5 mathematicians surveyed recommended this as the final form for their students who choose to finish.
TFAE (The Following Are Equivalent): If I say this it means that, and if I say that it means the other thing, and if I say the other thing...
BY A PREVIOUS THEOREM: I don't remember how it goes (come to think of it I'm not really sure we did this at all), but if I stated it right (or at all), then the rest of this follows.
TWO LINE PROOF: I'll leave out everything but the conclusion, you can't question 'em if you can't see 'em.
BRIEFLY: I'm running out of time, so I'll just write and talk faster.
LET'S TALK THROUGH IT: I don't want to write it on the board lest I make a mistake.
PROCEED FORMALLY: Manipulate symbols by the rules without any hint of their true meaning (popular in pure math courses).
QUANTIFY: I can't find anything wrong with your proof except that it won't work if x is a moon of Jupiter (Popular in applied math courses).
PROOF OMITTED: Trust me, It's true.

Top

 Post subject: Posted: Tue Nov 25, 2008 7:57 am
 Technomaniac

Joined: Sun Dec 05, 2004 11:27 am
Posts: 3250
Location: Sydney, Australia

I've always used "trivial" to mean the same thing as "possible"

_________________
Trying is the first step towards failure
b

Top

 Post subject: Posted: Tue Mar 17, 2009 1:04 pm
 Octogenarian

Joined: Wed Feb 25, 2009 1:06 pm
Posts: 87
Location: Belgium, Antwerp,Heist!
Jasmine ... What the....
o... i mean ...
You just slapped me back to the 4th grade
(just to say: whow your good at physiX )

_________________
'every problem has a solution'
'hacker: a programmer that deletes all the flaws and pushes to get that extra millisecond out of his pc/application'

Top

 Post subject: Posted: Tue Mar 17, 2009 5:49 pm
 Bibliotherapist

Joined: Wed Dec 21, 2005 6:23 pm
Posts: 6210
Location: Manchester, UK
N N N N NNNNECRO-POST!!!!

This is a thread from november... as much as complimenting Jasmine is nice, the OP has already abandoned the topic

_________________
God must love stupid people, he made so many.
theraje: 'God doesn't love stupid people, they're just easier to make'
http://sharedillusions.blogspot.com

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 9 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ Forums    Forum Rules and Posting Guidelines Wiki Discussion    Help    Content Issues Game Programming Discussion    C and C++ Game Programming    Java Game Programming    Language Agnostic Programming    .NET Game Programming    VB Game Programming    Mobile Game Programming    Web-Based Game Programming    Other Languages    OpenGL Development    Direct X Development Game Development Discussion    Game Design    Game Media Off-Topic Discussion    Announcements    Off-Topic    Community Projects    News
Powered by phpBB® Forum Software © phpBB Group