2
$\begingroup$

I working on a MouseLook script, but the Cap system doesnt work great (When i move too rapidly to the Cap, the camera does ugly jumps).

I explain, my cap upper is set at 160 degree, my camera start at 90 degree, when i move rapidly my mouse up, the camera goes over 160 degrees (more speed, more excessive angle)and rapidly after, the script set the angle at 160 degree, its verry ugly, i dont want the angle goes over my cap value.

i will give you my script for better details.

Maybe the rotation system i used is not correct...

Does someone know how exactly work the cap/rotation system of the MouseMove script by Riyuzakisan?

Here is my faulty script:

import bge, math
from bge import render
from mathutils import Vector

def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    # Inclure l'objet "Camera" dans le script.
    camera = own.children['Camera']

    # Props
    cap = True
    capBas = 20
    capHaut = 160

    # Prendre le sensor "Mouse".
    mouse = cont.sensors ["Mouse"]

    # sensib = Propriété "Sensibilite".
    sensib = own['Sensibilite']

    # Recuperer la taille de l'ecran.
    x = render.getWindowWidth()//2
    y = render.getWindowHeight()//2

    # Definir le centre de l'ecran.
    centre_ecran = (x,y)

    # centre = Centre de l'ecran.
    centre = Vector(centre_ecran)

    # Definir la position de la souris.
    position_souris = Vector(mouse.position)

    # Définir la rotation a effectuer.
    offset = (position_souris-centre)*-sensib*0.0002

    if mouse.positive:
        # Récupérer la rotation de la camera.
        Camrot = camera.localOrientation.to_euler()
        Camrotx = int(math.degrees(Camrot[0]))
        print(Camrotx)

        # Appliquer rotation verticale camera.
        camera.applyRotation(( offset.y, 0, 0), True)

        if Camrotx > capHaut and cap == True:
            Camrot[0] = math.radians(capHaut+0.99)
            camera.localOrientation = Camrot.to_matrix()

        if Camrotx < capBas and cap == True:
            Camrot[0] = math.radians(capBas+0.01)
            camera.localOrientation = Camrot.to_matrix()

    # Appliquer rotation horizontale joueur.
    own.applyRotation((0, 0, offset.x), True)

    # Garder le curseur au centre de l'ecran.
    render.setMousePosition(x,y)

main()

I dont want to use the Mouse Actuator for MouseLook.

Thanks.

$\endgroup$
  • $\begingroup$ Just curious, is this different from the mouse look in Camera Fly Mode? $\endgroup$ – Mentalist Sep 21 '15 at 14:21
  • $\begingroup$ Cap stop the rotation of the camera at a certain angle. $\endgroup$ – Hack0re Sep 21 '15 at 15:16
1
$\begingroup$

I have edited your script just a bit and now it seems to work fine. The main problem with your script was that you applied the rotation first, then checked for the cap and, if the rotation was too big, applied another rotation. I think that this led to the shaking.

The better aproach is to first calculate if the rotation would get too big when adding the offset, and if so just apply as much rotation as needed

import bge, math
from bge import render
from mathutils import Vector

def main():

cont = bge.logic.getCurrentController()
own = cont.owner

# Get the child object named "Camera"
camera = own.children['Camera']

# Props
cap = True
capLow = 20
capHigh = 120

# Get the sensor "Mouse"
mouse = cont.sensors ["Mouse"]

# sensib = Property on this object named "Sensibilite"
sensib = own['Sensibilite']

# Get the center of the screen
x = render.getWindowWidth()//2
y = render.getWindowHeight()//2

screen_mid = (x,y)

# center = Center of the screen
center = Vector(screen_mid)

# Get the position of the mouse
mouse_position = Vector(mouse.position)

# Define the rotation that should be made
offset = (mouse_position-center)*-sensib*0.0002

if mouse.positive: #This part has changed!
    # Get the rotation of the camera
    Camrot = camera.localOrientation.to_euler()
    Camrotx = math.degrees(Camrot[0])
    print(Camrotx)

    # Check if new rotation value would be too high
    if Camrotx + offset.y > capHigh and cap == True:
        # Choose offset.y so that: Camrotx + offset.y = capHigh
        offset.y = math.radians(capHigh - Camrotx)

    # Check if new rotation value would be too low
    elif Camrotx + offset.y < capLow and cap == True:
        offset.y = math.radians(capLow - Camrotx)

    # Apply the rotation the the camera
    camera.applyRotation(( offset.y, 0, 0), True)

# Apply the horizontal rotation to the player/parent object
own.applyRotation((0, 0, offset.x), True)

# Reset the mouse position
render.setMousePosition(x,y)

main()
$\endgroup$
  • 1
    $\begingroup$ Can you put the code in English please. I don't know the language this code was made in (no offence to nationality or country). I would like to see how to correct it. I am still new to Python, but love to learn all I can. Thanks. $\endgroup$ – Iam KraZasHell Jul 21 '18 at 17:01
  • $\begingroup$ @TimIamKra'Z'Shelhorse Ok, I translated all the comments. But it's basically really just a simple difference: The original script changes the rotation of the camera multiple times while mine writes those things to a variable and just applies it at the end. Let me know if you need more explanation :) $\endgroup$ – Ectras Aug 9 '18 at 7:21
  • $\begingroup$ thanks, that will help. $\endgroup$ – Iam KraZasHell Jul 11 at 4:49

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.