I’m a complete Blender noob, and new to game scripting as well, so this whole 3D bit is still a hefty struggle. Below is a few hours of poking around and I’m almost satisfied with the results.
I am attempting to write a script to handle aricraft/spacecraft movement. The goal is to have mouse moving back and forth pitch the object up and down, respectively, and to have left and right mouse movement roll the object on its y axis. As it is, the rotation is static, in that it doesn’t matter how the object is facing. I don’t want this. I’m using localOrientation, but it doesn’t seem to be local at all, but global.
Also, I notice a lot of people using render.setMousePosition to place the mouse back at the center of the screen. When I did this it also snapped the object’s rotation back to the center. Removing the call made this work.
ALSO, also, when I first move the mouse the object flips wildly, but after that everything works. For why??
Ultimately I can see this code needing to be scrapped. I have several other concerns with its results, but these are the main ones. If you see anything else wrong please tell me.
I have one sensor called ‘mouse’ (which is where self.mouse comes from), and the properties are all set to 1 for now. I just run this on a basic Cube with dynamic as its physic type.
def getMouseMovementSpeed(self): """ Return the speed the mouse is moving after sensitivity modifier. """ winw = bge.render.getWindowWidth() winh = bge.render.getWindowHeight() x = (winw / 2 - self.mouse.position) * self.owner["xRotationSensitivity"] y = (winh / 2 - self.mouse.position) * self.owner["yRotationSensitivity"] return ((x, y), (winw, winh)) def yRoll(self, speed): z_rotation = self.local_orientation.z * (180 / math.pi) z_rotation += speed self.local_orientation.z = speed / (180 / math.pi) def xPitch(self, speed): x_rotation = self.local_orientation.x * (180 / math.pi) x_rotation += speed self.local_orientation.x = speed / (180 / math.pi) def execute(self): self.mouse_speed, winsize = self.getMouseMovementSpeed() self.local_orientation = self.owner.localOrientation.to_euler() self.yRoll(self.mouse_speed) self.xPitch(self.mouse_speed) self.owner.localOrientation = self.local_orientation.to_matrix() print (self.owner.localOrientation) print (self.owner.localOrientation.to_euler())
Thanks in advance for any help. I’m super excited to understand this better and put it to use! Oh, and no I didn’t copy from existing scripts. I looked a few over to see how the API worked but that was it. Not that I’m proud of it (this code here), but I know some people discredit work if they think it was copied. I’m legitimately trying to learn this stuff!