Simple Physics for a 2D game
Simple Physics for a 2D game
Simple Physics for a 2D game
Simple Physics for a 2D game
Simple Physics for a 2D game
Simple Physics for a 2D game Simple Physics for a 2D game Simple Physics for a 2D game Simple Physics for a 2D game Simple Physics for a 2D game Simple Physics for a 2D game Simple Physics for a 2D game Simple Physics for a 2D game
Simple Physics for a 2D game Simple Physics for a 2D game
Simple Physics for a 2D game
Go Back  Xtreme Visual Basic Talk > > > Simple Physics for a 2D game


Reply
 
Thread Tools Display Modes
  #1  
Old 05-21-2009, 04:29 AM
V-Rage V-Rage is offline
Newcomer
 
Join Date: May 2009
Posts: 6
Default Simple Physics for a 2D game


I've been developing a platformer 2d game and have already implemented gravity and collision detection and such. untill now the jump system was coded using the system's gettickcount. can anyone pseudocode me some er..pseudocode to help over with the jump using a timer?
emphasis on the word timer please
Reply With Quote
  #2  
Old 05-21-2009, 05:35 AM
darkforcesjedi's Avatar
darkforcesjediSimple Physics for a 2D game darkforcesjedi is offline
Trust me, I'm an

* Expert *
 
Join Date: Apr 2001
Location: In ur base, pwnin d00dz
Posts: 1,964
Default

You said you've already implemented gravity. Jumping requires nothing more other than upward velocity.
__________________
To err is human; to debug, divine.
Reply With Quote
  #3  
Old 05-21-2009, 06:49 AM
Qua's Avatar
QuaSimple Physics for a 2D game Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

Why're you so interested in using a timer for jumping? I suppose you have a game loop in your application, if you just apply gravity during that game loop, you just have to add an upwards velocity to the object to make it jump like darkforcesjedi suggested.

Usually that won't give you a very satisfying feeling when jumping though, so you might want to try tweak the jump a bit to get a better feeling.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #4  
Old 05-21-2009, 09:29 AM
V-Rage V-Rage is offline
Newcomer
 
Join Date: May 2009
Posts: 6
Default

Quote:
Originally Posted by Qua View Post
Usually that won't give you a very satisfying feeling when jumping though, so you might want to try tweak the jump a bit to get a better feeling.
which is why I want to implement a timer

and I cant do it in the game loop because the gravity is rendered again using another timer, rendering it directly through the game loop makes it go very fast(unless you can tell me anohter way to speed it down)
Reply With Quote
  #5  
Old 05-21-2009, 09:39 AM
Rockoon's Avatar
Rockoon Rockoon is offline
Joseph Koss

* Guru *
 
Join Date: Aug 2003
Location: Unfashionable End
Posts: 3,615
Default

Why is your gravity handled via a timer? Handle that in the game loop too.
Reply With Quote
  #6  
Old 05-21-2009, 09:57 AM
V-Rage V-Rage is offline
Newcomer
 
Join Date: May 2009
Posts: 6
Default

again, its way too fast without a timer , and its tile based if that makes a difference
Reply With Quote
  #7  
Old 05-21-2009, 11:18 AM
darkforcesjedi's Avatar
darkforcesjediSimple Physics for a 2D game darkforcesjedi is offline
Trust me, I'm an

* Expert *
 
Join Date: Apr 2001
Location: In ur base, pwnin d00dz
Posts: 1,964
Default

You need to measure the time between game loops:

V = V0 + gt

Use a System.Diagnostics.Stopwatch to time the loops. Once you know "t", multiply it by the gravity "g" and add that to the velocity in the vertical direction. Then you're free to increase the velocity (jump) and it will be diminished in the game loop by gravity.
__________________
To err is human; to debug, divine.
Reply With Quote
  #8  
Old 05-21-2009, 11:55 AM
V-Rage V-Rage is offline
Newcomer
 
Join Date: May 2009
Posts: 6
Default

Quote:
Originally Posted by darkforcesjedi View Post
You need to measure the time between game loops:

V = V0 + gt

