1 registered members (TipmyPip),
18,388
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
weird jump errors
#355647
01/28/11 14:00
01/28/11 14:00
|
Joined: Jan 2011
Posts: 65
reknak
OP
Junior Member
|
OP
Junior Member
Joined: Jan 2011
Posts: 65
|
Hi there, I'm trying to make a decent jump function, and it does run when I'm pressing spacebar (like intended), but my entity flies extremely fast (compareable with Superman I even think ^^) when jumping. I really have no idea what it is wrong the function, if someone knows please say so  . Here's the script (only the relevant parts of the script, with red the really relevant parts): /////////////////////////////// #include <acknex.h> #include <default.c> #define STATE skill1#define ANIMATION skill2 #define CREATOR skill3 /////////////////////////////// ... var jumpheight = 0;... // state 5: jumping //////////////////////////////////////////// function jump() { jumpheight = 1; my.ANIMATION += 8*time_step; ent_animate(me,"jump",my.ANIMATION,0); while (1) { jumpheight += 8*time_step; c_move(me, vector(0,0,jumpheight), NULL, GLIDE); if (my.ANIMATION > 50) { break;} wait(1); }
while (my.ANIMATION > 50) { jumpheight -= 8*time_step; c_move(me, vector(0,0,jumpheight), NULL, GLIDE); if (jumpheight <= 0) { break; jumpheight = 0; my.ANIMATION = 0; my.STATE = 1; } wait(1); } }
//////////////////////////////////////////////////////////////// function camera_follow(ENTITY* ent) { vec_rotate(camera.x,ent.pan); // rotate the camera position with the player vec_set(camera.pan,vector(ent.pan,-10,0)); // look in player direction, slighty down while(1) { camera.x=mywizard.x-(100)*cos(mywizard.tilt)*cos(mywizard.pan); camera.y=mywizard.y-(100)*cos(mywizard.tilt)*sin(mywizard.pan); camera.z=mywizard.z-(50)*sin(mywizard.tilt) + 50; mywizard.pan = camera.pan; mywizard.tilt = camera.tilt; wait(1); } } /////////////////////////////////////////////////////////////////////////// action wizard_walk() { mywizard = my; camera_follow(me); my.event = wizard_hit; my.emask |= ENABLE_IMPACT; VECTOR vFeet; vec_for_min(vFeet,me); // vFeet.z = distance from player origin to lowest vertex // set FLAG2 to make the wizard detectable by c_scan, // and store the wizard pointer in its own CREATOR skill // in case the wizard is detected directly set(my,FLAG2); my.CREATOR = me; my.STATE = 1; while (1) { // state 1: walking //////////////////////////////////////////// if (my.STATE == 1) { // rotate the entity with camera rotation // move the entity forward/backward with the arrow keys + strafe var distance = (key_w-key_s)*9*time_step; var distancestrafe = (key_a-key_d)*6*time_step; c_move(me, vector(distance,distancestrafe,0), NULL, GLIDE); // animate the entity my.ANIMATION += (1.2*distance) + (1.2*distancestrafe); ent_animate(me,"run",my.ANIMATION,ANM_CYCLE); // adjust entity to the ground height, using a downwards trace c_trace(my.x,vector(my.x,my.y,my.z-1000),IGNORE_ME | IGNORE_PASSABLE); my.z = hit.z - vFeet.z; // always place player's feet on the ground if (key_space) // spacebar pressed -> jump like a maniac! { jump(); my.ANIMATION = 0; my.STATE = 5; }
|
|
|
Re: weird jump errors
[Re: reknak]
#355961
01/30/11 02:28
01/30/11 02:28
|
Joined: Jun 2004
Posts: 2,234 Wisconsin USA
FoxHound
Expert
|
Expert
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
|
Should be as simple as lowering the amount you jump in each frame as well as lowering the total amount near the end of the jump.
--------------------- There is no signature here.
QUIT LOOKING FOR ONE!
|
|
|
Re: weird jump errors
[Re: FoxHound]
#355974
01/30/11 08:09
01/30/11 08:09
|
Joined: Jan 2011
Posts: 120 United States
Logan
Member
|
Member
Joined: Jan 2011
Posts: 120
United States
|
Yeah. You're actually increasing jump_height each frame, so the player is accelerating into the air, stopping abruptly, and shooting on back down. You start jump_height at 1 before the while() loop in jump(), but I'd recommend starting it high and then slowly subtracting, like FoxHound says. That's how real physics work--you push off with a given force, and then slowly you lose upward velocity to gravity, and then gravity overtakes you, and pulls you back down. You'll have to do the math to get it to align right with my.ANIMATION, but I'd start jump_height at 10ish, and then subtract time_step (or maybe 2 or 3 * time_step) every frame.
function jump()
{
jumpheight = 10;
my.ANIMATION += 8*time_step;
ent_animate(me,"jump",my.ANIMATION,0);
while (1)
{
jumpheight -= 2*time_step;
c_move(me, vector(0,0,jumpheight), NULL, GLIDE);
if (my.ANIMATION > 50) { break;}
wait(1);
}
while (my.ANIMATION > 50)
{
// keep decreasing the velocity by same value
// that's how it works IRL
jumpheight -= 2*time_step;
c_move(me, vector(0,0,jumpheight), NULL, GLIDE);
if (jumpheight <= 0)
{
break;
jumpheight = 0;
my.ANIMATION = 0;
my.STATE = 1;
}
wait(1);
}
}
|
|
|
Re: weird jump errors
[Re: Logan]
#356021
01/30/11 14:20
01/30/11 14:20
|
Joined: Jan 2011
Posts: 65
reknak
OP
Junior Member
|
OP
Junior Member
Joined: Jan 2011
Posts: 65
|
It still doesn't work, like e.g. I fly really fast backwards out of the level and just keep flying. I tried to lower the jumpheight increase (e.g. to 0.002 * time_step), but that didn't fix that it also. C_move also doesn't seem to stop after the jumpheight = 0. Does maybe someone has or knows a tutorial about making a jump function)?
function jump() { jumpheight = 10; my.ANIMATION += 8*time_step; ent_animate(me,"jump",my.ANIMATION,0); while (1) { jumpheight += 2*time_step; c_move(me, vector(0,0,jumpheight), NULL, GLIDE); if (my.ANIMATION > 50) { break;} wait(1); }
while (my.ANIMATION > 50) { // keep decreasing the velocity by same value // that's how it works IRL jumpheight -= 2*time_step; c_move(me, vector(0,0,jumpheight), NULL, GLIDE); if (jumpheight <= 0) { break; jumpheight = 0; my.ANIMATION = 0; my.STATE = 1; } wait(1); } }
|
|
|
Re: weird jump errors
[Re: reknak]
#356131
01/30/11 23:54
01/30/11 23:54
|
Joined: Jun 2004
Posts: 2,234 Wisconsin USA
FoxHound
Expert
|
Expert
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
|
He won't stop jumping because your while loop will only end if you get over 50 on my.animation, however you never increase my.animation in that loop. You do before that but not in that loop. Also avoid using "break" in your code. It is just sloppy.
--------------------- There is no signature here.
QUIT LOOKING FOR ONE!
|
|
|
Re: weird jump errors
[Re: reknak]
#356537
02/02/11 17:42
02/02/11 17:42
|
Joined: Jan 2011
Posts: 120 United States
Logan
Member
|
Member
Joined: Jan 2011
Posts: 120
United States
|
It's this line:
c_move(me, vector(0,0,jumpheight), NULL, GLIDE);
Switch the vector() and the NULL (also I think you should use nullvector instead of NULL for good practice):
c_move(me, nullvector, vector(0,0,jumpheight), GLIDE);
That should fix it. The first vector tells c_move how far to move the entity in RELATION to himself. So your player's "up" was really "up and forward" because he was leaning forward. But the second vector moves the entity according to absolute coordinates, i.e. WED coordinates, with no regard given to what the entity's pan, tilt, and roll were. Thus the second vector should always be used for gravity, jumping, wind, etc. Sorry I didn't catch that before.
EDIT: You'll of course have to change both times c_move appears in your function.
Last edited by Logan; 02/02/11 17:43.
|
|
|
Re: weird jump errors
[Re: Logan]
#356853
02/04/11 13:16
02/04/11 13:16
|
Joined: Jan 2011
Posts: 65
reknak
OP
Junior Member
|
OP
Junior Member
Joined: Jan 2011
Posts: 65
|
That did the trick logan, thanks  . I have one last thing, I noticed that when I jump, the second part (falling) doesn't start when my animation is above 50. I just keep hovering in the air. The "{jumpheight = 0;}" does run, but the other lines don't run. function jump() { jumpheight = 10; while (my.ANIMATION <= 50) { my.ANIMATION += 10*time_step; ent_animate(me,"jump",my.ANIMATION,0); jumpheight += 0.5*time_step; c_move(me, nullvector, vector(0,0,jumpheight), GLIDE); if (my.ANIMATION > 50) { break; } wait(1); } if (my.ANIMATION > 50) {jumpheight = 0;} while (my.ANIMATION > 50) { my.ANIMATION += 10*time_step; ent_animate(me,"jump",my.ANIMATION,0); jumpheight -= 0.5*time_step; c_move(me, nullvector, vector(0,0,jumpheight), GLIDE); if (jumpheight <= 0) { break; jumpheight = 0; my.ANIMATION = 0; my.STATE = 1; } wait(1); }}
|
|
|
Re: weird jump errors
[Re: reknak]
#356918
02/04/11 18:04
02/04/11 18:04
|
Joined: Jan 2011
Posts: 120 United States
Logan
Member
|
Member
Joined: Jan 2011
Posts: 120
United States
|
You set jumpheight to 0, and then immediately in the next while() you subtract 0.5*time_step and then say if jumpheight<=0 break. It's no wonder it's not working.  Now I don't know exactly what you were going for with that if(jumpheight<=0) thing but it's obviously short-circuiting your function, so you'll have to change it. Change it to if(my.ANIMATION >= 100) maybe, or better yet, change your whole while loop and move that stuff to after it.
while (my.ANIMATION > 50 && my.ANIMATION <= 100)
{
my.ANIMATION += 10*time_step;
ent_animate(me,"jump",my.ANIMATION,0);
jumpheight -= 0.5*time_step;
c_move(me, nullvector, vector(0,0,jumpheight), GLIDE);
wait(1);
}
// these lines will run after the loop ends. you don't need break;
jumpheight = 0;
my.ANIMATION = 0;
my.STATE = 1;
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|