Skip to content
BoredEngineer edited this page May 9, 2016 · 8 revisions

Using Machinery Modeling Toolkit on your own

This guide is for using bare-bone C++ plugin, if you are interested in using MMT content examples refer to this wiki: https://github.com/BoredEngineer/MMT_Content/wiki

As of now, custom physics during sub stepping can be run only on classes inherited from MMTPawn. Just as you make you own pawn from default pawn: right click in content browser -> Blueprint Class -> then type "MMTPawn" in search field -> press "select":

Inside of you new blueprint you can find a new custom event "MMT Physics Tick": This event will fire every time when physics update is initiated, including during sub-stepping. Substep Delta Time that it provides works both during sub stepping and when it's switched off. Any physics calculations that you want to run during sub-stepping should be called from this event only.

IMPORTANT Physics calculations are running in a parallel thread and when sub-stepping is enabled you shouldn't try to call any nodes such as DrawDebugxxx, PringString and etc. as it will crash the engine. If you want to draw some debug information you can write it into an array and display during normal Tick event, or temporary disable physics sub-stepping. If you want to use custom SceneComponent to run physics calculations then make sure that it's TickGroup is set to Pre-Physics: Visual update of your components, such as animations, rotations, movement and etc. should be done in a normal Tick event, leave MMT Physics Tick for custom AddForce calls.

0lento's Three Rules: "As additional note, you can read relative values using regular BP nodes as long as the components itself don't simulate any physics (have simulate physics enabled, are constraints etc). As long as they don't simulate physics, relative values are updated only on tick, therefore they are always up-to-date and are safe to use on physics ticks too.

This all can sound pretty confusing but there really are only three specific rules when you use sub-stepped physics ticks:

  • Don't add force (including torque) using regular engine nodes (impulses are fine).
  • Don't get any world space transforms/locations/rotations using regular engine nodes for actors/components that simulate physics themselves.
  • Don't directly change any transforms/locations/rotations on physics tick (you can still store data on variables and pass it to regular Tick). Only use physics tick to interact with the physics scene using forces, velocities etc. "

StayUpRight pawn (in MMT_Content repo) has example of how to deal with relative positions. There is a custom bp function which return world space location of specific socket. What it does is takes relative component transform of socket and "multiplies" it by world space transform of component which holds this socket. Practically this moves socket from local coordinates into world coordinates during sub-steps. This is something that you have to do regardless of sub-stepping but usually it's nicely hidden under several pre-build functions such as GetSocketLocation.

Physics sub-stepping "friendly" functions Nodes such as GetWorldLocation, GetSocketTransform and etc. are not updated during physics sub-stepping, they are updated only after all sequential sub-steps are complete, so if an object is moving during sub-stepping, nodes such as GetWorldLocation will be returning the same old value, which object had before current physics update. Such nodes are necessary for custom physics calculations and this plugin provides a number of replacement functions which give you up to date information during sub-stepping:

Testing physics To test if sub-stepping is working as expected press Shift+0, game will be running with a limit of 15 Fps. Otherwise you can use this nifty console command "t.maxFPS [desired fps]". Executing "t.maxFPS 20" will force rendering at 20 fps. If sub-stepping is enabled and you have enough MaxSubSteps set, your physics driven objects should move normally as for each rendering frame physics will be updated multiple times and physics delta time, which is used in many calculations, will be more or less the same. To return your rendering to normal FPS, execute "t.maxFPS -1".

Note on packaging In 4.10 and earlier versions we can't package blueprint only project with custom plugins, you have to convert it into c++ project, so UE4 can link plugin library. This is very easy to do. You just need to install Visual Studio 2015 community edition and add an empty C++ class into your project, directly from the editor. For example, to package MMT_Content (blueprint only project) for executable, I simply do this:

  1. Clone project in launcher
  2. Open it in editor
  3. Add an empty c++ class and wait for editor to open visual studio and re-compile everything
  4. Now just package as usual
  5. Delete cloned version as it's not needed anymore
Clone this wiki locally