How to prevent monkey from snapping onto giraffe's head "platform" when jumping

85 views
Skip to first unread message

Maik Riechert

unread,
Mar 18, 2015, 12:46:10 PM3/18/15
to craf...@googlegroups.com
Hi guys,

out of fun a friend and I started creating a little game, it's in an early stage and we hit a problem which is weird and confusing, and we thought it should be handled out-of-the-box by Crafty.


Try jumping directly onto the head of the giraffe from the ground. You should not be able to reach it, but instead it snaps you onto it because it already intersects with the head platform. How can this be prevented? Isn't that a common thing to do? We use the standard Gravity component.

Thanks
Maik

mucaho

unread,
Mar 18, 2015, 1:19:05 PM3/18/15
to craf...@googlegroups.com
Hi

The Gravity system was recently improved in order to enable such custom behavior. The way I see it you have two options:
  • try to hack it into the current Crafty release, and/or copy&paste Gravity component into a new CustomGravity component, and make the necessary changes in there
  • build the newest development version and use the CanLand event to implement custom landing behavior

If you choose to go with the 2nd option, the following should work (please tell me if it doesn't).

var player = Crafty.e("2D, Gravity");
player
.bind("CheckLanding", function(ground) {
   
if (player._y + player._h > ground._y) // disallow landing of player, if the player's position is below ground
        player
.canLand = false;
});

Cute looking game btw! :)

Maik Riechert

unread,
Mar 18, 2015, 2:05:07 PM3/18/15
to craf...@googlegroups.com
Thanks, that indeed looks promising. I've no problem using a dev version, although let's do this first to make it more comfortable: https://github.com/craftyjs/Crafty/issues/885

By the way, why would I have to use underscored attributes like _x? Doesn't that indicate private variables not part of a public API which may break at any point?

Cheers
Maik
--
You received this message because you are subscribed to a topic in the Google Groups "Crafty" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/craftyjs/L_1KzbynGOM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to craftyjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

starwed

unread,
Mar 18, 2015, 2:12:56 PM3/18/15
to craf...@googlegroups.com
There are several properties (such as x,y, w, and h) which are actually getters/setters for the private versions (_x, _y, etc).  When you change the position of an object by writing e.x = 5, it sets e._x to 5 but also updates Crafty's spatial map, flags the entity to be redrawn, and triggers some other events you can listen for.  So in this case, e._x is private not because the interface might change, but because you should never set it directly, as that bypasses all of that.  It's ok to read from them, though, and will be (slightly) faster.  (Or at least, it was the last time I did a perf test.)

Maik Riechert

unread,
Mar 18, 2015, 4:48:38 PM3/18/15
to craf...@googlegroups.com
I see. I'm just trying the current dev version and have some problems adjusting to the changed semantics regarding meters. I used twoway(5,19) and gravityConst(1) before. When I use gravityConst(9) now it still jumps up extremely high and I have the feeling that it's also moving faster. Why all these breaking changes? What's the formula to adapt the values accordingly?

mucaho

