[Last question] obj.pid, obj.ForceLimitX, obj.reference : can we have it?

Is it possible?

If I use

own.setLinearVelocity([0.0,speed,0.0], 1)

my object’s speed changes suddenly from 0 to “speed”.

I would like to do something like this (not working with objects) :


<i>obj</i>.forceLimitY = [ForceLimitYMin, ForceLimitYMax, True] 
<i>obj</i>.forceLimitZ = [ForceLimitZMin, ForceLimitZMax, True]

And use servo motion advantages (acceleration control).
(linVelocityMax doesn’t seem to be related)

Does anybody know anything about it?
Thank you.

The motion methods of KX_GameObject are just the ones labeled as “simple” in the motion actuator.

Hi Moguri,
that’s what I could understand by reading the documentation.
I’m not a programmer but if I understand well, logick bricks are just pack of instructions that are translated when the game engine is started.

We can create servo motion logic bricks (actuator) and even change the settings (LimitYMax, etc…) with Python.
Is that really impossible to access to servo motion directly or to have an overview of the “translated code” for servo motions to recreate it (that would be a waste of time, though) without actuator?

Well… servo is just advanced control of force movement. You start with a min force and increase it until you get to your desired velocity, then you stick with a force that results in no acceleration but just maintains the velocity of the object.

Simple forces should be easy to do with pure Python:


import GameLogic as g

cont = g.getCurrentController()
own = cont.owner

#A keyboard sensor called "keyup" must be attached
key = cont.sensors['keyUp']

#I just do X force here... other can easily be added
forceX = 0

if key.positive:
    #this is equivalent to setting the force to 10
    forceX = 10

#force = mass * acceleration, so acceleration is force / mass.  
#We divide by 60 because there are 60 logic tics a second (by default).  
accelX = forceX / own.mass / 60

#Now just apply the new velocity by adding the acceleration
vel = own.getLinearVelocity()
vel[0] += accelX
own.setLinearVelocity(vel)

Now see the attached demo file for it in action. The bottom cube just uses a motion actuator, the top one uses the script. They move identically.

Then you just have to tweak the script to get servo-like results.

-Sam

EDIT: I might add that this is going to ignore friction while the script is running. Because we are setting the linear velocity directly, it’s going to override friction. Friction will continue to work when the script is no longer running though…

Maybe it would be better to use applyImpulse?

Attachments

pythonForce.blend (35.8 KB)

Hi cray,
I haven’t even figured out how to use the servo logic brick yet. But with a lot of trial and error I’ve used scripts to create nice smooth movements of objects and cameras. It would be great if blender, or maybe a custom script, was able to print the python equivalent of whatever is wired as bricks. Sure there’s lots of great tutorials but it would be a really good learning tool.

Hi,

TheSambassador
That’s working great, thank you.
I was thinking about doing something like this (except the fact that I’m not a coder) but I felt a bit it was like reinventing the wheel.
In my dreams, I was hoping that I could use Python on the object like we do with the actuators :


act.setLinearVelocity() =&gt; obj.setLinearVelocity()
act.pid =&gt; obj.pid
act.ForceLimitX =&gt; obj.ForceLimitX
act.reference =&gt; obj.reference

That would be useful. I don’t know much about Blender Api, but I don’t see why it wouldn’t be possible to implement?
A lot of actuators settings are already available through Python.
I would be happy to have Blender’s developers point of view about it, I would surely learn something new.

Equip

It would be great if blender, or maybe a custom script, was able to print the python equivalent of whatever is wired as bricks.

I do agree. These days I’m also trying to learn the Python alternatives to all logic bricks.

It sounds like you’re wanting the game engine to be completely scriptable (without any logicbricks).

That issue has been discussed before.

However… in theory one could write a bunch of modules/classes/functions that would allow for almost complete logicbrick independence. Some sensors/actuators would be harder than others to recreate in python, but my knowledge of BGE python is far from complete, so maybe some of these would be easy. In theory, all you’d need to do is have a script on an object and an “Always” sensor attached to it.

