Applying force on dynamics with statics

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Senlaar
Posts: 12
Joined: Mon Jun 22, 2020 6:11 am

Applying force on dynamics with statics

Post by Senlaar »

Hello! Awesome library =)

I have a simple simulation with a static floor, static box, and dynamic sphere. I can apply linear force on the sphere in the plane of the floor, and it moves around exactly as expected. Unexpected, however, it what happens if the sphere is up against the box and I apply force in the box's direction: the sphere slowly enters the box instead of being blocked by it.

This seems solved by the character controller demo: moving via the TargetVelocity vector instead of directly applying force correctly stops the sphere right at the edge of the box. I was hoping to avoid the lines of code and required understanding of CharacterControllers.cs though. (Adding something like the sphere getting 'kicked' by a magical foot that is separate from normal character movement control seems complicated w/ CharacterControllers.)

I'm generally looking for a solution with the library that allows me to apply arbitrary force to dynamics in the simulation that will respect the rigid bodies of other dynamics and statics. Any chance someone has a pointer here on the simplest way to achieve this?

Thanks very much in advance!

-senlaar
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Applying force on dynamics with statics

Post by Norbo »

Sounds like the PositionFirstTimestepper, which integrates velocity into position at the beginning of the frame. If the velocity is modified outside the timestep, it'll move the body before collision detection or the solver has a chance to stop it.

Switching to the PositionLastTimestepper would probably fix the problem. You can choose which to use in the SImulation.Create function. Another option would be to move the velocity modifications into the BeforeCollisionDetection callback: https://github.com/bepu/bepuphysics2/bl ... per.cs#L60

PositionLastTimestepper technically has very slightly higher overhead than the PositionFirstTimestepper, but it's very unlikely that it'll matter. This isn't the first time new users have been confused by this default, so I think I'm going to remove the default and modify some demos.
Senlaar
Posts: 12
Joined: Mon Jun 22, 2020 6:11 am

Re: Applying force on dynamics with statics

Post by Senlaar »

Thank you Norbo! Using PositionLastTImestepper yields exactly what I was looking for. Feels great applying force to any of the objects.

Aaaand now I'm hoping I could bother you with another question :)

I'd like to give some dynamics and statics the ability to actively "push back" against some objects at a collision, so the striking objects receives more force than usual as it bounces off. Can this be achieved through narrow phase callbacks, or do I need to subscribe to the timestepper's BeforeCollisionDetection and/or CollisionsDetected? It seems like I would need the two objects in question, as well as the relevant normal to "push back" on (i.e. if I have a sphere hitting a cube, I need to know which side of the cube it has struck.) Are there any existing examples of this kind of logic at collision?

Thanks so much again. Really enjoying the library.

-senlaar
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Applying force on dynamics with statics

Post by Norbo »

Two approaches:
1) If you want a contact constraint to still be generated for the collision, you can let it be created and then examine contacts after the fact. The SolverContactEnumerationDemo shows how to pull raw contact constraint information out of the solver. Pretty low level since it interacts directly with the vectorized solver-stored data, but can be simple and convenient.

2) Use a INarrowPhaseCallbacks implementation to collect relevant information during the narrow phase execution. Something like the TankCallbacks of the TankDemo would do the trick. Note that the INarrowPhaseCallbacks implementation will be invoked from multiple threads, so you have to be careful about how you read/write stuff from the callbacks. You cannot change body velocities from the callbacks directly. (There's also the ContactEventsDemo, which shows the beginnings of a more heavyweight traditional events system. I'd generally recommend doing the simplest and most direct thing, and an event system usually isn't that.)
Post Reply