Use a System.Diagnostics.Stopwatch to time the loops. Once you know "t", multiply it by the gravity "g" and add that to the velocity in the vertical direction. Then you're free to increase the velocity (jump) and it will be diminished in the game loop by gravity.

yeah, im trying that now , thank you so much
Reply With Quote
  #9  
Old 05-21-2009, 12:41 PM
Euroclydon's Avatar
Euroclydon Euroclydon is offline
Regular
 
Join Date: May 2009
Location: Northern Indiana, USA
Posts: 70
Default

Just a quick idea while you're working on jumps...

One cool jumping trick is variable height. This means you jump higher by holding down the jump key longer. A quick tap gives you a short hop.

I plan to use this one myself. The coding shouldn't be too difficult.
Reply With Quote
  #10  
Old 05-21-2009, 01:38 PM
Rockoon's Avatar
Rockoon Rockoon is offline
Joseph Koss

* Guru *
 
Join Date: Aug 2003
Location: Unfashionable End
Posts: 3,615
Default

Thats easy enough, just dont apply gravity while the jump key is depressed (or until a certain timeout)
Reply With Quote
  #11  
Old 05-21-2009, 03:07 PM
Euroclydon's Avatar
Euroclydon Euroclydon is offline
Regular
 
Join Date: May 2009
Location: Northern Indiana, USA
Posts: 70
Default

Of course, using a late timeout could give you an effect for a rocket pack.

You could even use variable gravity, setting it lower on certain levels to make them moon-like.

You could even try reversed gravity for upside-down levels...
Reply With Quote
  #12  
Old 05-22-2009, 04:05 PM
wolfstrike's Avatar
wolfstrike wolfstrike is offline
Junior Contributor
 
Join Date: Aug 2004
Posts: 336
Default

Timer is a bad word around here
__________________
The Pho·net·ic Programmer
Reply With Quote
  #13  
Old 05-22-2009, 07:09 PM
Rockoon's Avatar
Rockoon Rockoon is offline
Joseph Koss

* Guru *
 
Join Date: Aug 2003
Location: Unfashionable End
Posts: 3,615
Default

Not a bad word.

The timer has a purpose, it is just that this isnt it.

If you need to do something periodic, but otherwise only need to wait for input events, then the timer is perect. Realtime games violates this premise because they do not have periods of waiting.

If you are comming from an application-programmer perspective (the majority of VB programmers), this is a hard adjustment to make. The event-driven model doesn't work well for realtime games. Game loops still leverage events, but they are not driven by them.
Reply With Quote
  #14  
Old 05-23-2009, 12:49 PM
Euroclydon's Avatar
Euroclydon Euroclydon is offline
Regular
 
Join Date: May 2009
Location: Northern Indiana, USA
Posts: 70
Default

I've tried putting game automation in a Timer. The code grows and grows... My processor handles it alright up to a point, but I shudder to think how a slower system would react.

So what we have here is basically a choice between the easy way and the efficient way.
__________________
"Have a place for everything and keep the thing somewhere else; this is not a piece of advice, it is merely a custom." - Mark Twain
Reply With Quote
  #15  
Old 05-23-2009, 06:27 PM
Qua's Avatar
QuaSimple Physics for a 2D game Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

Whether to use timers or not is not really a performance issue, and I don't believe you would get better performance by using a while loop than using a timer. The reply from Rockoon explains rather well why timers isn't optimal for a game loop.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #16  
Old 05-24-2009, 04:04 PM
Euroclydon's Avatar
Euroclydon Euroclydon is offline
Regular
 
Join Date: May 2009
Location: Northern Indiana, USA
Posts: 70
Default

The timer continues to time in the background while the event code is executing. Again, this is only really a problem if the user's processor can't handle it.

Not trying to disagree, this is just a viewpoint I try to look from, since I began with VB on a much slower system than I have now. I understand the frustration of trying to get something to run like it's supposed to!
While I like a cool, fancy game just as much as the next guy, I try to keep things as compatible as possible.

