From BlenderWiki

Jump to: navigation, search
Blender3D FreeTip.gif
IMPORTANT! Do not update this page!
We have moved the Blender User Manual to a new location. Please do not update this page, as it will be locked soon.

Blender Physik

Blender enthält erweiterte Physiksimulationen, in Form von der Bullet Physik Engine (BulletPhysics.org). Most of your work will involve setting the right properties on the objects in your scene---dann können Sie sich zurück lehnen und die Engine übernimmt alles weitere. Die Physik Simulation kann für Spiele, aber auch für Aufnahmen von Animationen genutzt werden.

Die Blender Game Engine (BGE) basiert auf der Physik von Rigid-Body, welches sich signifikant von dem komplementären Set an Werkzeugen die in Weichkörper Physik Simulationen verwendet werden unterscheidet. Auch wenn die BGE über den Typ Weichkörper verfügt, ist es nicht annähernd so nuanciert wie nicht-BGE Weichekörper. Das Gegenteil ist mehr der Fall: es ist mit der kein-BGE Physik schwierig etwas das ähnlich zu einer steifen Form ist hinzubekommen. In Rigid Körper Physik ist, als Effekt oder Folge, keine Art von Mesh Deformationen vorhanden. Zu einer Diskussion wie dieser Effekt teilweise überwindet werden kann, können Sie sich Mesh Deformationen ansehen.

Erste Schritte

Wenn Sie noch nie mit der BGE gearbeitet haben, möchten Sie sich vielleicht 10-15 Minuten das Einleitung Tutorial auf Englisch ansehen. Anschließend folgt ein interaktives Beispiel, [Doc:2.6/Tutorials/Frijoles|Frijoles auf Englisch]].

Die Grundlagen sind:

  • Drücken Sie P während sich Ihre Maus in einer 3D Ansicht befindet um zu starten.
  • Drücken Sie Esc zum Beenden.
Zur Blender Game Engine wechseln.
  • Wechseln Sie zur "Blender Game" Engine damit Ihr Eigenschaften Editor die richtigen Optionen enthält.
Eigenschaften Knöpfe
  • Beobachten Sie die Unterschiede zwischen "Blender Game" und "Blender Render" nachdem Sie verschiedene Eigenschaften Knöpfe gedrückt haben: Render, Szene, Welt, Materialien, Partikel, und, am wichtigsten, Physik.
  • Beginnen Sie Änderungen an den Eigenschaften vorzunehmen um Ihre Simulationen zu beeinflussen.
  • Take a step deeper with Logic Bricks for total control, to include Python scripting.

Typen

Die fünf Allzweck Typen sind:

Typ Kollision Bewegung Rotation Deformation Beispiel
Keine Kollision Eine Anzeige auf dem Bildschirm.
Statisch Eine Mauer.
Dynamisch Ein Character in einem Scroller von der Seite.
Rigid Body Häufig bewegte Objekte.
Weicher Körper Eine wehende Flagge.

Zusätzliche, spezielle Typen:

  • Occluder - Verhindert Berechnungen von gerenderten Objekten (aber nicht deren Physik!).
  • Sensor- Erkennt Objekte ohne die Restitution von Kollisionen.
  • Navigation Mesh - Zum Erstellen von Pfaden zur Wegfindung. Nützlich für Künstliche Intelligenz.

Welt Optionen

The global Physics Engine settings can be found in the World Properties, which include the Gravity constant and some important engine performance tweaks.

Beschränkungen

It is imperative to understand that the Blender Constraints generally don't work inside the BGE. This means interesting effects such as Copy Rotation are unavailable directly.

Your options include:

  • Parenting - But not Vertex Parenting.
  • Rigid Body Joint - This is the one Constraint that you can set up through the UI that works in the BGE. It has several options, and can be very powerful - see ITS page for a detailed description and demo .blend. Don't forget that you can loop through objects using bpy instead of clicking thousands of times to set up chains of these Constraints.
  • Rigid Body Joints on the Fly - You can add/remove them after the BGE starts by using bge.constraints.createConstraint(). This can be good either to simply automate their setup, or to truly make them dynamic. A simple demo can be viewed in: BGE-Physics-DynamicallyCreateConstraint.blend‎
  • Python Controllers - As always, in the BGE, you can get the most power when you drop into Python and start toying with the settings directly. For instance, the Copy Rotation mentioned above is not hard -- All you have to do is something to the effect of own.worldOrientation = bge.logic.getCurrentScene().objects['TheTargetObject'].worldOrientation

Physik Visualisieren

Manual-BGE-Physics-Visualization.png

Go to Game » Show Physics Visualization to show lines representing various attributes of the Bullet representation of your objects. Note that these might be easier to see when you turn on Wireframe Mode (Z) before you press P. Also note that you can see how the Bullet triangulation is working (it busts all your Quads to Tris at run-time, but the BGE meshes are still quads at run-time).

  • RGB/XYZ Widget - Representing the object's Local Orientation and Origin.
  • Green - "sleeping meshes" that are not moving, saving calculations until an external event "wakes" it.
  • White - White lines represent active bounding meshes at are undergoing physics calulations, untill such calculations are so small that the object is put to rest. This is how you can see the effects of the Collision Bounds.
    • Thick, or Many White Lines - A compound collision mesh/meshes.
  • Violet - Bounding meshes for Soft bodies.
  • Red - The Bounding Box, the outer boundary of object. It is always aligned with global X Y and Z, and is used to optimize calculations. Also represents meshes that have been forced into "no sleep" status.
  • Yellow - Normals.
  • Black - When in wireframe, this is your mesh's visual appearance.

