Jump to content

Detect no movement


Telash
 Share

Recommended Posts

Hey guys,

Is it possible to detect if an object is not moving without relying on velocity?

 

I have a series of 'rocks' falling to the ground, stacked ontop of each other, with a slight bounce to make it seem a little bit more realistic, however I want to trigger another action once that are totally stationary (no longer bouncing) and I am unsure how to proceed.

I have tried using '.bound.touching.down' and even a timer, however neither seems to give me the desired results.

touching.down activates on the first bounce and the timer, which while being more reliable does happen to fall just short (sometimes the falling sprites are delayed for whatever reason and the timer is activated already, triggering the event early).

Does anybody happen to know how I can solve this?

Thanks in advance :)

 

Edit: The reason I am not relying on velocity is because i still require the gravity to be activate on the object.

Link to comment
Share on other sites

  • 5 months later...
//check to see if slime isn't in the same position as a few frames ago, otherwise turn around. (i.e stuck on unpushable boxes)        slime.checkPos++;    if (slime.checkPos > 5){        if (slime.oldPos[0] - slime.body.x < 1 && slime.oldPos[0] - slime.body.x > -1 &&            slime.oldPos[1] - slime.body.y < 1 && slime.oldPos[1] - slime.body.y > -1){            this.switchDirection(slime);        }else{slime.checkPos = 0; slime.oldPos[0] = slime.body.x; slime.oldPos[1] = slime.body.y;}    }

I recently wrote this into my update function that checks if my slime object has stopped next to a box it can't push, if it hasn't moved 1 pixel in 5 frames is turns around. I think it should work fine but I haven't tested it much, just set oldPos to [x,y] when you create whatever it is you want to check.

 

I haven't looked to see if there is a function in phaser to do this in a cleaner way.

Link to comment
Share on other sites

Yeah, Phaser caches the previous position and provides deltaX and deltaY properties for this kind of situation:

if (slime.deltaX === 0 && slime.deltaY === 0) {  // slime has not moved since the last frame}

However, this is only for the previous frame, and if you want to check if it's not moved over a longer period of time, you'll need to roll your own method for this.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...