# How do I cause a character to jump away from point of origin (planet)?

This topic is 1183 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am making a game for a class and part of the game is set up such that the player runs around a planet and the camera follows the player. In other words, if you zoom out and keep the camera stationary, you will see the player running around a planet with his feet always pointing toward the center of the planet.

I want to make the player jump away from the planet and have "gravity" pull him back.

Currently the game is set up such that when you press the space bar it gives the player a y velocity and gravity pulls him back. This works fine for a normal platformer game but if you have the player at an angle the x and y coordinates change depending upon the angle. I'm also not sure how to give the player an initial velocity because that would also depend on the angle.

It was suggested to someone else in this forum to create a gravitational force that affects the player and another force that opposes the player to keep him on the surface of the planet. I don't know how to do that either.

Thanks!

Zach

##### Share on other sites
character up direction = Normalize(character position - center of planet);
jump => character velocity += character up direction * jump speed;
gravity => character acceleration -= character up direction * gravitational force; // If you want to have a distance falloff like "real" gravity, use a standard gravitational equation here.

This is one general idea. You obviously will need to adapt this to factor in your timestep, etc. Edited by Nypyren

##### Share on other sites

Thank you very much for your help. I haven't been able to get it to work yet but I will keep trying.

Zach

##### Share on other sites
Nypyren has an excellent solution, I just wanted to add something.

When stating the problem you used the word angle. If you have a solution that involves using angles with trig functions (sin, cos, atan ect) then there is almost always a cleaner, faster, and more robust solution using vector algebra.

Instead of angles, think direction as defined by a unit vector. Instead of trying to calculate angle differences use dot product and cross product. Those are my two bits.

##### Share on other sites

I've added jumping to my current project which is a 2D RPG top down tilemap view sort of a thing. Whenever I work with tiles in my tilemap I think in terms of x,y co-ordinates. For any instance of a character in my game I track current x and y positions and velocities. But when looking at it that way, "y" doesn't represent height from the ground, which is actually what you want to change for your character when a jump button is pressed. So you need separate variables to track height and speed from the ground. You can then translate this height value to a y value on your screen.

I will confess that I went and called those height values "z" in my game which I believe is not actually correct or at least standard when working in 3D environments (I believe z typically represents depth not height). Fortunately I don't have to answer to anyone for the code that I'm writing and I will never work with a team (on this project) and have to explain the choice. You should probably pick a better name for however you refer to "height from the ground".

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634140
• Total Posts
3015761
×