Skip to content
Browse files

ofxGamepadCamera & bugfixes

  • Loading branch information...
1 parent 3eefb4c commit 2b9c4cada8e265fe781987e48a599dfc4d567bb1 Philip Whitfield committed
View
2 example/config.make
@@ -12,7 +12,7 @@ OF_ROOT = ../../..
#ADD THIS TO USE OIS: -DUSE_OIS
-USER_CFLAGS =
+USER_CFLAGS =
# USER_LDFLAGS allows to pass custom flags to the linker
# for example libraries like:
View
25 src/ofxGamepad.cpp
@@ -13,7 +13,10 @@ ofxGamepad::~ofxGamepad()
void ofxGamepad::axisChanged(int axis, int value)
{
- axisValues[axis]=ofMap(value, axisMinVal, axisMaxVal, -1, 1);
+ float val = ofMap(value, axisMinVal, axisMaxVal, -1, 1);
+ if(val>-axisThreshold[axis].min && val<axisThreshold[axis].max)
+ val = 0;
+ axisValues[axis]=val;
static ofxGamepadAxisEvent evt;
evt.gamepad=this;
evt.axis=axis;
@@ -74,12 +77,18 @@ void ofxGamepad::setNumAxis(int amt)
{
numAxis = amt;
axisValues.resize(numAxis);
+ for(int i=0;i<axisValues.size();i++){
+ axisValues[i] = 0;
+ }
}
void ofxGamepad::setNumButtons(int amt)
{
numButtons = amt;
buttonValues.resize(numButtons);
+ for(int i=0;i<buttonValues.size();i++){
+ buttonValues[i] = false;
+ }
}
void ofxGamepad::draw(int x, int y)
@@ -147,4 +156,16 @@ void ofxGamepad::setName(string n)
void ofxGamepad::disable(){
isDisabled=true;
-}
+}
+
+void ofxGamepad::setAxisThreshold(int axis, float thresh)
+{
+ setAxisThreshold(axis, thresh, thresh);
+}
+
+void ofxGamepad::setAxisThreshold(int axis, float min, float max)
+{
+ axisThreshold[axis].max = max;
+ axisThreshold[axis].min = min;
+}
+
View
13 src/ofxGamepad.h
@@ -6,6 +6,15 @@
class ofxGamepad;
+class ofxGamepadThreshold{
+public:
+ ofxGamepadThreshold(){
+ min = max = 0.05;
+ }
+ float min;
+ float max;
+};
+
class ofxGamepadEvent
{
public:
@@ -57,6 +66,9 @@ class ofxGamepad
void disable();
+ void setAxisThreshold(int axis, float thresh);
+ void setAxisThreshold(int axis, float min, float max);
+
ofPoint drawSize;
string name;
int id;
@@ -80,6 +92,7 @@ class ofxGamepad
int numButtons;
std::vector<bool> buttonValues;
std::vector<float> axisValues;
+ std::map<int, ofxGamepadThreshold> axisThreshold;
};
#endif // OFXGAMEPAD_H
View
72 src/ofxGamepadCamera.cpp
@@ -0,0 +1,72 @@
+#include "ofxGamepadCamera.h"
+
+ofxGamepadCamera::ofxGamepadCamera():pad(NULL),speedRotation(70), speedMove(200), useAnalogueDolly(false) {
+}
+
+ofxGamepadCamera::~ofxGamepadCamera() {
+}
+
+void ofxGamepadCamera::setup() {
+ //CHECK IF THERE IS A GAMEPAD CONNECTED
+ if(ofxGamepadHandler::get()->getNumPads()>0) {
+ setGamepad(ofxGamepadHandler::get()->getGamepad(0));
+ } else
+ ofLogWarning("no gamepad connected in ofxGamepadCAmera::setup");
+
+ ofAddListener(ofEvents.update, this, &ofxGamepadCamera::update);
+}
+
+void ofxGamepadCamera::update(ofEventArgs& e) {
+ if(pad == NULL)
+ return;
+
+ float curTime = ofGetElapsedTimef();
+
+ float mult = curTime - lastTime;
+ pan(-pad->getAxisValue(PS3_STICK_R_X)*speedRotation*mult);
+ tilt(-pad->getAxisValue(PS3_STICK_R_Y)*speedRotation*mult);
+
+ truck(pad->getAxisValue(PS3_STICK_L_X)*speedMove*mult);
+ boom(-pad->getAxisValue(PS3_STICK_L_Y)*speedMove*mult);
+
+ if(useAnalogueDolly) {
+ dolly(-(pad->getAxisValueU(PS3_THRUST_L2)-.5)*speedMove*mult);
+ dolly((pad->getAxisValueU(PS3_THRUST_R2)-.5)*speedMove*mult);
+ } else {
+ if(pad->getButtonValue(PS3_BTN_L2)) {
+ dolly(-speedMove*2*mult);
+ }
+ if(pad->getButtonValue(PS3_BTN_R2)) {
+ dolly(speedMove*2*mult);
+ }
+ }
+
+ if(pad->getButtonValue(PS3_BTN_L1)) {
+ roll(-speedRotation*.7*mult);
+ }
+ if(pad->getButtonValue(PS3_BTN_R1)) {
+ roll(speedRotation*.7*mult);
+ }
+
+ if(pad->getButtonValue(PS3_BTN_SELECT))
+ reset();
+
+ lastTime = curTime;
+}
+
+void ofxGamepadCamera::setGamepad(ofxGamepad* p) {
+ pad = p;
+}
+
+void ofxGamepadCamera::setInitialPosition(float x, float y, float z) {
+ setInitialPosition(ofVec3f(x, y, z));
+}
+
+void ofxGamepadCamera::setInitialPosition(ofVec3f pos) {
+ initialPosition = pos;
+}
+
+void ofxGamepadCamera::reset() {
+ setPosition(initialPosition);
+ setOrientation(ofVec3f(0, 0, 0));
+}
View
27 src/ofxGamepadCamera.h
@@ -0,0 +1,27 @@
+#ifndef OFXGAMEPADCAMERA_H
+#define OFXGAMEPADCAMERA_H
+
+#include "ofxGamepadHandler.h"
+
+class ofxGamepadCamera : public ofCamera {
+
+public:
+ ofxGamepadCamera();
+ ~ofxGamepadCamera();
+ void setup();
+ void setGamepad(ofxGamepad* pad);
+ void setInitialPosition(float x, float y, float z);
+ void setInitialPosition(ofVec3f pos);
+ void reset();
+
+ float speedRotation;
+ float speedMove;
+ bool useAnalogueDolly;
+private:
+ void update(ofEventArgs& e);
+ float lastTime;
+ ofxGamepad* pad;
+ ofVec3f initialPosition;
+};
+
+#endif // OFXGAMEPADCAMERA_H
View
2 src/ofxGamepadConfigs.h
@@ -14,6 +14,8 @@ enum PS3_CONFIG {
PS3_STICK_L_Y = 1,
PS3_STICK_R_X = 2,
PS3_STICK_R_Y = 3,
+ PS3_THRUST_L2 = 12,
+ PS3_THRUST_R2 = 13,
PS3_BTN_CROSS = 14,
PS3_BTN_CIRCLE = 13,
PS3_BTN_SQUARE = 15,
View
2 src/ofxGamepadHandler.cpp
@@ -125,6 +125,7 @@ void ofxGamepadHandler::updatePadList() {
gamepadsNew.push_back(ofPtr<ofxGamepad>(new ofxGamepadLinux(file.getAbsolutePath())));
activeIDs.push_back(i);
} catch(std::exception& err) {
+ ofLog(OF_LOG_ERROR, "could not create new gamepad");
}
}
}
@@ -175,6 +176,7 @@ ofxGamepad* ofxGamepadHandler::getGamepad(int num) {
if(getNumPads()>0)
return gamepads[num].get();
ofLog(OF_LOG_WARNING, "ofxGamepad::getGamepad(): WARNING NO GAMEPAD CONNECTED. GAMEPAD IS NULL.");
+ return NULL;
}
int ofxGamepadHandler::getNumPads() {
View
2 src/ofxGamepadLinux.cpp
@@ -75,7 +75,7 @@ void ofxGamepadLinux::setupFF() {
fName = "/dev/input/event"+list[list.size()-1]; //There is a problem here, thought that the event number is always the same as the js number, but it is not...
if ((fdEvent = open(fName.c_str(), O_RDWR)) < 0) { // change from O_RDONLY to O_RDWR need in force feedback
- ofLog(OF_LOG_ERROR,"could not open joystick input event");
+ ofLog(OF_LOG_WARNING,"could not open joystick input event "+fName+". This means force feedback is not going to work");
return;
}
fcntl( fdEvent, F_SETFL, O_NONBLOCK );
View
4 src/ofxGamepadOIS.cpp
@@ -78,11 +78,11 @@ void ofxGamepadOIS::update(){
}
bool ofxGamepadOIS::buttonPressed( const OIS::JoyStickEvent &arg, int button ){
- buttonChanged(button, 0);
+ buttonChanged(button, 1);
return true;
};
bool ofxGamepadOIS::buttonReleased( const OIS::JoyStickEvent &arg, int button ){
- buttonChanged(button, 1);
+ buttonChanged(button, 0);
return true;
};
bool ofxGamepadOIS::axisMoved( const OIS::JoyStickEvent &arg, int axis ){

0 comments on commit 2b9c4ca

Please sign in to comment.
Something went wrong with that request. Please try again.