Gravity in XNA

For discussions about game development that does not fit in any of the other topics.
Post Reply
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Gravity in XNA

Post by Xaleph »

Hi all,

So i FINALLY started working in XNA ( about time right? ) anyway, I`m going to make a simple 2d platformer-like game.

The thing is, I suck at math, big time. So i was wondering if anyone could explain in code how to create gravity. As my "understanding" of it lacks the visual representation on how to do it.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Gravity in XNA

Post by Jackolantern »

It depends on where you want gravity to come into play. If you have jumping, then you need gravity, acceleration and friction. What do you need gravity for?
The indelible lord of tl;dr
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Gravity in XNA

Post by Chris »

It's all about making comparisons between the ground axes and the player axes. This will need to be done anyway if the player has to clip against objects. First you need a collision detector. After that you can build the gravity and jump events. The gravity is quite simple. It's basically simply decrementing the y axes at a set rate.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Gravity in XNA

Post by Jackolantern »

I know this is nit-picking, but it is actually incrementing the y-value, since increased y is down. I figure that is probably what you meant though ;)
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Gravity in XNA

Post by Xaleph »

THanks all, yes it`s mostly for jumping, I created a hierarchical structure for game objects, all require vectors for positioning and rectangles for the container. A inheriting class is called the GravityObject, this should auto update the movement using gravity. So there should always be a downward push unless the Y axis is equal to a constant called FLOOR ( y=800)

Anyway, I Suck in math, big time. I never paid attention during Algebra so I now wish I kinda did.

Anyway, like I said, there should be a gravitational pull to the FLOOR all the time, unless the player is already there. So if he`s jumping, the velocity should be bigger then the gravitational pull,hence going up, et cetera, I read about it, but I cant visualise it..

Any and all help is welcome.

It helps thinking about Mario. Jumping et cetera, same movements hahaha.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Gravity in XNA

Post by Xaleph »

Oh one more thing, collision detection is not in yet, but i`m working on it for now, i`m not sure if I want the groud to be an object for intersecting because i`m not sure if there`s going to be gaps in the floor where you can fall trough, but if not: the constant Y axis height is 800 for now so that`s when I know it hit the ground.
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Gravity in XNA

Post by Chris »

Jackolantern wrote:I know this is nit-picking, but it is actually incrementing the y-value, since increased y is down. I figure that is probably what you meant though ;)
Good point. I'm thinking in 3d decrementing x :P. My engine places everything in positive axes. 1 is ground level.

Put a switch in the gravitational pull loop which tells the function to repel or attract, increment or decrement. Then place a countdown timer in the jump event which switches the gravitational to repel until the timer hits 0.

Another thing you'll have to think about is the force of the character moving back and forwards when he jumps. The more force the further he will move in the x direction.
Last edited by Chris on Sat Oct 29, 2011 6:31 pm, edited 3 times in total.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Gravity in XNA

Post by hallsofvallhalla »

base all gravity on a set formula and have all things use that formula.
Like creating a method that accepts values for max height.

As in for the player, hitting spacebar decreases Y by -value per millisecond to a total of max height. then increase by Y until collision
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Gravity in XNA

Post by Xaleph »

Yeah i got that, it`t the abstract GravityObject, so all inheriting classes will inherit the gravity, but i don`t know the formula`s, i`m totally new to this and when i mean i don`t know math, i really mean: I have zero understanding of math. Really. I never really used it. When I see cos/sin/tan tetha, my mind goes blank and will try to find something else to do. Usually this means that my hand will try and find a pen or pencil and start drawing silly pictures on any solid piece of paper, or anything really...

Anyway this is what i`ve got:

1. a downward pull ( GRAVITY )
2. a velocity < GRAVITY - until FLOOR or collision
3. a velocity > GRAVITY - until velocity <= GRAVITY

How to i code this? What vars do I need? How to i make it "jump" using an arc ( not linear? ohh fancy words right? )
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Gravity in XNA

Post by Chris »

Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “General Development”