Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Gravity, jumping, collision

Author
Message
wattywatts
17
Years of Service
User Offline
Joined: 25th May 2009
Location: Michigan
Posted: 15th Jul 2012 21:14
Hi everyone
I'm toying with the idea of making a polygonal sidescroller, and I'm not sure how to approach this bit. I apparently own dark physics, so I suppose I'll give that a try for this stuff (never used it before, is it easy?) but for now I'm not sure how to approach the character animation, in terms of jumping and gravity.
When I animate the player character's jump should I account for height in the animation itself, or keep him "on the ground" in the animation and account for that within the code?
Hopefully you can tell what I'm asking here, as well as the fact that I have very little idea what I'm doing.

http://mattsmith.carbonmade.com/
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 16th Jul 2012 18:23
You would probably need to play around with the different collision types as you go along with this. You can either use the bounding box surrounding the character or a sphere around it from the objects center.

For most things you do not actually need a physics library as it is easy enough to code most of it yourself.

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
wattywatts
17
Years of Service
User Offline
Joined: 25th May 2009
Location: Michigan
Posted: 16th Jul 2012 19:38
I'm not sure how to create the gravity effect though. I was thinking I could constantly force the character y values down in the loop and the collision would stop him from going through the floor, but then I'm not sure how to handle jumping.

http://mattsmith.carbonmade.com/
GregA
15
Years of Service
User Offline
Joined: 21st Jun 2011
Location:
Posted: 16th Jul 2012 21:29 Edited at: 16th Jul 2012 21:31
This is wildly over simplified....

First, go read the wiki page about vectors, and vector operations

http://en.wikipedia.org/wiki/Vector_space

Yeah, I know the page goes into 3nd year calculus... But you need to understand the associative property with regards to vectors.

Second, skim over Newtons second law.
[href="http://en.wikipedia.org/wiki/F%3Dma"]http://en.wikipedia.org/wiki/F%3Dma[/href]

After you understand those two topics, you now understand two things.

First, vectors are just like plain old numbers, they can be added, subtracted, multiplied and divided.

Second, objects in motion tend to stay in motion, and objects at rest tend to stay at rest.

Ok, and a quick look at Newtons second law...

1. Objects not only have a location, but they also have a motion.

For each frame of your application
Add all the forces acting on your character to its motion vector
Then Add your characters motion vector to his location vector



Then I add my own game makers simplification... Humans are slow and blind, when you collide with something forget calculating it exactly. Just return your object to its old location and zero out its motion vector, and let the forces start building again. Your players will not notice that you cheated.
wattywatts
17
Years of Service
User Offline
Joined: 25th May 2009
Location: Michigan
Posted: 16th Jul 2012 21:53
What's so wrong with something like this?

I'm not trying to say that you're wrong or anything of the sort, I'm just curious why something like that wouldn't work.

http://mattsmith.carbonmade.com/
GregA
15
Years of Service
User Offline
Joined: 21st Jun 2011
Location:
Posted: 16th Jul 2012 22:42 Edited at: 16th Jul 2012 22:50
Well, your character will fall at a static, non accelerating rate.

Then when he jumps, he will just teleport up to 50.


instead...

on edit: this is pseydocode...



Then let automatic collision do its thing you should be good to go
Kezzla
17
Years of Service
User Offline
Joined: 21st Aug 2008
Location: Where beer does flow and men chunder
Posted: 17th Jul 2012 02:16
here's a little example of simple jumping.

you would need to add conditions to control the length of spacebar press, but this shows how to make something jump.



so far as animation goes, I would leave the actor in the same position and just animate the jumping motion, then let your program move the actor up and down. It will give you greater control over the exact placement of your actor.

Sometimes I like to use words out of contents
wattywatts
17
Years of Service
User Offline
Joined: 25th May 2009
Location: Michigan
Posted: 17th Jul 2012 17:35
Kezzla that works great, I added in some simple collision and looks like for the most part I can just concentrate on graphics now. Thank you!
Thanks for your help too GregA!

http://mattsmith.carbonmade.com/
Kezzla
17
Years of Service
User Offline
Joined: 21st Aug 2008
Location: Where beer does flow and men chunder
Posted: 18th Jul 2012 02:02
no worries mate.

Sometimes I like to use words out of contents
wattywatts
17
Years of Service
User Offline
Joined: 25th May 2009
Location: Michigan
Posted: 20th Jul 2012 00:04 Edited at: 20th Jul 2012 01:31
Any clue on how to make the player jump up then come down, instead of holding the spacebar down to go continually up?

EDIT
I think what I'm actually asking is how to make something happen after a button is pressed, then let go of, instead of when the button is initially pressed. Maybe I should start a new thread for this question.

http://mattsmith.carbonmade.com/
29 games
20
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 20th Jul 2012 01:54
You need to set some flags that mean that the player has to release the jump key in order to initiate another jump. However, the exact nature of how you do this depends on the game play that you want. SO for instance, do you want:

1) Press and hold jump key
2) Player jumps up then falls
3) Upon landing player jumps up again as they have the jump key pressed

or

1) Press and hold jump key
2) Player jumps up then falls
3) Player lands and then play has to release then press the jump key again in order to jump up

There isn't a right answer, it's a matter of how you want the game to play. I've modified Kezzla's code to do the latter.



If you take out all the stuff to do with the variable "jumping" then you can chain together infinite jumps by constantly pressing-releasing-pressing the jump key.

Below is the method I use for jumping, this version happens to include double jumping (I can strip that out if you want).



Another jumping game play mechanic is one used in Canabalt where the player can do a quick tap for a low jump but then press and hold for higher jump, but only for a limited time.

One thing that you'll have to do is juggle around with thing like gravity and initial jump speeds (in Kezzla's code this would be gravity# and the 3 to -3 restriction on the force# variable) to get them to suit the scale of you game.
wattywatts
17
Years of Service
User Offline
Joined: 25th May 2009
Location: Michigan
Posted: 20th Jul 2012 03:05 Edited at: 20th Jul 2012 04:00
Thanks 29, I am trying to make a double jump.
I've got that sort of working, but it double jumps if I hold the jump button down and I can't seem to fix it.
I'm probably too tired tonight..

Here's my current code, if you wouldn't mind taking a look at it?



The player character's included if you'd like to run it, the code's tied to the animation.


EDIT
Nevermind, I figured it out!

http://mattsmith.carbonmade.com/

Login to post a reply

Server time is: 2026-07-08 01:55:37
Your offset time is: 2026-07-08 01:55:37