problem with parent actuator in BGE

    Hello , I use the module below 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")

use a rigid body joint?

also, you could use math and not a physics controller,

if player.getDistanceTo(mouse.hitPosition)>X:
(tab)camera.worldPosition =(camera.worldPosition+camera.worldPosition+mouse.hitPosition+player.worldPosition)*.25

First of all thanks for the very fast response.

Now, I am very new to rigid body joints and the tutorials I saw did not help me a lot.I have also a thread here for this: http://blender.stackexchange.com/questions/28722/how-to-rotate-object-around-another-with-rigid-body-constraint-in-bge
I did not post it here,but tldr if you can make a tutorial of how to use it I would appreciate.

The command you posted seems useful but I do not want to move the camera( if I understood it right and if the lines above are not simplified Pythagorean triple or euclidean distance). I wanna rotate it around the player on any given time by holding the middle mouse button.I want to use the player as a pivot point.

mouse movement ---------and----------MouseLook1
Mouse middle invert------/

mouse movement--------and------------MouseLook2
Mouse middle button-----/

the lines above are (vector 1 + vector 1 + vector 2 + vector 3)/4 = (average of the positions)

So you want reg mouse look when not pressing middle mouse, and different mouselook when middle mouseing?

And

if by could use math and not a physics controller
you meant, I should not use velocity. I just find it smoother for the eye, than just “teleporting” the object through changes in the worldPosition. Yes, I faced problems with it, but solved them till now.

Yes.That is what I try to do.

object.worldPosition = ((object.worldPosition*31)+target.worldPosition)/32

will ‘slide’ the camera to the target up to a certain distance

I would place a object at the camera ’ home ’ position, and use the math to move the camera, that said, I have used physics cams myself in the past.

I am just having a hard time picturing how you want to to respond.

Attachments

2mouseLooks.blend (466 KB)

I understood what you did,but this is the logic behind it that I think I covered in my module.
I am gonna try to be more clear.

While middle button is not pressed,I want the camera to do the following:
1)slide towards the edges of the screen when the mouse cursor is “pushing” her that way,thus giving her velocity towards the direction of the mouse ( x,y in screen co-ordinates) like in most strategy games.
2)zoom in/out by scrolling.

While middle button is pressed:
1)rotate the camera by using the z global axis of the player as pivot point. Like orbiting.

In the current module
The way it is done now in the module,if I press the middle mouse button and move the mouse cursor at the x-axis of the screen resolution,at the right side of the screen centre, it rotates the cube right.Similarly it rotates the cube left.

Now the camera is parented throught the actuator to the cube-player. So it follows the rotation and orbits successfully.

But after it orbits, the slide of the camera that I mentioned above stops happening when I am not pressing the middle button and I move the mouse cursor towards the edges of the screen.


What I am aiming for

1)correcting the above error
2)instead of rotating the cube-player and the camera as a result, I want to fake the rotation of the cube and convince the camera to rotate like the cube would ( but the cube is gonna stay still)

I tried.Was I clear?If i failed again I am gonna try to make a video…

The code here


object.worldPosition = ((object.worldPosition*31)+target.worldPosition)/32

is probably better handled with lerp


object.worldPosition = object.worldPosition.lerp(target.worldPosition, 1/32)

parented objects cannot have forces applied to them,

One method, dont parent the camera, instead use forces and torque to follow the point, and other forces to ‘stretch’ the camera to the bounds?

import bge
cont = bge.logic.getCurrentController()
object = cont.owner
if 'target' not in object:
    object['target'] = bge.logic.getCurrentScene().objects['TargetObjectName']   
else:
     target=object['target']
     V2= object.getVectTo(target)
    object.applyForce(200*V2[0]*V2[1],0)
    object.worldLinearVelocity*=.5
    object.alignAxisToVect(target.worldOrientation.col[0],0,.1)
    object.alignAxisToVect(target.worldOrientation.col[2],2,.1)
    object.applyForce((0,0,9.8*object.mass),0)

Here you are sir :smiley:

Check it out.

Attachments

WhatYouSeek.blend (491 KB)

Ok, you made this look so easy.It works although when you slide the camera after rotation it wants to reset so much that you can’t keep sliding as in the first place. But since someone found my mistake in the original post (I missed a cont.activation duh!) ,another error occured and I was trying to fix that. But in your blend you don’t have this error and it works just as I want it to!

So I am gonna play with that and mix it with mine.Thanks a lot for the blend, and the notification.Will make a new thread for future errors :eyebrowlift:

the stock mouse actuator is using reset cords, you can easily turn that off, but I have not tested it in this case.

This is a method to do what you want without using the mouse data, just the hitPoint

I use apply impulse on a object I create and parent to the ‘CameraHome’ , along a vector to the hitpoint of the mouse,

Attachments

FurtherToWhatYouSeek.blend (511 KB)