Throwing A Grenade

Blitz3D Forums/Blitz3D Programming/Throwing A Grenade

Berserker [swe](Posted 2005) [#1]
Hi all Greetings from Sweden!

This time i've really tried before posting but after a while i get tired and eger to finish todays goals and start gaming some "real" games (Rainbow Six) =)

Well i think theres more people out there who needs to now the following aswell.

The thing is that i want to throw a grenade. I have got it to work as far as the grenade is moving +1 for ever 0.350th of a sekund and depending on how strong the throw is it goes different far away from its origin (the players hand). The nade also has collission checks and it explodes after 3sec after it has landed on ground. So im almost happy with it.

But now i want it to go in a realistic bow (dont know the english word). Anyway this is my code (the not working one):
Function Update_Grenade()

  For ThisNade.NadeType = Each NadeType

    If ThisNade\State = 1 Then;If its moving
      If (MilliSecs() + 350.0 > ThisNade\MovingTimer#) Then
(ThisNade\ThrowPower#/10.0)
                ThisNade\X# = EntityX(ThisNade\Ent)
        ThisNade\Z# = Float((ThisNade\Z# + ThisNade\MovingTimer#))
        ThisNade\Y# = Float((ThisNade\Y# + (ThisNade\Z# * ThisNade\Z#)))
                PositionEntity ThisNade\Ent,ThisNade\X#,ThisNade\Y#,ThisNade\Z#  
        ThisNade\MovingTimer# = MilliSecs()
      EndIf

      If Abs(EntityX(ThisNade\Ent,1)) > 10000 Then		FreeEntity ThisNade\Ent
        Delete ThisNade
      ElseIf Abs(EntityY(ThisNade\Ent,1)) >10000 Then
        FreeEntity ThisNade\Ent				Delete ThisNade						ElseIf Abs(EntityZ(ThisNade\Ent,1)) > 10000 Then
          FreeEntity ThisNade\Ent				  Delete ThisNade					ElseIf ThisNade\ThrowPower# <= 0.0 Then            
          ThisNade\ThrowPower# = 0.0             
          ThisNade\DetonateTimer#  = MilliSecs()
          ThisNade\State = 2      
        EndIf
    ElseIf ThisNade\State = 2 Then;Has stopped
      TranslateEntity ThisNade\Ent,0,Gravity#,0
      If MilliSecs() > (ThisNade\DetonateTimer# + 3000.0)Then
  DetonateNade(ThisNade\Ent)  
      EndIf
    EndIf
 
  Next

End Function


Sry if the code dont match on web...
Can anyone help?
Thanks!


Berserker [swe](Posted 2005) [#2]
I need to have the speed/power of the throw.
And the Angle of the throw.
I have them both but how to update Nade movement?


Berserker [swe](Posted 2005) [#3]
No?! anyone? This cant be that hard. If only it had been in 2D. I think i know the math for it just not how to update it. If it moves 1lenght (how far that is) every 0.25seks by the Y#=Float(ATan2(Z#)) .


_Skully(Posted 2005) [#4]
You mean you want to add gravity to the movement?

Just figure out what your game gravity should be based on your 350ms movement scheme and add that value to the movement vector each iteration


slenkar(Posted 2005) [#5]
I just used a physics engine.


Sledge(Posted 2005) [#6]

No?! anyone? This cant be that hard. If only it had been in 2D. I think i know the math for it just not how to update it. If it moves 1lenght (how far that is) every 0.25seks by the Y#=Float(ATan2(Z#)) .



So do it in 2D then, after which place a pivot at the origin and a second at the correct X and Y location on the same plane, make the second a child of the first, then rotate the first (yaw) so the orientation of the plane gives the second the required amount of movement along the z axis. Point the grenade at the second pivot and move it to catch collisions.


Stevie G(Posted 2005) [#7]
From the speed and yaw / pitch angle of the throw you can calculate the angular velocity. To save on the trig you can use a global pivot to assist. Sort of pseudo code ..

Const Gravity# = 9.8
Global Pivot = createpivot()

Speed# = 100
Yaw# = 300
Pitch# = -30
rotateentity Pivot, Pitch , Yaw , 0
tformvector 0,0,Speed,Pivot,0

Vx# = tformedx()
Vy# = tformedy()
Vz# = tformedz()

Then repeat this ....

PositionX = PositionX + Vx * Time
PositionZ = PositionZ + Vz * Time
PositionY = PositionY + Vy * Time - Gravity * Time*Time
if PositionY < GroundHeight
Vx = Vx * .9
Vy = - .75 * Vy
Vz = Vz * .9
endif

.... until the grenade explodes

Obviously you'll have to experiment with the values to get what you want but this should give you an idea.


Berserker [swe](Posted 2005) [#8]
TFormey huh?

I wanna do it in 3D with Z. Didnt hink i had to have a pivot for impact coords sice it might bounce on walls.

But thank you all, i'll looke in to this after ï've got some sleep =)


Stevie G(Posted 2005) [#9]
The tform commands have nothing to do with impact coords .. they are used only to get the initial velocity vector when the grenade is thrown. If you want it to bounce off walls / floor realistically you'll need to implement angle reflection based on the collision normal and velocity vector. There is code in the archives from various people to do this.

You asked how to simulate an arc when throwing ....


Berserker [swe](Posted 2005) [#10]
Well, im not saying youre wrong, sry if i offended you.
I've tried it now but since i use an Entity (wich is a sphere) it doesent seem to work. Heres my code:

The one to create the Grenade:


And the one that updates the nades movement:


i not sure if the ThisNade\ThrowPower# that messes things up. Its when you release the 'g' key that the Greande is throwed, and depending how long the 'g'-key had been hold down the nade has different high speed.
The max is 100.0 though.

PLease help me i think im pretty close to solving it now. =)

Thanks


Berserker [swe](Posted 2005) [#11]
Im getting tired at this now so im just gonna settle for a strate line movement for my nade. Unless this change is somewhat correct:
ThisNade\Y# = Float(EntityY(ThisNade\Ent,1) + Sin(-(ThisNade\MovingTimer#/10.0)))
        MoveEntity ThisNade\Ent,0,ThisNade\Y#,(ThisNade\ThrowPower#/10.0)


in the Update_Nade() function.

i dunno anymore =(


Stevie G(Posted 2005) [#12]
I'll take a butchers at your code tomorrow and see what happens. I have to admit your code does seem a little ott for what you want to acheive though!


Berserker [swe](Posted 2005) [#13]
Hey, maybe if i TurnEntity(ThisNade\Ent),0,89.0,0 at the beginning and then in the update just TurnEntity(ThisNade\Ent),0,-0.1,0 wouldnt that result in the nade (wich is moving at its z-axel) will slowly face the floor?


Stevie G(Posted 2005) [#14]
Just a quick demo of what I mean, use cursors to control turret and space to fire.

As you can see the motion is quite realistic , albeit not physically correct.

Stevie




Berserker [swe](Posted 2005) [#15]
Wow thanks! Thats exactly what i needed!

Only i cant implement it wreally in my game... I cant quite understand th "TformVector" and "TFormPoint" commands since its difficult and in english (im from Sweden).

I did get it to work at first but then i didnt want it to move with constant speed e.g "Timstep = 0.05" i wanted it to move according to the power wich it was fired with. I have it so that the longer you press 'g' the harder you throw, and i want the power to decrease after each check. That would result in the nade going different far depending to the "ThisNade\ThrowPower#" variable.

Also the Grenade doesent move directly out from the player theres something wrong with the RotateEntity perhaps i dont even need it...?
But then again without routation the nad always moves in a certain degree, or? well theres the problem with TForm.
I want it to go in the "EntityPitch#(Camera)".

Hope that made some sence. Im working on it but i have to get som exercise now =) would truly appreciate som tweaking help. But if not then thanx anyhow you have helped me alot!

Cya


Stevie G(Posted 2005) [#16]
The TimeStep should be the time it takes your game to update a single frame ... e.g. 1.0 / FramesPerSecond.

If you use the tform commands there will be no need to rotating the grenade before throwing it. In my example you could use ..

TFormPoint 0,0,10,CAMERA , 0 to get the starting position of the grenade 10 units in front of the camera. Note that the CAMERA rotation is taken into consideration.

TFormVector 0,0,TEST\Power, CAMERA, 0 can also be used to get the initial velocity of the grenade.

E-mail me the relevant routines as they stand and I'll have a look.

stevieATSteviegoodwin.plus.com


Berserker [swe](Posted 2005) [#17]
Finally after som tweaking it seems to work, it goes a little slow but the math seems to work! Thank you so very much. When i get some time over i might be able to make a CodeArchive entry on this (offcourse youre name will be mentioned).

I'll keep in touch... belive me =) Thanks and goodnight for now.


Stevie G(Posted 2005) [#18]
No worries. You may want to increase the power setting or increase the gravity to get a better effect. Remember the power will be the number of blitz units the grenade travels per second.


Berserker [swe](Posted 2005) [#19]

The TimeStep should be the time it takes your game to update a single frame ... e.g. 1.0 / FramesPerSecond.



How do i get my FPS? is it The "WaitTimer(FrameTimer#)"
i have one of thoose but cant see any big difference if i alter it. I have the "FrameTimer#" as a constant of 70.0


big10p(Posted 2005) [#20]
FrameTimer shouldn't be a float as it's a handle. i.e.
fps_timer = CreateTimer(60)
.
.
.
WaitTimer(fps_timer)