Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static and kinetic friction feature in forge_2d #3153

Closed
1 task done
tojewel opened this issue May 9, 2024 · 10 comments
Closed
1 task done

Static and kinetic friction feature in forge_2d #3153

tojewel opened this issue May 9, 2024 · 10 comments

Comments

@tojewel
Copy link

tojewel commented May 9, 2024

Problem to solve

Hi, I am developing a 2d board game (https://gg-carrom.web.app) where I need to set the static friction component for the puck to make more realistic. Forge_2d only supports one friction coefficient right now which is I think the kinetic friction.

Proposal

Is there any other way to set the static friction parameters? I assume we need to implement this feature.

More information

Here is a good explanation of the static and kinetic friction: https://www.khanacademy.org/science/physics/forces-newtons-laws/inclined-planes-friction/v/static-and-kinetic-friction-example

Other

  • Are you interested in working on a PR for this?
@ufrshubham
Copy link
Collaborator

Generally speaking, Forge2d is a port of Box2d. So any changes to the core physics should ideally be made in Box2d.

Coming to the question about friction, Box2d supports both. It just uses the same parameter for them.

@tojewel
Copy link
Author

tojewel commented May 9, 2024

Thanks for the insight! I didn't know about the Box2d's model before. A quick search tells me, they don't have the concept of static friction https://chat.openai.com/share/49a1b0db-a310-4c0b-9bf7-f5e15872dd6c

Do you think we can implement this feature as an extra/plugin without violating the consistency with Box2d. Or anybody know a way to implement this using one single friction parameter.

@ufrshubham
Copy link
Collaborator

A quick search tells me, they don't have the concept of static friction

You are searching at the wrong place. This is from box2d's own docs

”Friction is used to make objects slide along each other realistically. Box2D supports static and dynamic friction, but uses the same parameter for both.”

Source: https://box2d.org/documentation/md__d_1__git_hub_box2d_docs_dynamics.html

@ufrshubham
Copy link
Collaborator

Do you think we can implement this feature as an extra/plugin without violating the consistency with Box2d. Or anybody know a way to implement this using one single friction parameter.

I suppose one could implement such a feature easily. If you can find a way to modify the friction value dynamically, you'll be easily able to change it once the body starts moving.

@tojewel
Copy link
Author

tojewel commented May 9, 2024

Thanks again for the information. I see the explanation there "Box2D supports static and dynamic friction, but uses the same parameter for both." but still not sure how to implement the static and dynamic nature of the friction. If I take hint from your last message, is it like, I can set the bodies as static and when they get hit by some other bodies then I can make them dynamic if the collision impact crosses a threshold? If the impact doesn't cross the threshold, the friction will be considered static.

@ufrshubham
Copy link
Collaborator

ufrshubham commented May 10, 2024

You don't need to change the type of bodies for this. Friction is simply the resistance to applied forces due to surface contact. It just so happens that the frictional resistance is generally a bit higher while the surfaces are at rest as compared to in motion. Since the body can't be "in motion" as well as "in rest" at the same time, only one of the two frictions will be acting on it at any given time. This means the same friction parameter can be used for both types of friction. Which one it is depends on the state of the body. If the body is stationary, it is static friction, if the body is moving, it is kinematic friction. So essentially you can reduce the friction once the body starts moving, to simulate the effect of kinematic friction. All you need to do is, check if the friction can be directly changed for the body. You'll have to look into the Forge2d repo for this. If that is not possible you can probably use the contact APIs to change the calculated friction when two fixtures come in contact with each other. I haven't done that myself but that seems like a good way to control friction between surfaces.

Overall, just keep in mind that the only difference between static and kinematic friction is that the latter is slightly less compared to the former and gets applied only once the body starts moving. If you can achieve the same effect any other way, you won't even need to have 2 different friction coefficients.

@tojewel
Copy link
Author

tojewel commented May 10, 2024

Dynamically setting the friction parameter sounds like a promising solution. I will try that and will update you here. One related question would be, in my game (https://gg-carrom.web.app/) the pucks are moving on a board. In real life they have a friction with the board. But in my setting I couldn't create a board body component and then put the pucks on that to have the friction simulation realistic. Do you know what is the hack for that? Thanks!

@ufrshubham
Copy link
Collaborator

But in my setting I couldn't create a board body component and then put the pucks on that to have the friction simulation realistic.

Unfortunately forge2d and box2d don't have a built-in way to set friction for top-down games. But they do provide APIs to apply forces to bodies. And since friction is just a force applied in opposite direction to the relative motion of surfaces, you can manually apply an opposing force to the puck, while it is in motion, to simulate friction. You could also set linearDamping for the body to make it loose some velocity over time.

@spydon
Copy link
Member

spydon commented May 27, 2024

I'll close this since it seems to be fully answered by DevKage, feel free to write more on here if anything is unclear. :)

@spydon spydon closed this as completed May 27, 2024
@tojewel
Copy link
Author

tojewel commented May 27, 2024

But in my setting I couldn't create a board body component and then put the pucks on that to have the friction simulation realistic.

Unfortunately forge2d and box2d don't have a built-in way to set friction for top-down games. But they do provide APIs to apply forces to bodies. And since friction is just a force applied in opposite direction to the relative motion of surfaces, you can manually apply an opposing force to the puck, while it is in motion, to simulate friction. You could also set linearDamping for the body to make it loose some velocity over time.

Thanks, @ufrshubham, for the detailed suggestions. Make sense. I will try it and let you know how it turns out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants