need translation of this code into proper python

Hello,

I don’t know how to code in python, but want to check if my object will behave properly if I use real physics equations directly in Blender Game Engine.

If someone could check the code for its errors I would thank a lot!

Greetings André

Attachments

flugdrache_0.2.blend (61 KB)

why the .sqrt?
sqrt is a math function - from math import*

I saw it in some script and thought it would work.

I need the power (or square?) of…

or ^2

but don’t know how to code that.

You can do powers with python- for example, ‘y’ squared can be written as y ** 2. Just use two asterisks.

but why have you added it ton the end of another variable with a dot?

if you want to square root something, import the math module:
import math

then type:
math.sqrt(number)

if you want to use a power:
import math

x = math.pow(number,power)
or
x = number**power

Well, it not a completely mad assumption that .sqrt() should be a method on numerical types - But it’s a wrong assumption in (current) python :slight_smile:

Ok, thx for the answers. I still got one question:

How can I use sin or tan?

math has asin, acos, and atan I think those are the same as sin, cos, tan.

I’m not too great with trigonometry, but asin, acos, and atan are arc sine, arc cosine, and arc tangent, while sin, cos, and tan are sine, cosine, and tangent. To use those, just import math and use them with an angle (in radians).

Ok, found out what the problem was. Imported these two moduls and it worked:
from math import *
from mathutils import *

Another question:

winkel = kite.orientation[1][1]

I was told it means, that it gets the angle between the objects y and the worlds y… what does it mean? why 1 and why only 2 numbers? (why not [1][1][1] for xyz?)

and one last question:

i want to programm the following:

if the angle ist 90° to the ground or the world axis, then the speed should be 0.
How can I do this? And not 0 at one, but steadily decrease according to the angle… angle = 0° full power and steadily decrease until angle = 90° no more power.

Thx in advance for your help.

@SolarLune Ah, yes sorry. I saw asin before in another script with asin and I thought the bge was just renaming it weird.

I could be wrong but I’m fairly certain that own.orientation[1][1] (alone) will not give you any angle.

If you want to find the angle between yours and the worlds axis implement something like this:


import GameLogic as logic
import math
import mathutils as mt

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

#Your orientation
ori = own.orientation

#World Y axis
worldY = mt.Vector([0.0, 1.0, 0.0])

#Your Y axis
ownY = mt.Vector([ori[0][1], ori[1][1], ori[2][1]])

#Getting the angle
angle_first = worldY.angle(ownY)

#Converting the angle to degrees
angle = math.degrees(angle_first)

#And saving it as a property so you can use the angle
own["angle"] = angle

own.orientation has a list within a list it looks like this [[1.0, 0.0, -0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
by using own.orientation[1] you get the second list [0.0, 1.0, 0.0] (remember programmers start counting from zero)
and by using own.orientation[1][1] you get the second variable in the second list 1.0

ahh, thank you for clarifying! now that makes sense!