unread,
Mar 18, 2015, 5:18:26 PM3/18/15
to craf...@googlegroups.com
Keep in mind it's still being implemented, so not 100% ready yet and it will likely change before release, but after another PR is merged the speed values should be backward compatible with the old version (at least my initial tests showed that).
Also your jumpspeed seems way to high (normally it's around 2 times the movement speed).

So did the proposed code snippet work? Is the monkey still snapping onto the girafe?

Maik Riechert

unread,
Mar 18, 2015, 5:46:13 PM3/18/15
to craf...@googlegroups.com
Nope, doesn't work. Now it just falls through the lower ground.

mucaho

unread,
Mar 18, 2015, 9:20:32 PM3/18/15
to craf...@googlegroups.com
Ok, I experimented with your code:
In order to debug better, you can add the SolidHitBox component to the monkey and the two jumping platforms, also you could remove display of giraffe temporarily to see the debug hitboxes better.
I changed the event callback to
.bind("CheckLanding", function(ground) {
 
if (this._y + this._h > ground._y + ground._h) // disallow landing of player, if player's feet are not above ground
     
this.canLand = false;
})

The above condition means the lower bound of monkey (== monkey's feet) must be above lower bound of platform in order for the monkey to land on it.
However, since your platforms are 1 px high, this will never register correctly, as Crafty does no CCD. Therefore I also set the platforms' height to 20 (which was determined empirically to not cause tunneling).

Maik Riechert

unread,
Mar 19, 2015, 4:21:50 AM3/19/15
to craf...@googlegroups.com
Thanks a lot! Works perfectly. Is it also possible to prevent the monkey from snapping to the platform when you jump from the left or right side and in theory wouldn't make it? Or when you are already on the platform and go a little to the left (or right) off the edge and then quickly go back, then it also snaps.

mucaho

unread,
Mar 19, 2015, 1:42:49 PM3/19/15
to craf...@googlegroups.com
Hi, Im glad it works, thanks for beta testing!
I think we should limit the downwards velocity when gravity is active (introduce settable terminal velocity) and document for user that he should make his platforms at least terminal velocity + 1 high.
I also found a bug while experimenting with your game.


Is it also possible to prevent the monkey from snapping to the platform when you jump from the left or right side and in theory wouldn't make it?
Unfortunately since CCD is not supported, I don't know of any way to avoid snapping completely. I fear we can only work around it as good as we can.

Or when you are already on the platform and go a little to the left (or right) off the edge and then quickly go back, then it also snaps
The quick left-right movement snapping could maybe be alleviated by forbidding the player to land, if the amount of frames that elapsed since the last "LandedOnGround" is very small

mucaho

unread,
Mar 19, 2015, 8:27:05 PM3/19/15
to craf...@googlegroups.com
Well, I kept tinkering and have some good news: I managed to significantly reduce the snapping and the resnapping when moving left-right quickly on the platform.

Here's how:
  • Changed the event callback to
.bind("CheckLanding", function(ground) {
   
if (this._y + this._h > ground._y + this._vy) // disallow landing of player, if player's feet are not above ground
       
this.canLand = false;
})
As you can see, the area checked for collision now dynamically scales with the vertical velocity.
This means that when vertical velocity is low (around jump peaks), the precision of detecting if the monkey is really above the platform is very high.

  • Then I reverted the jumping platforms to their initial 1px height (as you had originally).
  • Finally, set the monkey's initial vertical position to be slightly above the ground, y: H-FH-20-50
I think we should probably go ahead and include this directly into the Gravity component, as it's probably more realistic than the current behavior.

Maik Riechert

unread,
Mar 20, 2015, 4:49:20 AM3/20/15
to craf...@googlegroups.com
Hmm, not sure, but for me this reintroduces the snapping to the giraffes head. I currently have twoway(2,5) and gravityConst(9). The effect is that the monkey jumps up to the point where half of the monkey height has reached above the higher platform without snapping but then snaps as the velocity goes down further.

mucaho

unread,
Mar 20, 2015, 5:20:45 AM3/20/15
to craf...@googlegroups.com
Ah, yes, sorry for the confusion, I was using the crafty build where the unit scale has been fixed. (it won't work with current develop HEAD commit, as the velocity is still in meter scale there)
...

Maik Riechert

unread,
Mar 25, 2015, 5:34:30 PM3/25/15
to craf...@googlegroups.com
I just built crafty from your branch but I see the same snapping behaviour. I have twoway(5,17) and gravityConst(1) and when I jump from underneath the giraffe's head, I snap onto the head. Can you reproduce that?

mucaho

unread,
Apr 9, 2015, 11:20:19 AM4/9/15
to craf...@googlegroups.com
Hi Maik

I could not reproduce the issue so I went ahead and forked your project on github and made a new commit which eliminates the snapping for me.
I built crafty with the PR included.
Take a look at the commit.
...

Maik Riechert

unread,
Apr 9, 2015, 1:19:50 PM4/9/15
to craf...@googlegroups.com
Yes, it works for me too now... I must have mixed something up, sorry for the work I caused! Let's declare this as solved now. Also, I actually understand the logic behind it now, very clever! Thanks again :)

mucaho

unread,
Apr 10, 2015, 4:51:24 PM4/10/15
to craf...@googlegroups.com
Np, I'm glad it works now :) Thank you for beta testing :)
...
Reply all
Reply to author
Forward
0 new messages