Python + Actuator Physics + mouse control

Game Engine, Players & Web Plug-in, Virtual Reality, support for other engines

Moderators: jesterKing, stiv

Post Reply
zuyuz
Posts: 0
Joined: Mon Sep 29, 2008 5:03 pm

Python + Actuator Physics + mouse control

Post by zuyuz »

If you know of any good, modern, tutorials about using python in blender's game engine, please direct me to those. So far, I've only found ones that are either outdated (refer to deprecated API commands) or incomplete.

Anyway, what I'm trying to do, is to get the cursor's location over the active window and then use that value to apply a variable torque or force to an object. (like you'd see in a flight sim)

So I found these (please forgive the use of deprecated code, I'm a total python noob):

Code: Select all

cont=GameLogic.getCurrentController()
own = cont.getOwner()
move = cont.getActuator("move")
speed = move.getDLoc()[1]
Xpos = Sensor.getXPosition()
Ypos = Sensor.getYPosition()

speed = speed + 0.01
move.setDLoc(0.0, speed, 0.0, 1)
GameLogic.addActiveActuator(move,1)
and

Code: Select all

from GameLogic import *
from Rasterizer import *
from math import *

Cont = getCurrentController()
Own = Cont.getOwner()
Sens = Cont.getSensors()
Sensor = Sens[0]

Height = getWindowHeight()/2
Width = getWindowWidth()/2

# get current mouse position
Xpos = Sensor.getXPosition()
Ypos = Sensor.getYPosition()
The first one works fine, but when I try to incorporate even a single line from the second one into it, so that I can use the mouse, it breaks the whole script. Can you help me consolidate the scripts?

zuyuz
Posts: 0
Joined: Mon Sep 29, 2008 5:03 pm

Nevermind, I got it!

Post by zuyuz »

I'm not totally sure what I did right, but it was something. This code, when attached to a controller, which is, in turn, attached to a motion actuator and mouse movement sensor, allows me to apply a variable torque to an object.

:D

Code: Select all


from GameLogic import *
from Rasterizer import *
from math import *

import Rasterizer as r
r.showMouse(1)

Cont = getCurrentController()

Own = Cont.getOwner()
Sens = Cont.getSensors()
Sensor = Sens[0]

Height = getWindowHeight()/2
Width = getWindowWidth()/2

# get current mouse position
Xpos = Sensor.getXPosition()


cont=GameLogic.getCurrentController()
own = cont.getOwner()
move = cont.getActuator("move")
speed = move.getTorque()[1]

speed = Xpos/100
move.setTorque(0.0, speed, 0.0, 1)

GameLogic.addActiveActuator(move,1)

Post Reply