Friction and exact collision

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
mitchellrivett
Prole
Posts: 9
Joined: Sat Mar 14, 2015 12:10 am
Location: Canada

Friction and exact collision

Post by mitchellrivett »

I am using Love's physics library and I've encountered a few problems:
  1. Friction
    I would like to get a dynamic body to slide over two static bodies at different speeds. How can I do this?
  2. Exact Collision
    When a dynamic body with no initial horizontal movement reaches the edge of a static body it tends to slide off. I would like this sliding to stop occurring such that the dynamic body falls once it passes the length of the static body.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Friction and exact collision

Post by ivan »

Hello, first of all friction is a coefficient between 0-1 and can be different for each fixture.
1. Set different friction values for the two static bodies and make sure the dynamic body's friction is greater than 0. Off the top of my head, the resulting friction in collisions is calculated using something very simple like 'colFriction = max(fix1.friction, fix2.friction)'. In the event I'm wrong (and 'min' is used instead of 'max') then you can catch that collision during the pre-solve phase and call: "contact:setFriction(friction)"
2. This happens because of the laws of physics, anything that moves will tend to continue moving unless acted upon by another force. You can try setting a high value for "linear damping" but then objects will move unnaturally (stopping abruptly with no inertia at all). If you're referring to the angular velocity, there is also body:setFixedRotation().
User avatar
mitchellrivett
Prole
Posts: 9
Joined: Sat Mar 14, 2015 12:10 am
Location: Canada

Re: Friction and exact collision

Post by mitchellrivett »

ivan wrote:Hello, first of all friction is a coefficient between 0-1 and can be different for each fixture.
Oh I was unaware of this feature. Thank you for informing me.
ivan wrote:2. This happens because of the laws of physics, anything that moves will tend to continue moving unless acted upon by another force.
I think you misunderstand what I'm trying to explain. I have attached an image to describe it.

Actually I have encountered more issues, which I have attached an image of. There are a lot of issues in this image however they have been found less frequently by lowering the friction from 1 to 0.7, however they still exist.
  • Applying an upward force every few seconds when a dynamic object is placed on a static object has a varying jumping height. I do not want this, I want the object to jump the same height every time.
  • Falling in to the the static object.
  • Floating above the static object.
Attachments
jump_height.gif
jump_height.gif (230.3 KiB) Viewed 4423 times
collision.gif
collision.gif (83.82 KiB) Viewed 4423 times
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Friction and exact collision

Post by raidho36 »

Are you letting physics do its job or are you interfering with it?
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Friction and exact collision

Post by ivan »

Something is off with either your rendering or your box2d setup when you have one fixture sinking into another.
It could be caused by many things, including a variable time step (using fixed time steps is a good starting point).
mitchellrivett wrote:Applying an upward force every few seconds when a dynamic object is placed on a static object has a varying jumping height. I do not want this, I want the object to jump the same height every time.
What you're describing is a jetpack and it's not how jumping works.
With jumping (and projectiles) you want to set the initial velocity ONCE and allow gravity to kick in.
I wrote a short article that you might find useful: http://2dengine.com/doc/gs_platformers.html

The second image looks like you have a circle fixture - as if you're drawing a red rectangle but the fixture is a circle.
Either that or your setup is incorrect - can't say conclusively without seeing the code.
Good luck!
User avatar
Tanner
Party member
Posts: 166
Joined: Tue Apr 10, 2012 1:51 am

Re: Friction and exact collision

Post by Tanner »

The box2d website also has a great resource on jumping mechanics. http://www.iforce2d.net/b2dtut/jumping
User avatar
mitchellrivett
Prole
Posts: 9
Joined: Sat Mar 14, 2015 12:10 am
Location: Canada

Re: Friction and exact collision

Post by mitchellrivett »

raidho36
raidho36 wrote: Thu Feb 09, 2017 9:51 am Are you letting physics do its job or are you interfering with it?
No, I am not interfering with the physics. The only thing I do is apply an upward force every few seconds. Not very frequently, it acts as a jump.

ivan
ivan wrote: Thu Feb 09, 2017 12:15 pm Something is off with either your rendering or your box2d setup when you have one fixture sinking into another.
It could be caused by many things, including a variable time step (using fixed time steps is a good starting point).
I can't find anything that would cause this except for using love.update's dt variable as a time step. I have attached my code and would appreciate it if you took a look.
ivan wrote: Thu Feb 09, 2017 12:15 pm What you're describing is a jetpack and it's not how jumping works.
With jumping (and projectiles) you want to set the initial velocity ONCE and allow gravity to kick in.
Setting velocity is a good idea, I will try that once I have the time. Currently I am using an force on the body to act as a jump.
ivan wrote: Thu Feb 09, 2017 12:15 pm The second image looks like you have a circle fixture - as if you're drawing a red rectangle but the fixture is a circle.
Either that or your setup is incorrect - can't say conclusively without seeing the code.
The shape is a rectangle. I have attached my code for you to look at the configuration.
ivan wrote: Thu Feb 09, 2017 12:15 pm I wrote a short article that you might find useful: http://2dengine.com/doc/gs_platformers.html
Tanner wrote: Thu Feb 09, 2017 12:19 pm The box2d website also has a great resource on jumping mechanics. http://www.iforce2d.net/b2dtut/jumping
I will read this once I have the time. Thank you for the informative posts.
Attachments
objects.lua
(2.79 KiB) Downloaded 60 times
main.lua
(2.29 KiB) Downloaded 61 times
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Friction and exact collision

Post by ivan »

Ok, so your gravity is way too high. Think about it: 300 m/s^2 is a huge value (in reality, it's around 9.8). Box2D has an upper bound for the maximum velocity of bodies so when you put an arbitrary number like 300, it will have an effect on various aspects of the simulation. Generally speaking you shouldn't have to invent magic values, if your math skills are good enough (from the previous link I posted: G = (2*jumpHeight)/(timeToApex^2)) you can calculate what forces/velocities to use so that your character can jump to a precisely given height.

Briefly going over the code this is what I noticed:

- use realistic values like 9.8 for the gravity, if you feel like your objects are too slow, adjust the love.physics.setMeter. It's always better to adjust the meter/rendering rather than to mess with the physics constants.
- fixed time steps are good and can be implemented using "accumulators".
- be very careful with setLinearVelocity/setAngularVelocity and don't mess with setPosition unless you are absolutely sure you know what you're doing. You want to let the physics simulation do its thing so the less you intervene the better it will work.
- box2d doesn't like very heavy objects resting on top of light objects, due to the positional correction the lighter objects will be squeezed out like the seeds of a watermelon, so keep that in mind when you stack objects
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 210 guests