Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  to jump  (Read 1924 times)
0 Members and 1 Guest are viewing this topic.
Offline Mandelbrot

Senior Newbie





« Posted 2011-10-28 11:37:37 »

[size=12pt]Hello everybody.[/size]

I have a little conception problem, and i would like your opinion please.

[size=12pt]What i wish :[/size]

  • A caracter can jump
  • The width of jump is variale ( in pixel )
  • The length of jump is variable too ( in ms )

For exemple the caracter can make a jump on 300 pixels in 1 seconde or another on 600 in 3 secondes.
it must fallow a parabole

Actualy i have succeeded to get the X position.
But it's difficult to get the Y position.

[size=12pt]The code :[/size]

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
        /**
    * Attributes
    */

        // is the caracter jumping ?
   private boolean isJumping = false;
   // is the jump begun ?
   private boolean isJumpingStarted = false;
   
   
   // Jump length (ex : 2 seconde)
   private int jumpPeriod = 2000;
   // Jump length index
   private long jumpIndex;
   
   // Frame / Second
   private final static int FPS = 1000/60;
   // Number of cycle during a jump
   private int jumpCycle = jumpPeriod / FPS;
   
   // Jump width
   private int jumpWidth = 300;
   // Step width / cycle
   private float pas = ((float)jumpWidth / (float)jumpCycle);

        /**
    *  Main loop
    */

   public void update()
   {
                // One external event put this boolean to true (mouse clic ou an another event for example)
      if(isJumping)
         jumping(System.currentTimeMillis());
   }

        /**
    * Jump function
    * @param gameTime
    */

   public void jumping(long gameTime)
   {
      // The jump was not yet initialised
      if(!isJumpingStarted)
      {
         jumpIndex = System.currentTimeMillis() + jumpPeriod;  
         isJumpingStarted = true;
      }
     
      // The jump is already initialised
      else
      {
         // Jump is finished
         // RAZ
         if(jumpIndex - gameTime <= 0)
         {
            isJumpingStarted = false;
            isJumping = false;
            velocity = BASIC_VELOCITY;
         }
         
         // Jump not finished
         // we change the velocity as a function of the length of the jump.
         else
         {
            velocity = pas;
         }
      }
   }


Do you think it right ?
Have you an advice for the y coordinate ?


[size=12pt]Thank for reading[/size]
and i wish my english is readable.



-----------------------------------------------------
I am French, be indulgent with my written words please
Offline zendar

Senior Newbie




Go Java!


« Reply #1 - Posted 2011-10-28 12:42:45 »


Not sure what you asking since you are not showing all the relevant code. The y position should be a function of the length of the jump in ms.
Offline Mandelbrot

Senior Newbie





« Reply #2 - Posted 2011-10-28 14:45:17 »

Re hello.

My caracter (on the picture, the blue square) has coordinates

x and y.

When it is moving to right, x is increasing and to left it's decreasing.

Right.
Now, when there are an event (here for example : when the caracter is colliding a box) the caracter jumps.
The caracter jumps during 2 seconds on 300 pixel.

It is good for the x move.
But i can't get the y.

The problem is to find the y from the x.

(i wish to be clear ?)

The initials coodinates can be everything, it is not important.

(ex (200;500))



On this exemple we can see 3 phases.

  • Before jump
  • During the jump
  • After the jump

Before the jump Y don't change and x has a fix velocity.
During the jump y (i will find it one day  Smiley) and x adjust its velocity for the jump lenght (2 seconds)
After jump, x and y return to their initials states


Thx for the reply Zendar =)

-----------------------------------------------------
I am French, be indulgent with my written words please
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #3 - Posted 2011-10-28 15:14:28 »

Not sure if you're thinking of this the right way, if you just want to jump.
Try and store the vectors of the character and update them as you go.
1  
2  
3  
update() {
   character.setX(character.getX() + character.getXVelocity());
}


Ofcourse check fro collision. I broke that down too much.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2011-10-28 18:34:14 »