If you want finer-grained control over the display options, you can add this as a Python Controller and uncomment whichever pieces you want to see:

import bge
debugs = (
#bge.constraints.DBG_DRAWWIREFRAME, # Draw wireframe in debug.
bge.constraints.DBG_DRAWAABB, # Draw Axis Aligned Bounding Box in debug.
#bge.constraints.DBG_DRAWFREATURESTEXT, # Draw freatures text in debug.
#bge.constraints.DBG_DRAWCONTACTPOINTS, # Draw contact points in debug.
#bge.constraints.DBG_NOHELPTEXT, # Debug without help text.
#bge.constraints.DBG_DRAWTEXT, # Draw text in debug.
#bge.constraints.DBG_PROFILETIMINGS, # Draw profile timings in debug.
#bge.constraints.DBG_ENABLESATCOMPARISION, # Enable sat comparision in debug.
#bge.constraints.DBG_DISABLEBULLETLCP, # Disable Bullet LCP.
#bge.constraints.DBG_ENABLECCD, # Enable Continous Colision Detection in debug.
#bge.constraints.DBG_DRAWCONSTRAINTS, # Draw constraints in debug.
#bge.constraints.DBG_DRAWCONSTRAINTLIMITS, # Draw constraint limits in debug.
#bge.constraints.DBG_FASTWIREFRAME, # Draw a fast wireframe in debug.
#bge.constraints.POINTTOPOINT_CONSTRAINT,
#bge.constraints.LINEHINGE_CONSTRAINT,
#bge.constraints.ANGULAR_CONSTRAINT,
#bge.constraints.CONETWIST_CONSTRAINT,
#bge.constraints.VEHICLE_CONSTRAINT,
)
for d in debugs:
  bge.constraints.setDebugMode(d)

Show Framerate and Profile

A shot of Manual-BGE-Physics-DancingSticks.blend‎ with Game » Show Framerate and Profile enabled

If you enable Game » Show Framerate and Profile, it will put some statistics in the upper-left area of the game window.

These can be very informative, but also a bit cryptic. Moguri has elaborated on their meanings, for us: http://mogurijin.wordpress.com/2012/01/03/bge-profile-stats-and-what-they-mean/


Nach Schlüsselbildern aufnehmen

Abgesehen vom Spielen, möchten Sie manchmal eine komplexe Szene Rendern die Kollisionen, mehrere Kräfte, Friktion zwischen mehreren Körpern, und Air Drag beeinhaltet---aber Sie haben nicht vor all diese einzeln zu animeren. Zum Glück, können Sie auf die Blender Game Engine zählen damit sie es für Sie erledigt.

Menü zum Aufnehmen von Schlüsselbildern in das Dopesheet.

All you have to do to achieve this effect is go to the Info Editor (the bar at the top of the window) Game » Record Animation, and it will lock away your keyframes for use in Blender Render mode. You can go back to the 3D view and hit AltA to play it back, or CtrlF12 to render it out as an animation.

Note that, through use of Game Logic Bricks, you can interact with the Game Engine as it is playing. That means you can record a part-simulated part-user-controlled animation.

Schlüsselbilder aufräumen

The mess that "Record Animation" leaves behind.

You will find that Record Animation machine-guns keyframes all over the place. It will probably be a keyframe per channel per frame, wall-to-wall. Some of this data is redundant due to unchanging channels.

After hitting O.

Simply press O while in the DopeSheet and it will remove all keyframes that do not represent a value change compared to the one to its left. Though this will not suddenly make your data dead simple, it will at least give you some clues about where the action/inaction areas are in the timeline.

Don't forget that the animation does not have to play starting from Frame 1. The keyframes will insert beginning at your current Frame position, or else you can use the NLA Editor and turn these things into Action Strips.


In .bullet Datei aufnehmen

You can snapshot the physics world at any time with the following code:

import bge
bge.constraints.exportBulletFile("test.bullet")

This will allow importing into other Bullet-based projects. See the Bullet Wiki on Serialization for more.

Mesh Deformationen

As mentioned above, Rigid Body physics do not affect mesh deformations, nor do they account for them in the physics model. This leaves you with a few options:

weichkörper

You can try using a Soft Body, but these are fairly hard to configure well.

Aktionen

To use an Action Actuator to do the deformation, you have to make a choice. If you use Shapekeys in the Action, you will be fine as far as the overall collisions (but see below for the note on reinstancePhysicsMesh()). The mesh itself is both a display and a physics mesh, so there is not much to configure.

