Any pointers on how to use the Physics System?

Hola!

I was wondering if anyone had any code, or basic pointers on what it takes to apply physics to objects. I’ve tried to do what I could with the bit in the manual, but with minimal success.

Any information would be most helpful. Thanks!
-Nick

Quick followup after playing some more. Here is what I have so far, based on the manual:

force = LinearVectorForce( 0, -1, 0 )
phys = actorNode.getPhysical(0)
phys.addPhysicsObject( PhysicsObject() )
phys.addLinearForce( force )

Which unfortunately doens’t seem to work/doesn’t cause any motion. And for more information, actorNode is created and used like:

actorNode = ActorNode( "name" )
actorNodePath = NodePath( actorNode )

I was wondering if anyone might be able to point me in the right direction for making this work?

Thanks for your time,
-Nick

Sure, here’s a quick-and-dirty example:

from direct.directbase.DirectStart import *
from pandac.PandaModules import *

def moveSmiley():
    # The physical object, that is, the object which is acted upon by
    # the physics system, needs an ActorNode to define the coordinate
    # system in which it is moving, and to manifest the object's
    # actions.  Don't confuse this with Panda's Actor class, which is
    # an unrelated object used for playing back animations.
    an = ActorNode('smiley')
    anp = render.attachNewNode(an)

    # A model is parented to the ActorNode just so we can see what
    # it's doing.
    smiley = loader.loadModel('smiley.egg')
    smiley.reparentTo(anp)

    # We need to attach it to a PhysicsManager to do the actual work
    # of moving it around.  We use the default PhysicsManager created
    # by ShowBase, but we could create our own if we wanted to.
    base.physicsMgr.attachPhysicalNode(an)

    # This method must be called to enable the task that computes the
    # PhysicsManager every frame.  (base.physicsMgr is the same
    # PhysicsManager that controls particle effects.)
    base.enableParticles()

    # Now apply a force to the ActorNode.  The Force needs a
    # ForceNode, to define the coordinate system in which it is
    # applied.
    fn = ForceNode('pull')
    fnp = render.attachNewNode(fn)
    pull = LinearVectorForce(0, -1, 0)
    fn.addForce(pull)

    # Here we make it a local force that affects only this one
    # ActorNode.  We could make it a global force instead by adding it
    # to base.physicsMgr.
    an.getPhysical(0).addLinearForce(pull)

David

Wow! Thanks for the great reply. That is exactly what I was looking for! Great comments! I do have a couple of hopefully simple followup questions:

  1. I’ve got a force acting on the body, how can I get/set the Velocity of the body? (Seems like I need a ‘PhyscialObject’, but I can’t figure out how to get one for the body)

  2. I’m a bit confused by the last 2 sections of the code. Seems like the second to last section (fn = FoceNode… through fn.addForce) is creating this force node, but then it isn’t used anywhere… ie, the actual force applied directly to the actor via ‘addLinearForce( pull )’ is just the LinearVectorForce (As opposed to the force node you just created). So what does the creation of the forceNode do?

Thanks again for your time, :slight_smile:
-Nick

  1. You need the PhysicsObject, which you can get via an.getPhysicsObject(). Then you can call getVelocity() and setVelocity() on the PhysicsObject.

  2. Right, the ForceNode is not used directly. It does have a purpose, however, which is to establish the coordinate system in which the force vector exists. The force remembers which ForceNode it has been added to, and looks up that ForceNode in the scene graph when it is time to compute the effective direction of the force.

If you were to take the ForceNode and reparent it to a rotating body (or just rotate the ForceNode directly, e.g. with fnp.setHpr(90, 0, 0)), that would in turn rotate the force.

David

  1. Heh… Well, that was simple enough! = )

  2. Ahh. OK. I gotcha. So if you reparented the ForceNode to the body you were effecting, you could say, make sure the ‘car’ always moved forward, even though the force would always be X directed.

Thanks David! Your help is greatly appreciated!
-Nick

Here’s another argument against the fixed-width layout of the website…

