Howdy, I decided to start to work on a sidescroller. I am having a bit of trouble getting my character to have a decent jump. What I have now is really robotic I was hoping to get it to feel more natural. I have probably searched through these and other forums for a good two hours and havn’t been able to find anything that helps. Could you guys give me a link to a tutorial or if easy enough post up some quick code. Thanks for the help.
This is much a physical movement you can find on many web articles about physics calculations formulas. 8)
I think that the most natural movement (which is also very easy to pull off) is to simply simulate gravity, and jump using an acceleration vector! In fact you should find that you will get the most natural movement that way in all senses.
- give the character a vertical speed (starting with an upward value)
- every game step, move the character up by that speed
- give him a vertical acceleration (a downward value)
- every game step, add the acceleration to the speed
You’d be surprised how much easier, smoother and more realistic it is to simply simulate physics!
p.s. liking the metal gear liquid shots
[quote]You’d be surprised how much easier, smoother and more realistic it is to simply simulate physics!
[/quote]
well yes, that’s what the steps above do…in a very basic but sufficient way ;D
lol oh yeah, it just sounds very very odd when you put it like that
because i didn’t know what coordinate system the topic starter uses, or i would have just said give the character a negative vspeed and a positive vacceleration)
What I do is I have two variables, gravity and speedy. Both values are float. Gravity is usually set between 0 and 1 (usually with two or three decimal values). speedy starts at 0 when the game begins. When your character needs to jump (such as the space key pressed) set your speedy variable to something like -8 (play around with both this and the gravity variable to get the desired effect) and in your game loop, you increment your y-position of your object by adding speedy to it (e.g. object.y += object.speedy;). After that line of code, you increment speedy by adding the gravity variable to it (e.g. object.speedy += object.gravity;). You’ll want to limit how high speedy can get (e.g. you don’t want it to be 100 or the character will fly through the floor). Usually it should be a no higher than your floor’s height divided by 2). I usually use this to limit it:
if (object.speedy > tileH/2) object.speedy = tileH/2;
(After adding the gravity to the speedy!)
Good luck,
JJ
This is a snippet of code that I used in a particle generator that I developed.
The code snippet also assumes you are using a coordinate system with every unit being one pixel.
/**
* Assuming no wind resistance/drag
* V = velocity
* THETA = angle of launch (must be in radian form)
* T = time traveled
* G = gravity
* x = V * cos(THETA) * T
* y = V * sin(THETA) * T - (.5 * G * T^2)
*/
function calculateNewData()
{
//CALCULATE HOW MUCH TIME HAS PASSED
long timeDifference = (System.currentTimeMillis() - eventTime);
//SO I CAN CALCULATE HOW FAR I'VE TRAVELED.
double distanceTraveled = velocity * timeDifference;
//CONVERT MY ANGLE INTO RADIANS BECAUSE THAT'S HOW REAL MEN DO MATH
double radTheta = Math.toRadians(theta);
//CALCULATE THE X-COORDINATE
int x = (int)(distanceTraveled * Math.cos(radTheta));
//CALCULATE THE Y-COORDINATE TAKING INTO ACCOUNT THE FORCE OF GRAVITY
int y = (int)((distanceTraveled * Math.sin(radTheta)) - (.5 * GRAVITY * Math.pow(timeDifference, 2)));
//ADD THE NEWLY GENERATED X,Y COORDINATES TO MY ORIGINAL POSITION
bounds.setLocation(new Point((int)(origLocation.getX() + x), (int)(origLocation.getY() + y)));
};