Page 1 of 1

Seek & Shot

Posted: Sun Oct 23, 2005 7:18 pm
by Psychophanta
Mmm... this is a help request post.
I am making a program to calculate speed and direction of an object in 3D space to reach an astro orbit.
I've made lots of things related to this.
You can try this elliptical particle movement and push LMB to reach it in 2 seconds (or the time you choose). The calculations are done in 3D, but it is displayed in 2D.
Well now i need this equation to be solved, and it is hard to me:
t^2 * M^2 = ((dx + Rbx * Cos(w*t+fi) + Rsx * Sin(w*t+fi))^2 + ((dy + Rby * Cos(w*t+fi) + Rsy * Sin(w*t+fi))^2 + ((dz + Rbz * Cos(w*t+fi) + Rsz * Sin(w*t+fi))^2
Solving 't' from this equation (all other terms are known) this tip can be done giving a stablished speed for the projectile launched, and the time is calculated depending on that speed.
Can someone help solving it? I can't, or i'd delay on it several days.
Thanks in advance.

Code: Select all

;******************************************************
;Seek & Shoot to elliptic way moving object (in 3D space):
;******************************************************
#PI=3.14159265:#DEGTORAD=0.01745329:#RADTODEG=57.2957795
#objradius=2:#refresh=60:#dly=16
#cursorradius=8:#Explosionsize=21
DefType .f
projspeed.f=5;<-rectilineus and constant speed for projectiles
MainAxisx.f=341:MainAxisy.f=0:MainAxisz.f=0
SecAxisx.f=0:SecAxisy.f=123:SecAxisz.f=0
phi.f=0; <- circular rotation angle
omega.f=0.0581333 ; <- circular rotation speed (angular speed)
time.f=2*#refresh
;
Procedure.b Collision(p1x.f,p1y.f,p1z.f,m1x.f,m1y.f,m1z.f,p2x.f,p2y.f,p2z.f,m2x.f,m2y.f,m2z.f)
  w.f=#objradius
  p1xp.f=p1x+w:p1xm.f=p1x-w:p1yp.f=p1y+w:p1ym.f=p1y-w:p2xp.f=p2x+w:p2xm.f=p2x-w:p2yp.f=p2y+w:p2ym.f=p2y-w
  p1zp.f=p1z+w:p1zm.f=p1z-w:p2zp.f=p2z+w:p2zm.f=p2z-w
  If ((p1zm<=p2zp And p1zp>=p2zm+m2z) Or (p1zp>=p2zm And p1zm<=p2zp+m2z)) Or ((p2zm<=p1zp And p2zp>=p1zm+m1z) Or (p2zp>=p1zm And p2zm<=p1zp+m1z))
    If ((p1xm<=p2xp And p1xp>=p2xm+m2x) Or (p1xp>=p2xm And p1xm<=p2xp+m2x)) And ((p1ym<=p2yp And p1yp>=p2ym+m2y) Or (p1yp>=p2ym And p1ym<=p2yp+m2y))
      ProcedureReturn 1
    EndIf
    If ((p2xm<=p1xp And p2xp>=p1xm+m1x) Or (p2xp>=p1xm And p2xm<=p1xp+m1x)) And ((p2ym<=p1yp And p2yp>=p1ym+m1y) Or (p2yp>=p1ym And p2ym<=p1yp+m1y))
      ProcedureReturn 1
    EndIf
  EndIf
  ProcedureReturn 0
EndProcedure
;-INITS:
bitplanes.b=32
SCREENWIDTH.l=GetSystemMetrics_(#SM_CXSCREEN):SCREENHEIGHT.l=GetSystemMetrics_(#SM_CYSCREEN)
If InitMouse()=0 Or InitSprite()=0 Or InitKeyboard()=0
  MessageRequester("Error","Can't access DirectX",0):End
EndIf
While OpenScreen(SCREENWIDTH,SCREENHEIGHT,bitplanes.b,"")=0
  If bitplanes.b>16:bitplanes.b-8
  ElseIf SCREENHEIGHT>600:SCREENWIDTH=800:SCREENHEIGHT=600
  ElseIf SCREENHEIGHT>480:SCREENWIDTH=640:SCREENHEIGHT=480
  ElseIf SCREENHEIGHT>400:SCREENWIDTH=640:SCREENHEIGHT=400
  ElseIf SCREENHEIGHT>240:SCREENWIDTH=320:SCREENHEIGHT=240
  ElseIf SCREENHEIGHT>200:SCREENWIDTH=320:SCREENHEIGHT=200
  Else:MessageRequester("Listen:","Can't open Screen!",0):End
  EndIf
Wend
;
CreateSprite(0,16,16);<-The mouse cursor
StartDrawing(SpriteOutput(0)):BackColor(0,0,0)
Line(0,0,15,10,$CABE2A)
Line(0,0,5,15,$CABE2A)
LineXY(5,15,15,10,$CABE2A)
FillArea(2,2,$CABE2A,$C0C1D0)
StopDrawing()
CreateSprite(1,#objradius*2,#objradius*2);<-the object
StartDrawing(SpriteOutput(1)):BackColor(0,0,0)
Circle(#objradius,#objradius,#objradius,$50F0CA)
StopDrawing()
;
ox.f=SCREENWIDTH.l/2:oy.f=SCREENHEIGHT.l/2:oz.f=0; <- geometrical center of ellipse
MainAxisMod.f=Sqr(MainAxisx.f*MainAxisx.f+MainAxisy.f*MainAxisy.f+MainAxisz.f*MainAxisz.f); <- radius 0 (a.k.a. semiaxis) and its length (modulo)
SecAxisMod.f=Sqr(SecAxisx.f*SecAxisx.f+SecAxisy.f*SecAxisy.f+SecAxisz.f*SecAxisz.f); <- radius 1 (a.k.a. semiaxis) and its length (modulo)
;Positionate both Focuses of the ellipse:
If SecAxisMod.f>MainAxisMod.f; <- it this is true, it means MAIN AXIS (where the Focuses are posistionated) SECONDARY AXIS must be swapped
  xch.f=SecAxisMod.f:SecAxisMod.f=MainAxisMod.f:MainAxisMod.f=xch.f; <- Swap it
  xch.f=SecAxisx.f:SecAxisx.f=MainAxisx.f:MainAxisx.f=xch.f; <- Swap it
  xch.f=SecAxisy.f:SecAxisy.f=MainAxisy.f:MainAxisy.f=xch.f; <- Swap it
  xch.f=SecAxisz.f:SecAxisz.f=MainAxisz.f:MainAxisz.f=xch.f; <- Swap it
EndIf
;
MouseLocate(ox.f,oy.f); <- lets locate mouse cursor just in the geometrical center of the ellipse
;-MAIN:
Repeat
  ExamineKeyboard():ExamineMouse()
  x.f=MouseX():y.f=MouseY():z.f=0
  targetoldx.f=targetx.f:targetoldy.f=targety.f:targetoldz.f=targetz.f
  vx.f=MainAxisx.f*Cos(phi)+SecAxisx.f*Sin(phi):vy.f=MainAxisy.f*Cos(phi)+SecAxisy.f*Sin(phi):vz.f=0
  targetx.f=ox.f+vx.f:targety.f=oy.f+vy.f:targetz.f=oz.f-vz.f
  targetmx.f=targetx.f-targetoldx.f:targetmy.f=targety.f-targetoldy.f:targetmz.f=targetz.f-targetoldz.f
  ClearScreen(0,0,0)
  If MouseButton(1) And shot.b=0
    shot.b=1
    shotx.f=x.f:shoty.f=y.f:shotz.f=z.f
    wx.f=MouseDeltaX():wy.f=MouseDeltaY():wz.f=0; <- speed vector of the base before launch projectile
    Gosub shot;<-shot to it
  EndIf
  ;
  If shot.b:Gosub moveshot:EndIf
  If collide.b:Gosub boom:EndIf
  ;
  DisplayTransparentSprite(1,targetx.f-#objradius,targety.f-#objradius)
  DisplayTransparentSprite(0,x.f,y.f);draw mouse pointer sprite
  phi.f+omega.f:If phi>=#PI:phi-2*#PI:EndIf
  FlipBuffers():Delay(#dly);<--swap buffers
Until KeyboardPushed(#PB_Key_Escape)
ReleaseMouse(1):CloseScreen():End
;-Subroutines:
shot:; FIXED TIME ALGORITHM:
  dx.f=ox-x:dy.f=oy-y:dz.f=oz-z
  shotmx.f=(dx+MainAxisx.f*Cos(omega.f*time+phi)+SecAxisx.f*Sin(omega.f*time+phi))/time
  shotmy.f=(dy+MainAxisy.f*Cos(omega.f*time+phi)+SecAxisy.f*Sin(omega.f*time+phi))/time
  shotmz.f=(dz+MainAxisz.f*Cos(omega.f*time+phi)+SecAxisz.f*Sin(omega.f*time+phi))/time
  initcount.l=ElapsedMilliseconds()
Return
moveshot:
  If shotx.f>SCREENWIDTH.l Or shotx.f<0 Or shoty.f>SCREENHEIGHT.l Or shoty.f<0:shot.b=0:Return:EndIf
  DisplayTransparentSprite(1,shotx.f-#objradius,shoty.f-#objradius);draw our projectile sprite
  timecount.f=(ElapsedMilliseconds()-initcount.l)/1000
  If Collision(targetx.f,targety.f,targetz.f,targetmx.f,targetmy.f,targetmz.f,shotx.f,shoty.f,shotz.f,shotmx.f,shotmy.f,shotmz.f)
    shot.b=0:shotmx.f=0:shotmy.f=0:shotmz.f=0
    collide.b=#objradius
    collx.f=targetx.f:colly.f=targety.f:collmx.f=targetmx.f/3:collmy.f=targetmy.f/3
  Else
    shotx.f+shotmx.f
    shoty.f+shotmy.f
    shotz.f+shotmz.f
  EndIf
Return
boom:
  If collide.b<=#Explosionsize
    StartDrawing(ScreenOutput())
    Circle(collx.f,colly.f,collide.b,$FFFFFF)
    StopDrawing()
    collx.f+collmx.f:colly.f+collmy.f
    collide.b+1
  Else
    StartDrawing(ScreenOutput())
    Circle(collx.f,colly.f,#Explosionsize*2-collide.b,$0F0FFF)
    StopDrawing()
    collx.f+collmx.f/2:colly.f+collmy.f/2
    collide.b+1:If collide.b>=#Explosionsize*2:collide.b=0:EndIf
  EndIf
Return
The name of the first person who solve the avobe equation in this thread (if there are one?) will be in a final program made in PB; an educative show (in a space-time scale of 1/31536000, this is: 1 year = 1 second) of what must be the angle and speed vector of a space-craft launched from Earth (or whatever) knowing some parameters of Earth (or whatever) and the target astro.

Posted: Tue Oct 25, 2005 3:30 am
by jack
t^2 * M^2 = ((dx + Rbx * Cos(w*t+fi) + Rsx * Sin(w*t+fi))^2 + ((dy + Rby * Cos(w*t+fi) + Rsy * Sin(w*t+fi))^2 + ((dz + Rbz * Cos(w*t+fi) + Rsz * Sin(w*t+fi))^2
some closing parenthesis are missing.

Posted: Tue Oct 25, 2005 9:34 am
by Psychophanta
Oh, at least one person read my post.
Objects at "empty" space don't move like the example around a massive object. Instead it move elliptically, but it suffer a big positive acceleration when approaching to the massive attractor object, and negative acceleration while get far from object.
I have all the equations of a real attracted mass movement referenced to the massive central mass position vector. There are wellknown also all the equations needed to calculate angular and linear speeds, masses of both objects, angular momentum, etc. Starting from a given data you can calculate easely other data of all it.
MMmm... i'm making this program in PB to show how it is, all easely to childrens and to adults.
When i get 't' in function of the other paramteres of the given equation, i can predict the exact time what will be taken from a spacecraft in reach a given astro, knowing the medium speed of the spacecraft.

Thank you Jack.
In fact no missing parenthesis, but surplus. This is the correct:

Code: Select all

t^2 * M^2 = (dx + Rbx * Cos(w*t+fi) + Rsx * Sin(w*t+fi))^2 + (dy + Rby * Cos(w*t+fi) + Rsy * Sin(w*t+fi))^2 + (dz + Rbz * Cos(w*t+fi) + Rsz * Sin(w*t+fi))^2 
Image

Posted: Tue Oct 25, 2005 10:34 am
by jack
MrMat could probably help here, as I am not physics savy but it looks to me you need a differential equation to solve your problem.

Posted: Tue Oct 25, 2005 3:27 pm
by Psychophanta
jack wrote:MrMat could probably help here, as I am not physics savy but it looks to me you need a differential equation to solve your problem.
I am no good with differentials and integrals. :?

Posted: Tue Oct 25, 2005 8:17 pm
by netmaestro
1. You should post this on a forum that has smart people on it.

2. Once written correctly, it looks to me like it would use a lot of CPU time.

:lol: :lol: :lol:

Posted: Tue Oct 25, 2005 8:31 pm
by Psychophanta
netmaestro wrote:1. You should post this on a forum that has smart people on it.
This has no much to do with to be smart, i think, but to enjoy maths :)
netmaestro wrote:2. Once written correctly, it looks to me like it would use a lot of CPU time. :lol: :lol: :lol:
Nooooo. Have you seen the example?:
It calculates the vector, this is, the correct direction in 3D of the craft (projectile) to reach the target always in 2 seconds. And it calculates that just when LMB is pushed.
The resolution of that equation will take the same time. FPU are really fast nowadays. And even it was done with a Z80, you wouldn't notice if the time taken is 1/1000000000 seconds, or 1/1000 seconds, because it would calcule it instantly.

Posted: Tue Nov 15, 2005 1:01 pm
by Psychophanta
Even this is probably not for the interest field of most of you, i will add this to this thread.
I got the solution for that. Not an analitic solution for the above equation and this other (needed for conservative central force):
t^2 · V^2 = (dx + s^2/(G·M·(e·Cos(w·t+F)+1))·Cos(w·t+F+a))^2 + (dy + s^2/(G·M·(e·Cos(w·t+F)+1))·Sin(w·t+F+a))^2
I say "not an analitic solution" because it seems that maths of nowadays don't allow an anallitic solution for this sort of equations. An equation like this
x = Cos(x)
has not an analitic solution nowadays. The only way to solve it is numerically. I did it:

Code: Select all

DefType .f  
Repeat
  f=Cos(x)
  x=f
Until Int(x*1000000)=Int(1000000*Cos(x))
;Check result:
Debug x
Debug Cos(x)
And i did it for both the above bigger equations i posted.
So i finally know how to do a Seek & Shoot to elliptic orbital object (in 3D space).
For those who are curious to see, here is the demonstration.