# Fire trigometric calculation fails big times help! (python Code)

So i have been trying to make an Object(A) fire an other ball in 60 degrees and hit an 3:rd object(B) no mather the distance between the objects. , this fails big times, this is my calculation:

mg(x/2*tan(V)) = N

where:
m = mass of fired object
g = gravity
x = distance between object A and B
V = degrees to fire in
N = force needed for the ball to hit object B

and this is the script used in game:

``````
import bgeimport math
cont = bge.logic.getCurrentController()
obj = cont.owner
scene = bge.logic.getCurrentScene()
ray = cont.sensors["ray"]
tim = cont.sensors["tim"]
fire = cont.actuators["fire"]
if tim.positive:
if ray.positive:
Target = ray.hitObject
#    Formel:
#    m*g*(x/2*tan(V)) = N
#constants
m = 1
g = 9.8
#variables
x = obj.getDistanceTo(Target)
print(x)
v = 60
V = math.tan(V1)
N1 = m*g
N2 = V*(x/2)
N3 = N1*N2
fire.linearVelocity = [0, N3, 0]
cont.activate(fire)
obj["tim"] = 0

``````

the object that i use to att the canon ball whit is set to 60 degrees and the linearVelocity is set to use local cordinates.
the fiered ball is also set to have 1 as mass and 0 in damping

the result of the code is that the object flyes like 100 times to far away, do any one have any ide’s on what i’m doing wrong?

I don’t know where you got that formula, but it is not even dimensionally correct. I think what you are after is

N = sqrt(g*x/sin(2V))

where N is the initial speed (not force) required to travel a distance x with an elevation of V.

Best wishes,
Matthew

Hi Matthew! thx for the reply!
, whell that formulla where exactly what i where locking for!
I got the formula that i put in my script straight from my head, and it where a while sins i where using math… so it might be alitle rusty anyway, in my defens so isen’t my forumla compleatly wrong, saying that it isen’t not even dimensionally correct is abit hard. Ofc the mass don’t mather as (as you pointed out) i’m locking for the initial speed and not the force applyed to get to that speed.

however if i change a feaw things I shud be able to get my formula to work: Sqrt(g*((x/2)*tan(V)))*1.1547 =~ N

Tbg.
esoneson