Almost all of the comments in the code David posted get line-wrapped, making the code much more difficult to read. Normally I could just make my browser window larger to get rid of the wrapping. Alas, all I get for making my browser wider is a bunch of empty space on the right…

Anyway, I still love the new design. Just bear with my nit-pickiness.

Hello, I’m new to panda3d, my current goal is to have an object move through the 3d space like a ship (inputs are pitch, roll, and thrust). I’m trying to use the physics class, but I’m having trouble getting it to work.

drwr’s code works for the linear movement, but when I try to rotate the object (angular force) it doesn’t seem to rotate.

from direct.directbase.DirectStart import *
from pandac.PandaModules import *

def moveSmiley():
    an = ActorNode('smiley')
    anp = render.attachNewNode(an)

    smiley = loader.loadModel('smiley.egg')
    smiley.reparentTo(anp)

    base.physicsMgr.attachPhysicalNode(an)
    base.enableParticles()

    fn = ForceNode('spin')
    fnp = render.attachNewNode(fn)
    spin = AngularVectorForce(0, -1, 0)
    fn.addForce(pull)

    an.getPhysical(0).addAngularForce(spin)

Any suggestions? I think it’s most likely that the force is being applied, but isn’t connected to the base physics handler or something. The manual is very lean on the physics stuff…

Is there anyone out there that is interested in writing any documentation or tutorials on the Physics module. It seems very capable, but apparently only a couple of people even knows what it does.

I can follow directions as good as the next programmer, but I cannot just make up methods or guess as to the function of something that is obscurely named iin some distant API.

Im afraid…

setVelocity
void PhysicsObject::set_velocity(LVector3f const &vel);

Description : Vector velocity assignment
Description : Piecewise velocity assignment

(from the API) really tells me just about nothing. Im sure I speak for a lot of people out here that we would “run with the ball” … If we had a ball to run with.

Is Disney purposefully holding the documentation back? They had to have developed it if there were a lot of programmers developing Toontown. Do they consider it a threat? Why give away the engine and not the documentation to really use it?

I, for one, really want to give Panda a fighting chance, and learning Python is fun, however the learning curve with sketchy documentation makes it that much harder and a lot of good programmers leave.

I almost left the other day because of the docs. I thought a feature wasnt supported and Ogre supports it with only a couple of function calls. I later found out that its IN Panda, but its not documented… Over time, I will have many valuable things to contribute. At the moment I feel like a bottom feeder only because I have to bug people and wait days between answers instead of just looking it up in the Reference Guide and Tutorial guide.

When Borland stopped supporting Turbo Pascal and starting doing C++ (before Delphi), they were so apologetic to their Pascal programmers they literally sent them a YARD of reference books on doing things with Turbo C. There had to be 9 thick books in that collection. Now, obviously, in the days of PDFs, a yard of books isnt necessary

The bottom line: if we had the information, we would help contribute more and projects would move a lot faster and the questions would not seem so basic.
[/code]

The physics system in Panda was originally developed by a summer intern at the Disney VR Studio, for the purpose of controlling particle effects. It worked quite well for what it did, but as an intern project it was never terribly well documented. It was later extended and enhanced by various full-time engineers over the years to add a bit more functionality, but as it happens, no one had the time to go back and add more documentation.

This is one of the sad realities of life: engineers are paid to develop code, not documentation. Although well-documented code almost always pays for itself in the long run, businesses (including the VR Studio) tend to be more focused on the immediate needs of the short run. So sometimes code gets written in a hurry with minimal documentation; and it’s difficult to take time away from the demands of our current projects to go back and add documentation to something that already works.

We do the best we can. In fact, I think that Panda does pretty well for itself, compared to many other professional projects of similar magnitude. There are still large areas of the code that aren’t documented well, or at all; but on the other hand there are vast areas that are really quite well documented internally. And we have this splendid online manual, which grows more and more detailed every month.