Am I getting too far off topic?
__________________
"Have a place for everything and keep the thing somewhere else; this is not a piece of advice, it is merely a custom." - Mark Twain
Reply With Quote
  #17  
Old 05-24-2009, 04:21 PM
Qua's Avatar
QuaSimple Physics for a 2D game Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

Yes, the timer continue to raise the tick/elapsed event even though the previous one is not finished yet which is totally unacceptable for a game loop. The timer creates several threads on which it launches the tick events. Imagine have several game loops running at the same time - the result would be utterly unpredictable. One game loop might be moving objects around, while the other was trying to do collision detection.

This would introduce the topic of thread safety which would complicate the matter even more.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #18  
Old 05-25-2009, 05:02 PM
Euroclydon's Avatar
Euroclydon Euroclydon is offline
Regular
 
Join Date: May 2009
Location: Northern Indiana, USA
Posts: 70
Default

I sort of worked around the "piling" problem by using a flag at the start of the Timer event. Something like:

Code:
Sub tmrAction_Timer ()
If FlagNoTime = True Then Exit Sub 'A public flag variable
FlagNoTime = True
...
FlagNoTime = False
End Sub
This is only a partial fix, since piling only occurs if the system can't finish all the in-between code before the interval passes again. If that's the case, repeated calls to the event are still going to cause trouble on that system. Calls to other procedures just make it worse.

So if we're going to repeat code anyway, we might as well just loop.
Do you think DoEvents should take care of allowing input?
__________________
"Have a place for everything and keep the thing somewhere else; this is not a piece of advice, it is merely a custom." - Mark Twain
Reply With Quote
  #19  
Old 05-26-2009, 03:42 AM
Rockoon's Avatar
Rockoon Rockoon is offline
Joseph Koss

* Guru *
 
Join Date: Aug 2003
Location: Unfashionable End
Posts: 3,615
Default

When you abandon the timer and step up to a GameLoop with DoEvents, you need to think a little differently about input.

First you need to come to terms with the fact that all events will be processed at your DoEvents statement.

Now, the standard game loop does not process input during the DoEvents, but instead only *remembers* that the input event took place so that later it can act upon that knowledge. This memory is most easily expressed in the form of global variables which reflect the state of things.

Code:
' In a module
Public Sub Main()
  Initialize
  Do
    HandleInput
    UpdatePhysics
    Render
    DoEvents
  Loop Until terminated
  CleanUp
End Sub
Here, even the 'terminated' state variable is a memory of some input event (at a minimum, a memory of the form's Unload event having fired)

(one of the important things here is that the DoEvents is the last thing before the termination check, because it is possible to re-activate an unloaded form just by touching it and procedures such as Render() surely will touch it)

Code:
' games form module
Private Sub Form_Unload(Cancel As Integer)
  terminated = True
End Sub
Handling other keyboard input during events is similar.. you just remember what keys are down and what keys are up:

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  KeyIsDown(KeyCode) = True
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  KeyIsDown(KeyCode) = False
End Sub
..and then during your HandleInput, you check your KeyIsDown() array for the states that matter to you.
Reply With Quote
  #20  
Old 05-26-2009, 07:11 PM
Euroclydon's Avatar
Euroclydon Euroclydon is offline
Regular
 
Join Date: May 2009
Location: Northern Indiana, USA
Posts: 70
Default

Simple enough. (?)

Actually similar to the way I handled input via the Timer. One doesn't have to abandon event-based programming entirely. What a relief!
__________________
"Have a place for everything and keep the thing somewhere else; this is not a piece of advice, it is merely a custom." - Mark Twain
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Simple Physics for a 2D game
Simple Physics for a 2D game
Simple Physics for a 2D game Simple Physics for a 2D game
Simple Physics for a 2D game
Simple Physics for a 2D game
Simple Physics for a 2D game Simple Physics for a 2D game Simple Physics for a 2D game Simple Physics for a 2D game Simple Physics for a 2D game Simple Physics for a 2D game Simple Physics for a 2D game
Simple Physics for a 2D game
Simple Physics for a 2D game
 
Simple Physics for a 2D game
Simple Physics for a 2D game
 
-->