Why not do it acceleration and velocity based? In other words, when he jumps, apply an impulse to his velocity, and constantly apply a downwards impulse from gravity. That is generally the best way to do things.

If you want to just make a parabola, that's generally pretty easy too. Just use a sine curve and it will look fairly natural if you tweak the curve you use. But it's never going to look as good as velocity-based jumping. Do that. Tongue

See my work:
OTC Software
Offline Mandelbrot

Senior Newbie





« Reply #5 - Posted 2011-10-28 18:54:26 »

Oki i'll try it.
But i think the result won't be a real beautifull parabola.
I'll try it and i'll say you if the result is good.

Thx  for answers.

-----------------------------------------------------
I am French, be indulgent with my written words please
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #6 - Posted 2011-10-28 19:13:24 »

Oki i'll try it.
But i think the result won't be a real beautifull parabola.
I'll try it and i'll say you if the result is good.

Thx  for answers.


Pretty sure this is how most side-view games does it, to be honest.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #7 - Posted 2011-10-31 02:20:00 »

Oki i'll try it.
But i think the result won't be a real beautifull parabola.
I'll try it and i'll say you if the result is good.

Thx  for answers.

It will indeed result in a parabola. The character's upward velocity is at its maximum when they first jump, and then is constantly decreased until it hits the minimum at the peak of the jump to become 0. Then the downward velocity is at its minimum just as it starts to drop and maximum once the ground is hit.

AKA parabola.

See my work:
OTC Software
Offline ReBirth
« Reply #8 - Posted 2011-11-03 12:20:19 »

To create paraboll move by using X and Y values, you can use simple math function X^2

Y = aX^2 + bX + c

or circle function

