# Fps jumping

28 replies to this topic

### #1 Demon Zito

Demon Zito

GMC Member

• GMC Member
• 21 posts
• Version:GM8

Posted 26 June 2011 - 07:38 AM

I got it working like that guy did but the cube I put on the ground instead of floating. I am wanting my character (first person) to jump on the cube, i have been trying for 2 years but im not smart enough to work out how to do it!!! , can anyone help me with the code for jumping up and/or onto a cube? thanks.
• 0

### #2 ihato

ihato

Destroyed

• Banned Users
• 739 posts
• Version:GM8

Posted 26 June 2011 - 10:16 AM

This is how i do it (Without "GMNewton" or "P3DC" dlls).
Begin Step Event
```var _var,inst;
inst=instance_place(x,y,block)
_var=2 // This is variaton. You can never realy be on the same z as the block, so this is used.
if (place_meeting(x,y,block)) //Usualy, my blocks are the same as sprites, so i use this
{
if (z>inst.z+_var) then
{
//Setting the gravity
zspeed=0
zgravity=0.4
z+=zspeed
zspeed-=zgravity
//Standing on the block
}
if (z>inst.z-_var) and (z<inst.z+_var) then {z=inst.z; zspeed=0;zgravity=0}
//Stoping, if you are smaller than it.
if (z<inst.z-_var) //
then{
x = xprevious;
y = yprevious;
if (abs(hspeed) >= abs(vspeed) && not place_meeting(x+hspeed,y,inst))
{ x += hspeed} else
if (abs(vspeed) >= abs(hspeed) && not place_meeting(x,y+vspeed,inst))
{ y += vspeed} else
speed = 0;}
}
else
{

zspeed=0
zgravity=0.4
z+=zspeed
zspeed-=zgravity
if (z<0) then {z=0;zspeed=0;zgravity=0}
}
```

Note: This not the copyed code from my game, but from my memory. I dont realy know will it work.

Edited by ihato, 26 June 2011 - 01:38 PM.

### #3 Demon Zito

Demon Zito

GMC Member

• GMC Member
• 21 posts
• Version:GM8

Posted 26 June 2011 - 11:37 AM

Thanks for the help but it came up with one error :
ERROR in
action number 1
of Begin Step Event
for object object_cam:

Error in code at line 3:
if (z>inst.z-_var) and (z<inst.z+_var) then {z=inst.z; zspeed=0;zgravity=0}
^
at position 8: Unknown variable inst

Edited by Demon Zito, 26 June 2011 - 11:37 AM.

• 0

### #4 ihato

ihato

Destroyed

• Banned Users
• 739 posts
• Version:GM8

Posted 26 June 2011 - 11:53 AM

:/
Can i give you an editable example of it?

### #5 Demon Zito

Demon Zito

GMC Member

• GMC Member
• 21 posts
• Version:GM8

Posted 26 June 2011 - 01:13 PM

erm what do you mean?
• 0

### #6 MasterOfKings

MasterOfKings

The True Master

• GMC Member
• 4888 posts
• Version:GM8

Posted 26 June 2011 - 01:57 PM

```var _var,inst;
inst=instance_place(x,y,block)
_var=2 // This is variaton. You can never realy be on the same z as the block, so this is used.
if (inst!=noone) { //if 'inst' returns noone (no object) then the following code will produce an error
if (place_meeting(x,y,block)) //Usualy, my blocks are the same as sprites, so i use this
{
if (z>inst.z+_var) then
{
//Setting the gravity
zspeed=0
zgravity=0.4
z+=zspeed
zspeed-=zgravity
//Standing on the block
}
if (z>inst.z-_var) and (z<inst.z+_var) then {z=inst.z; zspeed=0;zgravity=0}
//Stoping, if you are smaller than it.
if (z<inst.z-_var) //
then{
x = xprevious;
y = yprevious;
if (abs(hspeed) >= abs(vspeed) && not place_meeting(x+hspeed,y,inst))
{ x += hspeed} else
if (abs(vspeed) >= abs(hspeed) && not place_meeting(x,y+vspeed,inst))
{ y += vspeed} else
speed = 0;}
}
else
{

zspeed=0
zgravity=0.4
z+=zspeed
zspeed-=zgravity
if (z<0) then {z=0;zspeed=0;zgravity=0}
}
}```
I didn't bother with formatting, I just added it.

-MoK
• 0

### #7 Demon Zito

Demon Zito

GMC Member

• GMC Member
• 21 posts
• Version:GM8

Posted 27 June 2011 - 12:44 AM

