# physics in panda

as some of you may have noticed from my previous posts, i am trying to make a submarine game underwater. in order to simulate realistic underwater motion the sub should for example slow down when the accelerator is released, and not stop instantly. as it is now in the code there are just a bunch of accelerate and decelerate functions and i alternate between these using tasks to get the desired effect. however i’m wondering if it is possible to do such a thing with the physics system that is built into panda instead. my understanding from reading the manual is that it should be possible to just make the sub into a physics node and then apply forces to it so when accelerating you apply a linear force to it etc but i’m not sure abt what to do abt the slowing down described above or if the sub is turning (since the turning arc should be relative to the speed) and how this would all be made into forces…does anyone have any ideas?

there are also the factors of viscosity and whether i can take this into account without “guessing” it’s affects by just playing around with the acceleration factors. i’ve noticed that you can take the mass of the object into account with the physics system so maybe this would be useful…?

hey again, i’ve tried a few things but no luck. hoping someone can correct me

``````		subAN = ActorNode("sub_physics")
subANP = self.submarin.attachNewNode(subAN)
base.physicsMgr.attachPhysicalNode(subAN)
subAN.getPhysicsObject().setMass(1000)

gravityFN = ForceNode('grav_force')
gravityFNP = render.attachNewNode(gravityFN)
gravityForce = LinearVectorForce(0,0,-9.8)

tempFN = ForceNode('testFN')
lvf = LinearVectorForce(10,0,0)

basically i’ve been playing around with the code above in the submarine class but can’t get any forces to work on the sub…does anyone know what’s wrong
[/code]

Hey…
I don’t think that using the physics engine for this is such a good idea.
It is probably easier to do what I’ve done in my First Person Space Shooter.

And as I said in response to your last post (when you’ve had problems with your torpedo) you should take a look at the code of others.
For example, the movement underwater and in space are the same.
Both are based on full 3D movement. The only difference between underwater and space is that underwater everything might be a bit slower and you have a ground.

So you can take my code as reference for your game
https://discourse.panda3d.org/viewtopic.php?t=2131&highlight=

Now to your specific problem. It can be solved in a task.
You need two variables, isSpeed and setSpeed

With whatever key you’ve defined to increase/decrease speed you’ll alter the setSpeed variable in a Task.
Then you change the isSpeed variable according to the setSpeed variable.
For the task you’ll need a variable to store the current time, let’s call it speedTimer…
The task might then look something like this:

``````def speedTask(self, task):

if (timer >= 0.05):
if (self.speed < self.setSpeed):
self.speed = self.speed + 0.1
elif (self.speed > self.setSpeed):
self.speed = self.speed - 0.1
cutoff = self.setSpeed - self.speed
cutoff = math.sqrt(cutoff*cutoff)
if (cutoff < 0.1):
self.speed=self.setSpeed
percent = (self.speed*100)/self.maxSpeed

Speed = 'Speed: '+str(math.floor(percent))+'%'
self.speedHUD['text'] = Speed

The code is simple.
At the beginning you get the current elapsed time of the task with task.time.
Substracting speedTimer from task.time will give you the elapsed time in seconds after the last time the task has been executed.
If the code in the task has been executed at 2.5s after the task has been started, the next execution will be at 3.0s in this example. So every 0.5s the task will increase the current speed (or decrease) by 0.1 …

To make the turning speed dependent on the current speed you could write something like this:

``turningSeed = isSpeed/2``

Legion, I’m not sure I like that approach, but I’m a total Panda3D n00b, so I’m struggling w/ the same thing that tokingmonkey is. While that would work as a cheap n sleazy fix, I think you should be able to use known physics principles like vector forces–thrust, friction, etc–to simulate things.

I’m working on a little game myself, and physics is my sore spot. If I can get forces & gravity to work, my dev time will skyrocket…into the ground… yeah, you know what I’m sayin…

…what I don’t understand is, if I add a linear vector force to my “world”, e.g. gravity, as a PhysicsNode or whatever at the root/render level–and if “base.enableParticles()” enables tasks that handle physics, why is it up to the programmer to update it every frame? Or am I missing something?

Ok, so I read a different thread, and I figured out how to get gravity working. And it’s ugly. I’m too close for missiles… switching to guns

er, that is, I’m gonna use PyODE. It’s a bit more confusing at the outset, but it has better documentation, and it was truly designed for physics simulations. It’s more robust.