(I say Panda does well compared to other professional projects. I’m not claiming that it’s as well documented as other volunteer projects. Projects that were designed from the ground up as volunteer open-source projects tend to be better documented from the beginning, because they don’t have the same market pressures on them to meet deadlines, and because good documentation is much more important when your developers are distributed around the world, rather than all collected in one room together. Since Panda started life as a closed-source project to meet the needs of Disney, and indeed still is developed primarily by Disney, it tends to be less well documented than some other open-source projects.)

Any user contributions to the documentation are, of course, welcomed and encouraged. Contributions of this nature will make the project better for everyone.

For figuring out systems that are not as well documented, it may be helpful to download the C++ source code and read that directly. The comments in the C++ code are the source of the generated API docs, but I find that the C++ code is a bit easier to follow than the generated API documentation, because it tends to be organized a bit more by functionality (rather than strictly alphabetically, for instance), and because occasionally comments that have been placed in unusual places in the code are inadvertently omitted from the generated docs.

And, of course, you can always read the code itself. Sometimes that’s the best description of what a particular function is supposed to do. :slight_smile:

David

I completely understand, and I would LOVE to contribute. If I knew the system, I most certainly would.

As I learn the system, by and large, I will contribute as I have useful things to add. Right now, Im in total absorption mode. It’s daunting to learn something without documentation.

I really need to understand the physics and gravity walkers for our our project and what the different physics methods contribute.

is there any way we can setup more of a “documentation project” so that everyone can contribute?

At one time you had a wiki. what happened to it? Is it a project that can be revitalized?

Hi,

The Panda3d manual can be modified by community members.

Make sure you are logged in and then click the Documentation > Manual link.

In the lower left corner of the screen you’ll find a small period. If you click it, you can then edit the manual.

Steve

Is there some specific question you have that I might be able to answer about the physics system?

Also, I’m a little curious to know what that feature is you alluded to that you thought Panda lacked, until you discovered it was just undocumented. :slight_smile:

David

David (et. al),

There is currently and entry in the manual under section (W) that appears to document the Physics engine. How accurate is that entry in relation to what we’ve been discussing? It seems to contradict the example outlined above.

I’m going to try and update the manual with the elements discussed here, but I’m wondering if I should clobber that entry or start a new one?

Take care,
Mark

The stuff there now sure seems light. What information is there is accurate enough, I guess, although it confuses the ActorNode class with the Actor class (completely unrelated classes with similar names).

If you wanted to tackle documenting the physics module properly, you wouldn’t do badly by scrapping that page completely and starting over. :slight_smile:

David

Okay, new chapter started :slight_smile:

It’s a bit sparse because I’m very new at the physics system. I’m going to try to find time to attempt to use the rest of these force types so that I gain more insight into them, but if anyone has more insight that they would like to add, by all means jump in!

Take care,
Mark

That’s great! What a welcome addition to the manual. Thanks!

David

THANK YOU!
THANK YOU!
THANK YOU!

I know what I’ll be adding tonight!!!

From the “Applying Physics to a Node” section of the book…

actorNode.addLinearForce(pusherForce)
actorNode.addAngularForce(spinnerForce)

When I run a routine …

self.jumpFN=ForceNode("jumpFN")
self.jumpFNP=self.jump.attachNewNode(self.jumpFN)
self.jumpForce=LinearVectorForce(0,0,10)
self.idleForce=LinearVectorForce(0,0,0)
self.jumpForce.setMassDependent(1)
self.jumpFN.addLinearForce(self.idleForce)

I get an error message…

If I substitute

self.jumpFN.addForce(self.idleForce)

for

self.jumpFN.addLinearForce(self.idleForce)

that part of the code doesnt error out.

Is the documentation wrong?

@David

In your example above, you have…

  fn.addForce(pull)

    # Here we make it a local force that affects only this one
    # ActorNode.  We could make it a global force instead by adding it
    # to base.physicsMgr.
    an.getPhysical(0).addLinearForce(pull) 

near the bottom. This appears to be doing the very same thing, using two different versions of the same “addForce”?