I’m in the process of developing a platformer in Blender (you can see my thread in the WIP section). I’ve decided that I want to use a wired Xbox controller for the game, but setting up the proper controls for it is a bit difficult.
Using the logic bricks, I was able to get the engine to recognize the left joystick and all of it’s directions. (It’s at the bottom of this page: http://blenderartists.org/forum/showthread.php?t=146769) However, I’m not sure how to rig it with the acctuators. Using the default cube as the ‘player’, it’s mapped like this- Up on the joystick is set to move forward on the Y axis, Down is to move in the opposite direction, and the sides control rotation on the z axis. I’m not sure if this is the right way to set up the controller. When the game engine started, it was always moving even if the joystick was in it’s neutral position. I found out online that setting the threshold to 6200 stops it from doing this.
Here’s the first problem- I need a ‘forward’ command to move in relation to the camera angle. At this point, the Up Axis input moves the cube in relation to it’s front face. I don’t know exactly how to approach this, so help would be appreciated.
There’s a second problem, too. The character needs to move at varying speeds depending on how far the joystick is pushed. I’ve read up on this already, and it seems that this can’t be done using the logic bricks. I don’t know much about Python or coding in general, but if someone could point me to a code that addresses this problem, I could at least study it and reverse engineer it in order to understand what it means. Thanks.
Hey, I’m using an Xbox controller for my game And I’ve actually figured out how to make this work really effectively…
Make an empty outside of the player in front of it (on whichever side you’re moving it forward with) Make an empty in the dead center of the player and parent the empty outside the player to the empty inside the player.
Vertex parent the empty inside the player to the player. (If you don’t know how to vertex parent something, look through google) This just makes it so the empty follows the position of the player but doesn’t copy the orientation.
Parent your camera to the empty in side the player, now where ever that empty is facing so is the camera.
Then, you make the player move forward AND track to the empty outside of it. Instead of rotating the cube with left and right on the joystick, rotate the empty inside the player. Since, the player is always tracking to the empty outside of it, it’s always facing towards it and since the camera is parented to the empty inside the player, the camera is always facing where ever the player is pointing…
I will make a quick example, I’ll edit this post when it’s finished…
Edit: the .blend is in the attachments, I used the arrow keys instead of the joystick sensor… Try it out and tell me if that’s what you were looking for
Left/Right = Move camera left or right
Up = Move cube forward, where the camera is facing
move_to_cam.blend (130 KB)
Thanks, Linkxgl! This setup isn’t exactly what I’m looking for, but it’s one hell of an improvment from my setup.
Here’s what I’m looking for- in your setup, the camera is allowed to move freely when the character is not moving. Once they start moving, the camera is locked behind them like this:
The setup I’m looking to achieve lets the camera move around the character, regardless of which direction they are traveling, like this:
This way, if the player wants the character to move along the path from this specific camera angle, they would move the joystick to the upper right hand corner- the same direction as they see the path. How could I tweak the setup to work like this?
Oh, haha, those examples are much more helpful to understand… That’s much easier though… All you do is unparent the camera from the empty inside the player, make a new empty, place it inside the player (the the other one) vertex parent to the player. Parent the camera to the new empty and rotate that instead of the one with the tracking empty.
I hope you understand what I mean.
Although I still don’t understand how you want the character to move… Do you want left and right to rotate player or the camera? I don’t understand what kind of controls you want for it, if you do, tell me and I can tweak the example to help you if you don’t understand what I’m talking about
Sorry for being vague, but I want the left joystick to control all of the character’s movement in the environment. The right joystick will only control the camera in a circle around the character.
And about your explanation- are you saying that I need two empties in the same character? Which of the empties would be the tracking empty? And apparently the hotkeys for both a regular parent and a vertex parent are Ctrl P. How do I know which one I’m using? (By the way, sorry for all of these questions).
Ohhh, so you want the movement to be the same where ever the camera is looking at it from? That’s much easier than 0.o, no need for 2 empties. One empty in the middle of the player, vertex parent to the player. Camera parented to the empty, rotation on empty. Movement on player. A quick example is in the attachments:
W - forward on player
A - rotate towards left on player
D - rotate towards right on player
Left/Right - rotate camera around player
move_to_cam2.blend (130 KB)
Before I write the same as three days ago here a analog joystick movement Python script.
HG1, thanks for the script! The calibration works well, and the analog control is great. I did notice that the threshold settings for each of the joystick controls was set to 0, but in anything I make, I have to set it at 6200 to stop the cube from moving around itself. I guess the calibration fixes that.
Linkxgl, thanks again for the example. The camera is now completely independent from the player, but there’s just one more thing I need to figure out.
In this example, say the player wants the cube to move to the upper right hand corner (the direction it is currently facing). The player will naturally want to push the joystick to the upper right hand corner also, but the controls force the player only press up on the joystick, which does not correspond to the direction they see on the screen. I would think that all directions on the left joystick need to account for both rotating and moving forward in relevance to the camera angle. It’s really confusing to explain, but have you ever played Super Mario 64? The controls I’m looking for are pretty much the same as that, except I’m replacing the four C buttons on the N64 controller for the right joystick on an Xbox 360 controller.
I know the title is kind of misleading, but the question is mostly about a camera setup using an xbox controller, not too much on using an xbox controller.
I just read your second problem, that is really easy with a python script.
The limit on the amount you push an analogue stick is around 32767. Near the center, it’s in the 1000 - 10000, pushing more increases that number up to 32767. If you are able to divide that with a decimal to get a small speed number, you can just put that into the linV or whatever you are using for speed.
Lets say you want the max speed to be 17 on Linear Velocity.
17/32767 = 0.00051881~
0.00051881 is the increase rate you want on your analogue stick.
Now all you do is get the current value of the joystick controller in real time. This most likely is a variable. We’ll call this a_values. a_values can range from 0 to 32767 or 0 to -32767 (One ONE of the joysticks axises)
So now, if you multiply the rate but the current amount that is being pushed you can get an increase in speed depending on how much you push the stick.
a_values * rate = speed
12234 * 0.00051881 = 6.3471~
32767 * 0.00051881 = 16.9999~ (which is the max speed)
I hope you know where I’m going with this. I’m not going write a script for but it’s really easy to figure out once you know what to do.
You can find all the code you need for this in the link above. Gives you the variables and the constants.
And looking through all the code in here:
Can you help you out greatly, if you don’t know too much python.
Ok, I just played Mario 64, the way I did it the first time is exactly how it’s done in Mario 64 The only thing is, is that you have to make it so which ever way you push the control stick it moves in that direction… I have a a script for that.
Now I totally understand what you mean, but for that you do need some programming…
I’ll make you a quick example… I’ll edit this post when it’s finished, and you’ll see what I’m talking about…
It’s in 2.55 because the script only works in 2.55 for some reason I tried to fix it to work in 2.49b, but it didn’t work, but as a test, you can see if that’s what you need…
Right Stick - Control the direction and movement of the cube
Left/Right trigger - Rotate Camera left and right
I’m pretty sure this is what you want, because this is what I have in my game
This one is still a little glitchy. Personally, I don’t use the logic bricks for joystick movement, because it’s harder to change their variables in real time, but for now, it’s good to go.
Here’s the .blend:
move_to_cam.blend (317 KB)
Just so you know, I didn’t right the direction script for the joystick, a person on Blenderartists named Joeman16 wrote the script, but I set it up the way to work like it does.
Joeman16 script only turns an object to the rotation of where the joystick it pointing. In the setup it moves towards where it’s pointing relative to where the camera is facing
I tried it in 2.49b, but it’s glitchy- I’m still restricted to forward movement in relation to the camera. Also, when the joystick is pushed all of the way, it stops, so you have to go in between. I was gonna get 2.5 at some point anyway, so I may as well download that now and see if it works any better. It’s way easier to play it and see how it works, rather than explaining it in words. I’ll let you know how it goes in 2.5.
By the way, is there some sort of ‘Python for Blender for Dummies’ book out there?
Edit: It works in 2.5! This is exactly what I’m looking for, although you were right when you said the movement is glitchy. Pushing the joystick all the way stops it sometimes, but it does what I want it to do.
I should have thought that a code was necessary to accomplish this. I’m trying to study the code included to somehow make sense of it (and possibly fix up the sporadic movement). At this point Python still confuses the hell out of me, but I really hope I can write this same script myself to use in my game. Thanks again!
I made a couple of tutorials with python
It’d be cool if you could check them out
Jusy scroll down the videos up until the 1st Python tutorial… I start of with just python and no blender, and then I get to the Blender python later on, and it help you grasp Python for a beginner
Awesome, thanks for the tutorials! I’ll make sure to check them out soon. I also noticed that the Python script in the .blend file has an explanation of what each line of code does with the game, so that really helps too.
In the .blend file though, I tried to change the camera controls from the triggers to the right thumbstick, but it won’t let me change the axis number to three for the Xbox controller. It will only accept 1 or 2. Any thoughts on this?
Yeah, that’s the main reason why I used the triggers… With python it works fine, you can also try single axis, I think that works too. I’m not sure why it doesn’t work with the axis…
Sorry, for the delay, but I’m trying to write my own script for analog movement. I’m still pretty confused as to how I should set it up and what order each line should go in. This is what I’ve got so far:
import GameLogic as GL
#gets the current controller#
cont = GL.getCurrentController()
#still dont know what this does, but Ive seen it in every other script written for a controller#
obj = cont.owner
#connects to a Joystick Acctuator set to All Events#
sen = cont.sensors[“Xbox360cont”]
#supposedly gets info from the joystick, assigned to a variable#
JoyL = sen.axisValues
#rate for movement at 17#
rate = 0.00051881
#defines a varible for total speed#
linV = JoyL * rate
In game, the joystick does nothing to the default cube, but it does say something in the console-
TypeError: can’t multiply sequence by non-int of type ‘float’
Could someone please give me suggestions to modify/add to the code to get it to work?
Look here http://www.tutorialsforblender3d.com/GameModule/ClassSCA_JoystickSensor_5.html.
With this function you get a List of 4 integer values for the X,Y,Z and Zrot. You cant multiply all 4 values at the same time with your factor.
x = sen.axisValues
y = sen.axisValues
z = sen.axisValues
zrot = sen.axisValues
With this function you values 2 Joystick’s. But the Xbox 360 controler have a third. For the third you need to use one of this functions.
But I think it is better to use only on function and don’t mix up two functions.
Thanks for the tip, HG1. The code is modified for the x and y values of the joystick, and no errors pop up when I start the game. The only thing left to do is make the cube to actually move. Here’s the modified script:
sen = cont.sensors[“Xbox360cont”]
JoyLx = sen.axisValues
JoyLy = sen.axisValues
rate = 0.00051881
xspeed = JoyLx * rate
yspeed = JoyLy * rate
You must tell me what movement you want.
With motion or servo motion actuator.
linV linear velocity (good for player works only with physic and constant speed).
angV rotate with velocity.
force add a force (good for player works only with physic and speed increase).
torque rotate with force.
dloc sets the object to a new relative position (good for camera move).
dRot rotate relative a object (good for camera rotate).
Without actuator (if you use .owner the script must run on the object).
setLinearVelocity linear velocity (good for player works only with physic and constant speed).
setAngularVelocity rotate with velocity.
applyForce add a force (good for player works only with physic and speed increase).
applytorque rotate with force.
applyMovement sets the object to a new relative position (good for camera move).
applyRotation rotate realtive a object (good for camera rotate).
# import bge
# get controller
cont = bge.logic.getCurrentController()
# get game object controller is attached to
obj = cont.owner
# set local (game object) linear velocity
obj.setLinearVelocity([xspeed , yspeed , 0.0], True)
I used setLinearVelocity([0.0 , 2.0 , 0.0], True]). The cube does move, but it sildes around a lot even after the joystick is released (By the way, the cube is a dynamic object). It still doesn’t seem analog at this point.
One thing doesn’t make sense to me though- the rate was set to move at a velocity of 17, but you can already define how much the cube moves by the setLinearVelocity line- so what’s the point of defining the rate?
If you want to stop fast you must set the speed to 0.001 (0.000 is not working) setLinearVelocity([0.0 , 0.001 , 0.0], True]). But that is only if you set a constant value (digital motion) and is not necessary for analog movement.
If you want analog movement you must give the scaled joystick value (yspeed) to the obj.setLinearVelocity([0.0 , yspeed , 0.0], True).
That is the reason why i wrote obj.setLinearVelocity([xspeed , yspeed , 0.0], True) in my post #18.
If you set the speed to 17 then the cube move with velocity of 17 and you dont have a value between.
When the joystick is in the center of the axis you have the value 0 (scaled 0) and on the right you have -32767 (scaled -17) on the left 32767 (scaled 17). If the joystick is in the middle between the center and and the left you get a value of 16383 (scaled 8,5). But 32767 is a to high speed to use it direct for the movement, so you have to multiply this value with 0.00051881 and then you got for the maximum left only 17 for the speed.
Look at Linkxgl post #9 he has very well explained how that work.