reading velocity

How is it possible to take the velocity along the local y axis of an object and add it
(or a multiple of it)
to a force along the local z axis when a button is pressed?

The idea is to make a sort of aircraft like movement.
At the moment when you press the up and down arrows it puts a torque on the x (lateral) axis which pitches it up and down.

However i want a force to push it up along the local z+ (direction of lift) when it pitches up and has velocity on the local y axis,
and of course the faster it goes the greater the force so at
yVel=0 then forceZ=0,
and when yVel=100 then forceZ=100.

hope this makes sense, I think most of the stuff i post doesn’t make all that much sense, so just ask for further explanation if needed.

Python. If you want to learn, check out my game development tutorial at Game Up! It’s not too hard - it’s just assigning variables. Use obj.getVelocity()[1] to get the speed of an object on the Y-axis, and then assign it with obj.setLinearVelocity([0, 0, yspd], 1) or a Motion actuator (still using Python). That function I mentioned above (obj.setLinearVelocity) will set the linear velocity of an object upwards on the local axis Z according to the variable yspd, which you would define before that.

Thanks for the reply

I tried doing the following, but this is the first time i have ever had anything to do with python so i really have no idea what to do.

obj.getVelocity()[1] = yspd
obj.setLinearVelocity([0, 0, yspd], 1)

The error i got was;
NameError: name ‘yspd’ is not defined

I also tried the beginning of the tutorial on gameup

from bge import logic
def Player():
cont = logic.getCurrentController()
obj = cont.owner
print (“Hi! I’m at”, obj.position)

However i couldn’t get that to work either, it gave the error;
ImportError: No module named bge

has something changed in 2.57b? What do i do to define yspd?

I think you have to use ‘import gamelogic’ rather than bge as I have read somewhere modules are not automatically imported. Also, shouldnt yspd = obj.getVelocity()[1]?

I understand your position. I’m in the same boat as you- Python is like a whole new world thats very, very scary.

Thanks, I think you’re right about those two being the wrong way round,

I tried this ;

import GameLogic
co = GameLogic.getCurrentController()
obj = co.owner
yspd = obj.getVelocity()[1]
obj.setLinearVelocity([0, 0, yspd], 1)

However neither gamelogic, Gamelogic, GameLogic, or gameLogic work, it still says there is “No module named GameLogic”

Is there something above it, isn’t a module a part of something, that maybe needs to be referenced first?

Tried looking through here,
http://www.blender.org/documentation/blender_python_api_2_57_release/bge.types.html#bge.types.KX_GameObject

but to a noob it makes almost no sense and couldn’t help me.

You probably shouldn’t use GameLogic with Blender 2.57 - it is, indeed, bge.logic, but you have to plug it into a Python controller (either a text resource in Blender, or an external text file in the same directory as your blend file (for simplicity) will work) for scripts to work in the BGE - you can’t just run the script.

To define yspd, you just assign a value to it - yspd = obj.getVelocity()[1].

EDIT: Sorry - that was my mistake up there. I didn’t say what yspd was - it’s a variable that holds your object’s movement velocity (on the Y-axis).

it’s connected to a python controller and the script looks like this;

yspd = obj.getVelocity()[1]
obj.setLinearVelocity([0, 0, yspd], 1)

I get the error
NameError: name ‘obj’ is not defined

(edit)

def Player():
cont = logic.getCurrentController()
obj = cont.owner
yspd = obj.getVelocity()[1]
obj.setLinearVelocity([0, 0, yspd], 1)
print (yspd)

this one doesn’t give any errors, but it doesn’t do anything either

shall i post the blend?

Okay, again, I’m sorry, as I didn’t post what obj is. Here’s the script in its entirety.



from bge import logic

cont = logic.getCurrentController() # Get the controller running this script (the Python controller)
obj = cont.owner # (Get the owner of that controller - this object

yvel = obj.getVelocity()[1] # Get the velocity of the object, which is a list, so separate only the second value (Y-axis velocity)

obj.setLinearVelocity([0, 0, yvel], 1) # And move the object on the Z-axis (locally) according to the Y-axis velocity

That SHOULD work, but I’m not exactly sure, since you say force in the beginning, but this is linear velocity. Try it out and see if it works alright for your purposes.

Thanks again for the reply, that one does work , but it seems to be acting along the global z, and also seems to quite jerky. A force on the local z might be better

the script only seems to be triggered on key press and on release.
Is there a way to make it loop constantly until key release?

I changed it a bit, and it works fine until the local y is the same as global z, then the apply torque pulls it back the other way

from bge import logic

cont = logic.getCurrentController() # Get the controller running this script (the Python controller)
obj = cont.owner # (Get the owner of that controller - this object

xvel = obj.getVelocity()[0] # Get the velocity of the object, which is a list, so separate only the second value (x-axis velocity)

yvel = obj.getVelocity()[1] # Get the velocity of the object, which is a list, so separate only the second value (Y-axis velocity)

zvel = obj.getVelocity()[2] # Get the velocity of the object, which is a list, so separate only the second value (z-axis velocity)

obj.applyForce([0, 0, yvel*10

], 1) # apply force to local z

obj.applyTorque([yvel*12, 0, 0

], 1) # apply torque around local x

here’s a link to the blend, controls are arrow keys and space for thrust
http://www.mediafire.com/?o64zqhsf60y6xoi