Jump to content

How to prevent "bouncing"?


Max
 Share

Recommended Posts

Hey everybody, here's another thing that I haven't been able to figure out by myself.

 

I'm using Arcade Physics for my game and I want most objects to not be passable. Setting immovable to true on them works great but the game doesn't seem to check for collisions between immovable objects (which makes sense, I guess) so they pass right through one another.

 

Setting immovable to false and bounce to 0, 0, I'm expecting the objects to not be passable and not move when something collides with them but if I hit those objects with my player-object, they seem to inherit the player's velocity and just start flying off.

 

My question is: how do I prevent collisions to impact an object's velocity but having them also not be passable.

 

A hacky approach would be to reset the object's velocity to 0, 0 (in update) before doing any movement calculations but this will obviously break any movement code in other places on that object. That makes this approach not very good in my opinion.

 

Thanks for all your help! :)

Link to comment
Share on other sites

Hmm this sounds odd. An immovable object should still allow collisions to be tested against it, but it won't itself register any collisions against anything else (because it's immovable, and so shouldn't be moving in order to collide with anything!) - see this example which sets a load of sprites as immovable to use as a 'floor' for the player sprite to run around on top of:

 

http://gamemechanicexplorer.com/#platformer-4

Link to comment
Share on other sites

Hmm this sounds odd. An immovable object should still allow collisions to be tested against it, but it won't itself register any collisions against anything else (because it's immovable, and so shouldn't be moving in order to collide with anything!) - see this example which sets a load of sprites as immovable to use as a 'floor' for the player sprite to run around on top of:

 

http://gamemechanicexplorer.com/#platformer-4

 

Thank you! I'm talking about two objects being immovable though. If you set a player object to immovable, it will pass through other immovable objects.

 

I don't understand why an object with bounce set to 0, 0 is still affected by impact though.

Link to comment
Share on other sites

If you set a player object to immovable, it will pass through other immovable objects.

 

Yes, this is intended - as I said, immovable means it shouldn't be moving; so if it is moving, it won't respect physics. I think I'd need to see more regarding what you're trying to do to be sure what the problem is exactly, and how we can (hopefully) solve it. Any chance you could upload your work in progress or show us a bit more of the code?

Link to comment
Share on other sites

Yes, this is intended - as I said, immovable means it shouldn't be moving; so if it is moving, it won't respect physics. I think I'd need to see more regarding what you're trying to do to be sure what the problem is exactly, and how we can (hopefully) solve it. Any chance you could upload your work in progress or show us a bit more of the code?

 

Ok, that makes sense. I think I must solve this with body.bounce then. I uploaded my prototype in its current shape here: http://pozzlegame.com/zombie-prototype

 

The police man's body is set to immovable. The nurse's body IS movable and bounce set to (0, 0). If you move the player (with arrow keys) to the nurse, you can see that she bounces away. I want her to stand still (it's what I'm expecting if I set bounce to 0) and the player not to move through her. I also want a similar solution for the police man without having to set his body to be immovable.

 

I bound the variables for police man, player and nurse to window.policeMan, window.player and window.nurse so you can easily access them from the console.

 

Thanks for helping man, it's very appreciated! :)

Link to comment
Share on other sites

Ok, by setting both to immovable, here it's working as you describe - the police man moves unrestricted left and right and you can't stop him or alter his motion at all. The nurse also stands completely still and cannot be pushed.

 

The 'bouncing' you're seeing is not really bouncing as defined by the bounce property, but just an object pushing another object (transfer of momentum) - bounce in this case is defined as an 'elastic' effect on the initiating object in the collision which causes it to reflect away from the object it's hitting (rather than just stopping like Newton's Cradle) but since you are in complete control over the player's velocity at all times, you will never see this effect happen. If you were to send the player flying at the nurse with a single velocity and then play with the bounce property, you'll see how this effect works.

 

Also because there's no drag (friction) on the nurse, when she does get hit, she flies off in one direction forever. If you apply some drag, you'll see she comes to a halt:

nurse.body.drag.setTo(100);

You can use high drag values to create 'pushable' objects, which slide along as long as something is pushing against it, but don't keep going when you stop.

 

I assume your collision rectangles are very short so that characters can walk behind and in front of one another and that's intended? If so, with immovable set on both characters, this seems to me to be working exactly as you need it to.

Link to comment
Share on other sites

For what it's worth, in this situation using immovable on a moving object is actually totally logical. Ideally, you'd want mass or inertia properties to be able to handle this, but mass doesn't work as expected in Arcade physics. If you try to make the police man's mass 10000 and the player's mass 1, and turn off immovable, the player can still easily alter the police man's motion and push him out of his patrol pattern. I suspect therefore that mass is another property that relies on velocity being something which is discreetly changed rather than constantly applied, and is used to calculate how much momentum an object imparts on another when under the influence of a single velocity application.

Link to comment
Share on other sites

For what it's worth, in this situation using immovable on a moving object is actually totally logical. Ideally, you'd want mass or inertia properties to be able to handle this, but mass doesn't work as expected in Arcade physics. If you try to make the police man's mass 10000 and the player's mass 1, and turn off immovable, the player can still easily alter the police man's motion and push him out of his patrol pattern. I suspect therefore that mass is another property that relies on velocity being something which is discreetly changed rather than constantly applied, and is used to calculate how much force an object imparts on another when under the influence of a single velocity application.

 

Thanks for taking the time to give me some insight and checking out the demo.

 

I'm honestly not satisfied with the immovable solution. I want zombies (who are also immovable) to be blocked by trees and other immovable objects but I don't want them to be pushed away by the player or other humans.

 

Solving this issue isn't super-urgent but these little imperfections in my game drive me crazy. :D Any other ideas maybe?

Link to comment
Share on other sites

Yeah, this compounds issues somewhat, and I honestly don't know of a solution that wouldn't end up being wildly hacky, like intercepting collisions before they happen and nullifying parts as needed. Given your low amount of sprites (I assume this will be the case in your game as it progresses) you could maybe try P2, which has more tweakable properties, collision groups and so on? Beyond that, I guess what you need is a way to say within Arcade physics 'object a is immovable to object b' and so on.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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