i added that to the begin step event of my Obj_cam and the same error message came up. According to the error message "inst" is a unknown varible, how do i fix this?? it wont let me ignore it!

Edit : wait... i found something weird in my step event so i deleted it and now it works. but i how do i get Obj_cam to jump? it doesnt seem to specify which keyboard button makes my person jump, and when i walk into a block i walk straight through it??

Edited by Demon Zito, 27 June 2011 - 01:14 AM.

• 0

### #8 dialgpalkia

dialgpalkia

GMC Member

• GMC Member
• 392 posts
• Version:GM8

Posted 27 June 2011 - 12:51 AM

have you created the variable? like this maybe: create of object_cam
`inst = //whatever it is`
it might work. i havenot tested it but it works with other variables

Edited by dialgpalkia, 27 June 2011 - 12:52 AM.

• 0

### #9 Demon Zito

Demon Zito

GMC Member

• GMC Member
• 21 posts
• Version:GM8

Posted 27 June 2011 - 02:07 AM

hmmm i think the inst problem has been solved because i just had a weird file...but now idk how to actually jump? does anyone know how to make the character jump by pressing space? then if i press forward and i could land on the block? that wud be a huge help and im opening up a youtube account so i can post my problem on there, after i'll link you people to the video and it would be easier to diagnouse my problem.

Edited by Demon Zito, 27 June 2011 - 02:51 AM.

• 0

### #10 dialgpalkia

dialgpalkia

GMC Member

• GMC Member
• 392 posts
• Version:GM8

Posted 29 June 2011 - 10:49 PM

here you go:
Step Event:
```if (place_free(x, y + 1)) gravity = 0.5; else gravity = 0;
x += (4 * keyboard_check(vk_right) * place_free(x + 4, y)) - (4 * keyboard_check(vk_left) * place_free(x - 4, y));

if (keyboard_check_pressed(vk_space) && !place_free(x, y + 1))
{
vspeed = -9;
}```
collision with object_block (MUST be solid):
```move_contact_solid(direction,12);
vspeed = 0;```

• 0

### #11 Demon Zito

Demon Zito

GMC Member

• GMC Member
• 21 posts
• Version:GM8

Posted 01 July 2011 - 12:29 PM

that script made me unable to move my mouse or body and i still cant jump!
• 0

### #12 Demon Zito

Demon Zito

GMC Member

• GMC Member
• 21 posts
• Version:GM8

Posted 04 July 2011 - 08:19 AM

Here is a place where you can download my game, remember ...space = jump, jump onto green blocks. thats what i want, if u find anything else thats bad or weird delete, edit and modify. thank you all so very very much!!! when you are done upload it on that website or any other similar websites!! Link : http://www.host-a.ne...lollxlxlxlx.gmk thank you again
• 0

### #13 Sinaz

Sinaz

MCP Killer

• Retired Staff
• 2751 posts
• Version:GM8

Posted 07 July 2011 - 08:01 PM

First, I deleted the code you put in Step->End Step.

In CREATE EVENT:
```zFloorOffset = 20; //I need a value to check for landing.  Since the camera is floating, I'm offsetting it's own virtual floor to match.
meter = 20; //looks like 20 units is about a meter in the world (height of box.)
zGravity = 9.81 * meter / room_speed; //rate of gravity in meters converted to system units
isFreefall = false; //state boolean for being in freefall.
zSpeed = 0; //we'll use this just like h and vspeed.
zJumpHeight = 2 * meter; //max height of a jump... 1 meter
zJumpImpulse = sqrt( 2 * zGravity * zJumpHeight ); //impulse needed to overcome gravity and jump to jump height.
```

That all sets up the variables needed to calculate gravity and such. You see I make some literal conversions... this is so you can make changes in a human understandable way-- what is 20 units in the game world? 1 meter. If you want to know how certain factors change behavior, change one variable at a time and test it. Changing meters is a good place to play around.

Then, I added an STEP END STEP event to handle movement resolution:
```//resolve z movement
if(isFreefall){ //object in freefall
zSpeed -= zGravity; //add gravity to velocity (z direction goes UP, unlike y in 2D space.)
z += zSpeed; //add velocity to position
z = max(z, zFloorOffset); //resolve for hitting the floor.
if(z == zFloorOffset){ //on the floor
isFreefall = false; //turn off freefall state
}
}
```

If the object is in freefall, that is, his feet aren't on the ground (freefall includes going UP as well as DOWN,) then the code applies gravity to velocity and velocity to position. It then makes sure the object can't go through the ground, and disables the state of being in freefall if it detects the ground. NOTE: we have't accounted for jumping onto things yet! Let's crawl, then we'll walk... or jump... onto thing... as it were.

