Page 1 of 1

Need help with some math...

Posted: Mon Oct 03, 2005 10:04 pm
by Joakim Christiansen
Image
The variables are:

Code: Select all

PlayerX
PlayerY
PlayerDirection
BulletX
BulletY
BulletDirection
I know what BulletDirection is going to be. :lol:
But not the XY position, I suck at math... :oops:
Any help would be appreciated! :D

Posted: Tue Oct 04, 2005 3:33 am
by dmoc
Straight line with a fixed life time (or distance). Assuming you have player/gun angle, work out x and y delta's (change in position per sec, scaled by game loop time) and simply add these to bullet position until max distance/time. By the way: x=CosA, Y=SinA (watch out for +/- sign). Google for basic trigonometry. HTH

Posted: Tue Oct 04, 2005 6:00 am
by Paul
I had a few minutes before bed... maybe this will help??
Arrow keys to move and turn, spacebar to shoot.

Code: Select all

If InitSprite()=0 Or InitKeyboard()=0 Or InitSprite3D()=0
  MessageRequester("Error","Problem Initializing DirectX")
  End
EndIf



#RAD=0.0175
#Guy=0
x.f=320
y.f=240
Xg=9
Yg=-24
speed.f=0


If OpenScreen(640,480,32,"")
  SetFrameRate(75)
  LoadSprite(#Guy,"guy.bmp",#PB_Sprite_Texture)
  CreateSprite3D(#Guy,#Guy)
  
  Repeat
    ExamineKeyboard()
    If KeyboardPushed(#PB_Key_Left)
      rot-2
      If rot<0:rot=359:EndIf
    EndIf
    If KeyboardPushed(#PB_Key_Right)
      rot+2
      If rot>359:rot=0:EndIf
    EndIf      

    
    speed=0
    If KeyboardPushed(#PB_Key_Up)
      speed=2
    EndIf      
    If KeyboardPushed(#PB_Key_Down)
      speed=-2
    EndIf      
    x-Cos((rot+90)*#RAD)*speed
    y-Sin((rot+90)*#RAD)*speed
    If x<0:x=0:EndIf
    If x>639:x=639:EndIf
    If y<0:y=0:EndIf
    If y>479:y=479:EndIf
    gunx=x+((Xg*Cos(rot*#RAD))-(Yg*Sin(rot*#RAD)))
    guny=y+((Xg*Sin(rot*#RAD))+(Yg*Cos(rot*#RAD)))


    If KeyboardPushed(#PB_Key_Space) And fire=0
      fire=1
      shotx=gunx
      shoty=guny
      shotincx=Cos((rot+90)*#RAD)*5
      shotincy=Sin((rot+90)*#RAD)*5
    EndIf

            
    ClearScreen(0,0,0)      
    Start3D()
      RotateSprite3D(#Guy,rot,0)
      DisplaySprite3D(#Guy,x-32,y-32)
    Stop3D()      
    
    StartDrawing(ScreenOutput())
      If fire
        shotx-shotincx
        shoty-shotincy
        If shotx>0 And shotx<639 And shoty>0 And shoty<479 
          Circle(shotx,shoty,5,RGB(255,0,0))
          Else
          fire=0
        EndIf
      EndIf         
    StopDrawing()
          
    FlipBuffers()            
  Until KeyboardPushed(#PB_Key_Escape)  
EndIf
End
Here is the image I used:
Image
http://www.reelmedia.org/test/guy.bmp


Enjoy! :)

Posted: Tue Oct 04, 2005 2:23 pm
by Joakim Christiansen
Thank you very much Paul! :D