X^2 + Y^2 = R^2 (using this, you can set jump's height on R)

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #9 - Posted 2011-11-03 14:28:39 »

in case u aren't entirely clear about how to use the parabola function rebirth just showed, ur gunna have to get out pen and paper and solve them to fit how you want ur jump to be.

(u can just skip this part if you have ever taken algebra Smiley )

so to start you should decide how high the vertex of your parabola should be. I'm gunna say mine is 50 pixels

how long is your jump going to be, I'll say mine is 26 pixels long(even will be easier)

here comes the math. first you need to substitute in everything you do know (I'm going to actually use a different form of the parabolic equationf or my ease) The vertex is going to be at (0,50)

0 = (coefficient)*((half of jump length)- (x of vertex, which should be 0 right now)) + (y of vertex)
0 = a(13-0)^2 + 50
-50 = a(169)
-50/169 = a

so then we put that back into the original equation.

y = -50/169(x- (x value of character start jump + 13))^2 + ((y value of character when u start he jump)+50)

so say if you character started on the ground and at 100 pixels x value, and was currently at 110 pixels then the equation would be like this

y = -50/169(110 - 113)^2 + 50



having said all that I just want to add 2 things. first off, I may have made a mistake somewhere in here, so dont just follow it blindly. second off, eli 's(haha, I typed demonpants and erased cuase I remembered ur chaned name Tongue) way is probably better because there isnt as much math involved, but if you want your perfect parabola this should give it to you.

hope I could be of help Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #10 - Posted 2011-11-03 15:02:03 »

thanks h3ckboy for the explanation, I'm bit lay now~ Grin

additional note, parabol move like that is executed by one single press button such as jump. if you want the jump to be driven by arrow button, it'll be simpler. send your char to top with certain Dy as jump speed and time (or Y, choose one) as limit (max high). then return your char to ground with negative value of Dy (can be same or not to previous one). listen up to key event especially left and right buttons and update char's X value based on that. hope I make it clear.

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #11 - Posted 2011-11-03 15:10:06 »

well, you could instead of taking time or height, in order to make it a little more generic do what eli said, just add a constant negative value on y accelaration (or in reality it would be positive cause of coordinates).

so waht you could do is give the y acceleration a big boost at start of jump, and then during the loop just have a variable that is whether the character is off the ground (cause u can be off the ground without jump) and then add a line like this
if(!onGround) yacceleration-=1;

or something along those lines
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #12 - Posted 2011-11-03 18:52:59 »

so waht you could do is give the y acceleration a big boost at start of jump...

You mean the y VELOCITY, not acceleration.

Myomyomyo.
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #13 - Posted 2011-11-05 09:44:33 »

yeah, thats what I meant.

on a related note, that way is so simple to set up it took me like 5 minutes to implement it into a CS project I was doing. Most of the time was spent trying to figure out the stupid commands.

but the point is, that this way really does work quite nicely
Offline ReBirth
« Reply #14 - Posted 2011-11-05 13:28:00 »

why I take the height or time because I thought it's neccessary so you now when to decrease the Y velocity. On jumping the Y velocity is also used with positive value.

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #15 - Posted 2011-11-05 16:18:32 »

yeah, I used to use the height to know when to start falling, but it makes the jump constant because when you jump you really slow down until you reach the top of your jump, but if you just go up and then down, you are going to go the same speed the whole way.
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #16 - Posted 2011-11-06 02:48:34 »

You (OP) seem to look at this as if you're coming straight from your physics textbook which has focus on calculating airtime, maximum points, e.t.c of parabolas. You need to drop that, as it isn't applicable to games in a simple way. You need to start looking at how we solve physics problems with computers: With numerical integration. It's a scary word, but it only means that each iteration is based on the previous one, like the formula: y(n + 1) = y + dy. If you don't understand this formula, it basically means that the next value of y is equal to the current value plus the difference in y each frame.

What does this have to do with jumping? y is your y coordinate. dy is your change in y over time, equal to your velocity. You also have a ddy, also called dv (delta velocity), also called acceleration, also called gravity (har har).

I would implement it by having two different states for the player: on ground and in air. When he is on the ground he has a constant y, a velocity of 0 and an acceleration of 0 (gravity is countered by the surface he's standing on). When he is in air, he has a varying y value, a varying velocity and a constant acceleration. Each frame, you add the acceleration to your velocity, and then your velocity to your position. Finally you check for collisions.

To initiate a jump, when the player presses jump, check if you're currently on the ground. If you are, set state to in air, give the player a one time boost to velocity. The rest is handled by the above updating.

So sorry if I'm just repeating what everyone's been saying already... ._.

Myomyomyo.
Offline ReBirth
« Reply #17 - Posted 2011-11-06 04:22:31 »

Actually your post is easier to understand
Quote
When he is on the ground he has a constant y, a velocity of 0 and an acceleration of 0 (gravity is countered by the surface he's standing on). When he is in air, he has a varying y value, a varying velocity and a constant acceleration. Each frame, you add the acceleration to your velocity, and then your velocity to your position. Finally you check for collisions.

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #18 - Posted 2011-11-06 09:47:12 »

@theagentd

I never said that I used parabolas for jumping.... I in fact explicitly said that was NOT the best way to do it. I do it like eli said. And how is the post you quoted show that I am being to complicated? I was just saying why having a constant velocity is a bad thing.

I'm just going to assume that everything after the first sentence wasn't directed at me, because lets be honest, if I didn't listen in math class would I really have been able to explain all I just did?
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #19 - Posted 2011-11-06 11:47:15 »

@theagentd

I never said that I used parabolas for jumping.... I in fact explicitly said that was NOT the best way to do it. I do it like eli said. And how is the post you quoted show that I am being to complicated? I was just saying why having a constant velocity is a bad thing.

I'm just going to assume that everything after the first sentence wasn't directed at me, because lets be honest, if I didn't listen in math class would I really have been able to explain all I just did?
Ah, sorry!! That was directed at OPs first post and the general impression I was getting of his posts after quickly eyeing through it! The math class thing was just a random "If you don't get what that means, here's an explanation."! Changed... T______T

EDIT: MY GOD!!! How could I possibly add quote from you, h3ckboy? I must have completely forgotten that I had your quote up there!!! T_________________________________T

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (9 views)
2014-12-27 04:03:04

TheDudeFromCI (12 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (94 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!