Problem defining funtion (Game python script)

Hi, although I have some time using blender and its GE, I’m new to python, and it hadn’t been so dificult to learn (in fact Im impressed how fast to learn it is).

But I have the following problem;

Before doing the actual game I use to do some test blends, this one (attached) is intended to move an Empty (Trackref, to differece from the other empty) thru an ipo curve. Parented to Trackref is other empty called Empty.

This is the script:

 
#import all needed (and uneeded) things
import GameLogic
import Rasterizer
import math
from Rasterizer import setMousePosition
#Getting controller, object list
cont = GameLogic.getCurrentController()
objList = GameLogic.getCurrentScene().getObjectList()
#Owner, mousemove sensor, track empty
Ow = cont.getOwner()
mmove = cont.getSensor("MMov")
track = objList["OBTrackref"]
#Show mouse
Rasterizer.showMouse(1)
#Get center screen coordinates, mouse position
CWidth = Rasterizer.getWindowWidth()/2
CHeight = Rasterizer.getWindowHeight()/2
CurrentXLoc = mmove.getXPosition()
CurrentYLoc = mmove.getYPosition()
#Define cursor-object interaction function
def screenadd(mat1,mat2,mat3,mat4,mat5,fac): 
    return mat1[0]+((mat2-mat3)/mat3*2)*fac,mat1[1],mat1[2]-((mat4-mat5)/mat5*2)*fac
#Center mouse at start
if Ow.init == 0:
 from Rasterizer import setMousePosition
 setMousePosition(CWidth,CHeight)
 Ow.init = 1
#After inicialization
if Ow.init == 1:
 #Prevents mouse from getting out of the game screen
 #Dont know why doesnt work for top/left sides
 if CurrentXLoc >= CWidth*2:
  CurrentXLoc = CWidth*2
  setMousePosition(CurrentXLoc,CurrentYLoc)
 if CurrentXLoc <= 1:
  CurrentXLoc = 1
  setMousePosition(CurrentXLoc,CurrentYLoc)
 if CurrentYLoc >= CHeight*2:
  CurrentYLoc = CHeight*2
  setMousePosition(CurrentXLoc,CurrentYLoc)
 if CurrentYLoc <= 1:
  CurrentYLoc = 1
  setMousePosition(CurrentXLoc,CurrentYLoc)
 #get position of main track object
 orig = track.getPosition()
 #Uses defined fuction to add mouse position to track empty position
 new = screenadd(orig,CurrentXLoc,CWidth,CurrentYLoc,CHeight,2)
 #Aplies changes
 Ow.setPosition(new)
#to chech if position is integrer (which is not the idea)
print new

It works, but in a choppy way

The problem must be in the screenadd function I defined, it seems to be working with integrers while it should be using float numbers.

The screenadd works by getting the position of Trackref, then adds the X component of it with the Xposition of the mouse (which has been substracted by the median X window value to get a centered reference, then divided by the total width of the window so it becomes a resolution independent relation, and finally multiplied by a factor to scale the movement) and does the same with the Z component of Trackref and the Y value of the mouse. Finally this funtion is applied to Empty’s position.

The script also needs to run every frame, but it does only when the mouse moves

The ball is parented to Empty for vizualization utility.

Sorry for my english and thank you beforehand.

Attachments

get to.blend (172 KB)

Changed the function so it didnt added the Trackref position to the Empty position (because its already parented). added float(x) to every variable in function definition.

This is the new code:

 
#import all needed (and uneeded) things
import GameLogic
import Rasterizer
import math
from Rasterizer import setMousePosition
#Getting controller, object list
cont = GameLogic.getCurrentController()
objList = GameLogic.getCurrentScene().getObjectList()
#Owner, mousemove sensor, track empty
Ow = cont.getOwner()
mmove = cont.getSensor("MMov")
track = objList["OBTrackref"]
#Show mouse
#  Rasterizer.showMouse(1)
#Define cursor-object interaction function
def screenadd(mat2,mat3,mat4,mat5,facx,facy): 
    return ((float(mat2)-float(mat3))/float(mat3)*2)*facx,0,-((float(mat4)-float(mat5))/float(mat5)*2)*facy
#Get center screen coordinates, mouse position
CWidth = Rasterizer.getWindowWidth()/2
CHeight = Rasterizer.getWindowHeight()/2
CurrentXLoc = mmove.getXPosition()
CurrentYLoc = mmove.getYPosition()
#Center mouse at start
if Ow.init == 0:
 from Rasterizer import setMousePosition
 setMousePosition(CWidth,CHeight)
 Ow.init = 1
#After inicialization
if Ow.init == 1:
 #Prevents mouse from getting out of the game screen / doesnt works in fullscreen
 CurrentXLoc = max(1,min(CurrentXLoc,CWidth*2))
 CurrentYLoc = max(1,min(CurrentYLoc,CHeight*2))
 setMousePosition(CurrentXLoc,CurrentYLoc)
 #Uses defined fuction to add mouse position to track empty position
 new = screenadd(CurrentXLoc,CWidth,CurrentYLoc,CHeight,4,2.5)
 #Aplies changes
 Ow.setPosition(new)
#to chech if position is integrer (which is not the idea)
print new

And also the .blend file

Attachments

get to.blend (196 KB)