To use an Armature as the deformer will require a bit of extra thought and effort. Basically the Armature will only deform a mesh if the Armature is the parent of that mesh. But at that point, your mesh will lose its physics responsivenes, and only hang in the air (it's copying the location/rotation of the Armature). To somewhat fix this you can then parent the Armature to a collision mesh (perhaps a simple box or otherwise very-low-poly mesh). This "Deformation Mesh" will be the physics representative, being type: Dynamic or Rigid Body, but it will be set to Invisible. Then "Display Mesh" will be the opposite set to type: No Collision, but visible. This still leaves us with the problem mentioned in the previous paragraph.

When you deform a display mesh, it does not update the corresponding physics mesh. You can view this evidently when you Enable Physics Visualization - the collision bounds will remain exactly as when they began. To fix this, you must call own.reinstancePhysicsMesh() in some form. Currently this only works on Triangle Mesh bounds, not Convex Hull. We have prepared a demonstration file in Manual-BGE-Physics-DancingSticks.blend‎. Note that we had to increase the World » Physics » Physics Steps » Substeps to make the collisions work well. The more basic case is the case the Shapekeyed Action, which you can see in the back area of the scene. Since it is the only object involved, you can call reinstancePhysicsMesh() unadorned, and it will do the right thing.

The more complicated case is the Collision Mesh » Armature » Display Mesh cluster, which you can see in the front of the scene. What it does in the .blend is call reinstancePhysicsMesh(viz), that is, passing in a reference to the visual mesh. If we tried to establish this relationship without the use of Python, we would find that Blender's dependency check system would reject it as a cyclic setup. This is an example of where Blender's checking is too coarsely-grained, as this circle is perfectly valid: the grandparent object (the Collision Mesh) controls the location/rotation, while the middle object (the Armature) receives the animated Action, where the child (the Display Mesh) receives the deformation, and passes that on up to the top, harmlessly. Something to note is that the Collision Mesh is merely a plane -- that is all it requires for this, since it will be getting the mesh data from viz.

Ragdolls

A third option is to create your items out of many sub-objects, connected together with Rigid Body Joints or similar. This can be quite a bit more work, but the results can be much more like a realistic response to collisions. For an Addon that can help you out in the process, check out the Blender Ragdoll Implementation Kit.

Tieferes Wissen

Sometimes you will want to look at:

Und dann existiert immer die Option:

Blender und Bullet Quellcode Dateien lesen

Das könnte einschüchtern klingen, sogar wenn Sie C/C++ kennen, aber es kann sehr informativ sein. Sie können sehen wie Blender Objekte einrichtet um Sie Bullet zu übergeben, und an einigen Stellen printf() Anweisen an hinzufügen, oder mit svn revert experimentieren um zur Normalität zurückzukehren.

Here is an example of the trail to get to the bottom of the handling of the options. We will observe the handling of the use_shape_match property, as an example.

    prop = RNA_def_property(srna, "use_shape_match", PROP_BOOLEAN, PROP_NONE);
    RNA_def_property_boolean_sdna(prop, NULL, "flag", OB_BSB_SHAPE_MATCHING);
    RNA_def_property_ui_text(prop, "Shape Match", "Enable soft body shape matching goal");
    if (m_cci.m_gamesoftFlag & CCD_BSB_SHAPE_MATCHING)//OB_SB_GOAL)
    {
        psb->setPose(false,true);//
    } else
    {
        psb->setPose(true,false);
    }
  • Here is the first bit of logic. It inverts the arguments to setPose depending on the value. Now then, since psb is of type btSoftBody, we have officially launched into Bullet territory. You have a couple options:
    • If you go to the Bullet API Navigator and expand the Class List menu, you can CtrlF for the btSoftBody class, and follow the link to the btSoftBody Class Reference Page. There you will see very sparse written documentation, but it will, at least, link you to a syntax-highlighted line where the method is implemented.
    • Get the Bullet Source with: svn checkout http://bullet.googlecode.com/svn/trunk/ bullet-read-only and probably run something like ctags -r . from that tree every now and then to build the tags file. Now you can dig further. Something like vim -t setPose will lead you to the implementation in src/BulletSoftBody/btSoftBody.cpp (which is the same code as can be found through the Bullet API Navigator in the previous step).
  • Through either approach, we find that the mysterious bools above are for btSoftBody::setPose(bool bvolume,bool bframe), which are immediately assigned to m_pose.m_bvolume and m_pose.m_bframe, respectively.
    • Subsequently searching for m_bvolume doesn't show much use in this file, other than the assignment and initialization. We could follow the trail deeper to the btSoftBody::Pose Struct Reference docs, but for now let's try:
    • Searching for m_pose.m_bframe. At this point, in this file, we have finally found the end of the simple passing of the flags, and we will see major chunks of code that are branched depending on this setting.
  • Whether we can learn anything apparent at this point will depend on our ability to understand the code and concepts within the Bullet implementation. Perhaps we followed a multi-step process to find inscrutability, but at least we can see the very lines executed within the BGE.
    • Now we have some symbols to search for in Google or in the Bullet Forums.
    • If we wanted to instrument this code with debugging printf()s, we could compile it and link it into our Blender build.