1
$\begingroup$

I wrote a module to rotate the camera with middle mouse button pressed, zoom in/out with wheel up/down and at last push the camera towards the direction of the mouse if the mouse is getting near to the end of the screen.

I want the camera to be a parent to the player that I have only when the rotation is taking place.So I activate/deactivate the parent actuator but a problem or an error occurs.

While the camera moves as the mouse suggests if the mouse reaches the end of the screen (That is before I press middle button once!), after I rotate the camera I can no longer "push" it under the same conditions.

I tried activating/deactivating the parent actuator or set the mode to remove itself.And all this time I was reading the physicsId of the camera to make sure it can move with linear velocity.All the readings were ok,and no errors popped so I do not know why the camera stops moving after I rotate it once.

Here is the blend file with the module inside it : test blend

and below is the script clean,without comments. Can anyone find what I am doing wrong?

import bge

bge.render.showMouse(True)

scene = bge.logic.getCurrentScene()

player = scene.objects["player"]

cont = bge.logic.getCurrentController()

wh_up = cont.sensors["wh_up"]
wh_down = cont.sensors["wh_down"]
wh_pressed = cont.sensors["wh_pressed"]

par_to_player = cont.actuators["par_to_player"]

cam = scene.active_camera

mousePos = bge.logic.mouse.position
if not wh_pressed.positive:
    par_to_player.mode = 2
    mX = max(0.0,min(1.0,mousePos[0]))
    mY = max(0.0,min(1.0,mousePos[1]))

if mX < 0.05:
    cam.localLinearVelocity.x = -5*(2-mX)
    cam.localLinearVelocity.y = (0.5-mY)*5
elif mX > 0.95:
    cam.localLinearVelocity.x = 5*(2+mX)
    cam.localLinearVelocity.y = (0.5-mY)*5

if mY < 0.05:
    cam.localLinearVelocity.y = 5*(2+mY)
    cam.localLinearVelocity.x = -(0.5-mX)*5
elif mY > 0.95:
    cam.localLinearVelocity.y = -5*(2-mY)
    cam.localLinearVelocity.x = -(0.5-mX)*5

cam.localLinearVelocity *= 0.9

if (wh_up.positive):
    cam.ortho_scale = cam.ortho_scale+5
if (wh_down.positive):
    cam.ortho_scale = cam.ortho_scale-5

if (wh_pressed.positive):
    bge.render.showMouse(False)
    cont.activate(par_to_player)
    par_to_player.mode = 1
    print (par_to_player.mode)
    mX_rot = max(0.0,min(1.0,mousePos[0]))
    if (mX_rot < mX):
        player.applyRotation([0, 0, -0.01], 0)
    elif (mX_rot > mX):
        player.applyRotation([0, 0, 0.01], 0)

print ("I reached the end of the module")
$\endgroup$
  • $\begingroup$ Much appreciate @Chebhou. Did you understand why the camera gets thrown? I checked the cube and it does not move after I release the MMB. $\endgroup$ – Lev Apr 23 '15 at 2:00
  • $\begingroup$ it keeps some angular-velocity from the rotation , do a check after unparenting if the camera has an angular V set both Linear and angular velocities to Zero $\endgroup$ – Chebhou Apr 23 '15 at 23:34
  • $\begingroup$ @Chebhou if you can check this, blender.stackexchange.com/questions/29092/… Can't seem to stop it. $\endgroup$ – Lev Apr 30 '15 at 1:28
1
$\begingroup$

you are not unparenting the camera after rotation correctly , you have to set the mode to 2 and activate the actuator to take effect :

if not wh_pressed.positive:
    par_to_player.mode = 2
    cont.activate(par_to_player)

also you may need to kill the rotation after unparenting ( the camera gets thrown away after rotation )

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.