Sensors that probably couldn’t be recreated (easily):
Keyboard
Mouse
Touch/Collision

Keyboard and mouse could be done, but you’d have to use some external scripts to get which keys were pressed.

Actuators that probably couldn’t be recreated (easily):
IPO
Action
Shape Action

I say mainly the animation ones because I don’t think that there’s a way to access and play animations directly through python.

Because we have methods for setting Velocity, I’d say that Motion actuators would be the easiest to recreate.

If I have time today maybe I could write some functions that would allow this… but it’d almost become its own module (we could call it PyControl or something).

-Sam

Hi TheSambassador,
I’m a newbie to BGE and this board of course so I’ll say hi now to you and cray and the guys who are here.
I guess like most beginners in BGE I become anxious as logic brick wiring becomes complex. And then you discover that you can display and wire bricks between multiple objects and it becomes terrifying. And you can progress too quickly without even having sufficient understanding of sensor pulses amongst many other things, uses of layers and scenes etc.
So then you discover scripting and so it goes on… hopefully.
I think I understand that asking blender to print the python eqivalent of anything but the most basic brick setup might be not just difficult but could be logically impossible.
If anyone’s read this far I’ve got a couple of quick questions:

  1. Without using the timer property can I get the time from/ with python? Do I need to import a module and what methods/functions (I don’t know the difference) are there, time, second, microsecond?
  2. Are there statements such as “Exit” and “GoTo” that I can use?

Thanks guys, see ya.

Hi Sam,
it’s not that I want the game engine to be completely scriptable.
I don’t have anything against sensors and controllers, I just want to use the tools that can help me to concentrate on making a game.

I am in the process of learning Python (a bit more advanced than what I used to do before) and sometimes things are confusing, I have the feeling that the functions available just need to be extended a little bit (instead of a whole recreation).
ex : few months ago, I had to use a Message actuator and change its settings with Python to send several messages to several objects.
At that time, I (and others) could just find workarounds.
Now, we can use a one line sendMessage() : it was just waiting to be done.

And I have the feeling that things could be the same with servo motions (for example).

Because we have methods for setting Velocity, I’d say that Motion actuators would be the easiest to recreate.
That’s what I thought. But instead of recreating it, I was thinking that everything is already available but it just hasn’t been done yet.
Do we need to create modules for that?
Should it be temporary solutions only?
Would it be better if suggested this to be added to the Game engine?


obj.setLinearVelocity()
obj.pid
obj.ForceLimitX
obj.reference 

The script you provided previously allows a good control : very nice workaround!

Equip
Welcome here.
It would be better if you could create a new thread for your questions.
You would then get a better help and keep the focus on my own questions in that thread.

It’s probably a better idea to start a new thread for this…

However:

  1. I’m sure that you could use python’s Time module. You’d have to save the “start time” of the game (or wherever you want your timer property to start) and use that to calculate the current time. Here’s some information on the time module.

  2. I’m not sure exactly what you mean by “Exit” and “GoTo,” but I’ll take a guess. By “Exit” you either mean exit the script, loop, or game. The “break” command will bump you out of a for or while loop. In terms of exiting the game… I’m not sure, look around on the Blender 2.49 python docs, but there might not be anything. By Goto… do you mean jump to a line in the script? Gotos usually aren’t “good programming” but there is a goto module that might help.

-Sam

Thanks guys, but I’ll thank you in this thread.
Yeah sam,
import time
now = time.time() will do it and break is all I need. Thanks.

see ya

I took a look at the code, and it definitely looks doable. When I get some other things cleared off my plate, I’ll see what I can do about adding servo control to KX_GameObject.

Great news, Moguri.
I was surprised that nobody had the need to use it before.
That would be great to read progress about it, when you will have the opportunity to work on it.
Where can I get informations so that I can use it as soon as it’s available?

Thank you again for your interest in this thread.