Finally, I added the jump code, because my first thought when looking at your game was "well, there's your problem... can't jump if there's no jump input!"

PRESS <SPACE>
```if(!isFreefall){ //can only jump when not in freefall
isFreefall = true; //freefall state ON
zSpeed =  zJumpImpulse;
}
```

Again, we check the state of being in freefall before doing anything else. So long as the player isn't in the air, he can jump. Later we cal talk about double jumping if that matters to you.

I have your edited game file--

Edited game file

I won't host this forever-- so get it now.
• 1

### #14 Titanium

Titanium

the perfectionist

• GMC Member
• 1105 posts

Posted 08 July 2011 - 12:46 AM

Maybe you really are jumping, but you're just not changing your projection? check you d3d_set_projection and make sure your zfrom is a variable.
• 0

### #15 Sinaz

Sinaz

MCP Killer

• Retired Staff
• 2751 posts
• Version:GM8

Posted 08 July 2011 - 01:07 AM

He wasn't jumping, I looked-- part of the problem was that he didn't even have the jump button wired up.
• 0

### #16 Demon Zito

Demon Zito

GMC Member

• GMC Member
• 21 posts
• Version:GM8

Posted 08 July 2011 - 04:27 AM

wow thankyou! that was really helpful! now i wonder how we jump on the blocks? :3
EDIT: As you can see the block i think is at ground level, then i will make more blocks the same, but higher, so it will be like a stair case!

Edited by Demon Zito, 09 July 2011 - 03:54 AM.

• 0

### #17 Sinaz

Sinaz

MCP Killer

• Retired Staff
• 2751 posts
• Version:GM8

Posted 09 July 2011 - 04:50 AM

wow thankyou! that was really helpful! now i wonder how we jump on the blocks? :3

Well, you know the height of the box... and you know the offset of the camera from the floor.

So if an object like a box has a sprite associated with it that represents its x/y foot print, you can use that to check for x/y collisions just like in 2D.

Then, when you jump, you first want to check if there is an x/y collision using the built in features of the engine. Then you can compare the z position minus the offset from the floor (being the z height of the "feet") to the z height of the object. If the z pos clears the z height of the object, then the object can pass over it (through the x/y position). Otherwise, the player will be colliding with an edge.
• 1

### #18 Demon Zito

Demon Zito

GMC Member

• GMC Member
• 21 posts
• Version:GM8

Posted 09 July 2011 - 05:24 AM

I can nearly understand what you are saying ... I not too sure how to put that as a code though.. though what you just said taught me a lot!
PS : sorry im not good with coding, but once this problem is over i can move on...this seems to be one of the only issues i have at the moment >_<

and when you mean the built in feature..the button in "control" and if that is what you mean...what x/y coordinates should i put in? and would the "objects" be set as "all" or "solid objects"...finally relative or NOT?

Thank you again for your patience!
• 0

### #19 Sinaz

Sinaz

MCP Killer

• Retired Staff
• 2751 posts
• Version:GM8

Posted 09 July 2011 - 08:10 AM

I can nearly understand what you are saying ... I not too sure how to put that as a code though.. though what you just said taught me a lot!
PS : sorry im not good with coding, but once this problem is over i can move on...this seems to be one of the only issues i have at the moment >_<

and when you mean the built in feature..the button in "control" and if that is what you mean...what x/y coordinates should i put in? and would the "objects" be set as "all" or "solid objects"...finally relative or NOT?

Thank you again for your patience!

So, one of the keys to coding is to simply be able to describe what you want to do in a clear logical fashion.

So... getting up on the box...

```psuedo-code plan:

Each frame, check for 2D collisions in the x/y.
If a collision is detected...
If the player is NOT in freefall...
resolve 2D collision
else
compare player z higher than object z height
higher?  ignore 2D collision (we'll resolve vertical collision in END STEP)
else resolve 2D collision (not high enough to clear obstacle)
```
In the END STEP event, you'd have to update the z motion code.

Instead of looking for just the floor, if you have detected a 2D collision AND passed the height test, you check for a landing on the z height of the obstacle rather than the floor.

Get that working, and we can talk about what happens if you walk off a ledge!
• 0

### #20 Demon Zito

Demon Zito

GMC Member

• GMC Member
• 21 posts
• Version:GM8

Posted 09 July 2011 - 08:35 AM

Ummmm do you know any tutorials that could make me know what you are talking about?
or you could just tell me the code and it would make everyones lives less painful, and i can study the code so i know what to do later on

Edited by Demon Zito, 09 July 2011 - 08:46 AM.

• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users