You can always check the svn logs for when things are added, or subscribe to the bf-blender-cvs mailing list (I think this is the right one) to get the log updates emailed to you.

I might work on this when I’m done with the velocity sensor I’m currently working on.

IMO, please don’t, that kind of things would be better implemented using python subclassing in 2.5, i don’t really see the need to bloat KX_GameObject with a servo controller.

btw, so you see i’m not just complaining, here is a pure python implementation of a fully functional pid servo class http://www.pasteall.org/blend/413 documentation is not in the deal, so you’ll have to figure it out yourself :wink: run with 2.49a

cyborg_ar
No need to say that you are “not just complaining”. We are discussing :slight_smile:
I don’t know how those things are discussed between Blender developers, for the moment I know that this addition would be very helpful.

So if I understand well, it will be “soon” (if Moguri works on it) or “from 2.5 release” (if the team decides that it’s better to wait 2.5)?

i don’t really see the need to bloat KX_GameObject with a servo controller.

I understand well that it will be better to do it with python subclassing.
But would it be “bloating” adding the servo controller to KX_GameObject?
There are already so many other settings that we can access (setLinearVelocity(), .dRot etc etc), that would just be logical if we could do it also.
About being patient, I know that it’s often better if we wait for the next release.
But is this addition such a big pain? :slight_smile:

Thanks for the .blend. This is something that we can use, but still a bit harder (to use and set) than obj.pid and obj.reference : I think that we “artists” need something easier to use.
(Even though I find the script not hard to understand, not hard to use, and I appreciate you submitted it).

Moguri
Thanks for the informations.
About my request, I guess we will have to wait that you and cyborg_ar decide what to do.
If you could keep us updated here when you take that decision, that would be great.

It’s NOT hard to do the way you suggest, but the code doesn’t work like that, the servo controller is extra code that forms part of the Servo actuator class (KX_ObjectActuator), setLinearVelocity() is a call to KX_IPhysicsController::SetLinearVelocity() and every KX_GameObject already include an KX_IPhysicsController as the memeber m_pPhysicsController1, so adding the function was just about exposing the call. To make the servo be part of KX_GameObject would mean to relocate or… eww… copypaste the code to KX_GameObject or KX_IPhysicsController, and the interface you suggest is confusing and would allow only ONE servo per object. for thins kind of aditional stuff i’d prefer a “plug-in” architecture, something logic bricks do reasonably well, or using a class like the one i implemented in python. Or maybe just remove the servo option from the motion actuator and make it a physics constraint implemented in bullet.

Thanks for those explanations, that was interesting and I understand the situation better.
I will use the class you submitted (and study TheSambassador’s one also) until we see something new and decisions are taken about it.

Last thing, can I ask how to set your class?

PIDServo(own, [0,0,1], True, [0, 0, 50])

own - ok
[0,0,1] - axis?
True - Local or not?
[30,0,0.5] - ? LimitZ max? min?
Where do you set linV, limitX, etc?

or

own - ok
[0,0,1] - axis?
True - Local or not?
[30,0,0.5] - Linv=30 LimitZMin=0 LimitZMax=0.5?

just a liiiitle more help please :slight_smile:

ok, documentation

CLASS PIDServo

Constructor PIDServo(owner, target, local, pid, min_=[None,None,None], max_=[None,None,None])

Parameters:

  • owner (KX_GameObject): Object the servo class will be moving.
  • target ([X, Y, Z] List): Target speed for the servo
  • local (Boolean): Use local coordinates
  • pid ([P, I, D] List): Gains for the Proportional Integral and Derivative components
  • min_ ([X, Y, Z] List): Minimum bound for the action force, set a certain axis to None to turn it off (eg: [None, None, 1.54] would only limit the Z axis)
  • max_ ([X, Y, Z] List): Maximum bound for the action force, set a certain axis to None to turn it off (eg: [None, None, 1.54] would only limit the Z axis)

PS: attached a fixed version, the previous one had some bugs

Attachments

servo.blend (135 KB)