diff --git a/example-ComplexPolygon/openFrameworks-Info.plist b/example-ComplexPolygon/openFrameworks-Info.plist index c4237f2..b829190 100644 --- a/example-ComplexPolygon/openFrameworks-Info.plist +++ b/example-ComplexPolygon/openFrameworks-Info.plist @@ -3,24 +3,32 @@ CFBundleDevelopmentRegion - English + $(DEVELOPMENT_LANGUAGE) CFBundleExecutable - ${EXECUTABLE_NAME} + $(EXECUTABLE_NAME) + CFBundleIconFile + $(ICON_NAME) CFBundleIdentifier - cc.openFrameworks.ofapp + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 + CFBundleName + $(TARGET_NAME) CFBundlePackageType APPL CFBundleSignature ???? + CFBundleShortVersionString + $(VERSION) CFBundleVersion - 1.0 - CFBundleIconFile - ${ICON} - NSCameraUsageDescription + $(VERSION) + LSApplicationCategoryType + public.app-category.graphics-design + NSCameraUsageDescription This app needs to access the camera NSMicrophoneUsageDescription This app needs to access the microphone + NSHighResolutionCapable + diff --git a/example-ContactListener/openFrameworks-Info.plist b/example-ContactListener/openFrameworks-Info.plist index c4237f2..b829190 100644 --- a/example-ContactListener/openFrameworks-Info.plist +++ b/example-ContactListener/openFrameworks-Info.plist @@ -3,24 +3,32 @@ CFBundleDevelopmentRegion - English + $(DEVELOPMENT_LANGUAGE) CFBundleExecutable - ${EXECUTABLE_NAME} + $(EXECUTABLE_NAME) + CFBundleIconFile + $(ICON_NAME) CFBundleIdentifier - cc.openFrameworks.ofapp + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 + CFBundleName + $(TARGET_NAME) CFBundlePackageType APPL CFBundleSignature ???? + CFBundleShortVersionString + $(VERSION) CFBundleVersion - 1.0 - CFBundleIconFile - ${ICON} - NSCameraUsageDescription + $(VERSION) + LSApplicationCategoryType + public.app-category.graphics-design + NSCameraUsageDescription This app needs to access the camera NSMicrophoneUsageDescription This app needs to access the microphone + NSHighResolutionCapable + diff --git a/example-CustomData/openFrameworks-Info.plist b/example-CustomData/openFrameworks-Info.plist index c4237f2..b829190 100644 --- a/example-CustomData/openFrameworks-Info.plist +++ b/example-CustomData/openFrameworks-Info.plist @@ -3,24 +3,32 @@ CFBundleDevelopmentRegion - English + $(DEVELOPMENT_LANGUAGE) CFBundleExecutable - ${EXECUTABLE_NAME} + $(EXECUTABLE_NAME) + CFBundleIconFile + $(ICON_NAME) CFBundleIdentifier - cc.openFrameworks.ofapp + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 + CFBundleName + $(TARGET_NAME) CFBundlePackageType APPL CFBundleSignature ???? + CFBundleShortVersionString + $(VERSION) CFBundleVersion - 1.0 - CFBundleIconFile - ${ICON} - NSCameraUsageDescription + $(VERSION) + LSApplicationCategoryType + public.app-category.graphics-design + NSCameraUsageDescription This app needs to access the camera NSMicrophoneUsageDescription This app needs to access the microphone + NSHighResolutionCapable + diff --git a/example-EdgeShape/openFrameworks-Info.plist b/example-EdgeShape/openFrameworks-Info.plist index c4237f2..b829190 100644 --- a/example-EdgeShape/openFrameworks-Info.plist +++ b/example-EdgeShape/openFrameworks-Info.plist @@ -3,24 +3,32 @@ CFBundleDevelopmentRegion - English + $(DEVELOPMENT_LANGUAGE) CFBundleExecutable - ${EXECUTABLE_NAME} + $(EXECUTABLE_NAME) + CFBundleIconFile + $(ICON_NAME) CFBundleIdentifier - cc.openFrameworks.ofapp + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 + CFBundleName + $(TARGET_NAME) CFBundlePackageType APPL CFBundleSignature ???? + CFBundleShortVersionString + $(VERSION) CFBundleVersion - 1.0 - CFBundleIconFile - ${ICON} - NSCameraUsageDescription + $(VERSION) + LSApplicationCategoryType + public.app-category.graphics-design + NSCameraUsageDescription This app needs to access the camera NSMicrophoneUsageDescription This app needs to access the microphone + NSHighResolutionCapable + diff --git a/example-Edges/openFrameworks-Info.plist b/example-Edges/openFrameworks-Info.plist index c4237f2..b829190 100644 --- a/example-Edges/openFrameworks-Info.plist +++ b/example-Edges/openFrameworks-Info.plist @@ -3,24 +3,32 @@ CFBundleDevelopmentRegion - English + $(DEVELOPMENT_LANGUAGE) CFBundleExecutable - ${EXECUTABLE_NAME} + $(EXECUTABLE_NAME) + CFBundleIconFile + $(ICON_NAME) CFBundleIdentifier - cc.openFrameworks.ofapp + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 + CFBundleName + $(TARGET_NAME) CFBundlePackageType APPL CFBundleSignature ???? + CFBundleShortVersionString + $(VERSION) CFBundleVersion - 1.0 - CFBundleIconFile - ${ICON} - NSCameraUsageDescription + $(VERSION) + LSApplicationCategoryType + public.app-category.graphics-design + NSCameraUsageDescription This app needs to access the camera NSMicrophoneUsageDescription This app needs to access the microphone + NSHighResolutionCapable + diff --git a/example-Edges/src/ofApp.cpp b/example-Edges/src/ofApp.cpp index 4683a54..262cca4 100755 --- a/example-Edges/src/ofApp.cpp +++ b/example-Edges/src/ofApp.cpp @@ -16,7 +16,7 @@ void ofApp::setup() { // load the lines we saved... - ifstream f; + std::ifstream f; f.open(ofToDataPath("lines.txt").c_str()); vector strLines; while (!f.eof()) { @@ -106,7 +106,7 @@ void ofApp::keyPressed(int key) { /* // want to save out some line... if(key == ' ') { - ofstream f; + std::ofstream f; f.clear(); f.open(ofToDataPath("lines.txt").c_str()); for (int i=0; i CFBundleDevelopmentRegion - English + $(DEVELOPMENT_LANGUAGE) CFBundleExecutable - ${EXECUTABLE_NAME} + $(EXECUTABLE_NAME) + CFBundleIconFile + $(ICON_NAME) CFBundleIdentifier - cc.openFrameworks.ofapp + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 + CFBundleName + $(TARGET_NAME) CFBundlePackageType APPL CFBundleSignature ???? + CFBundleShortVersionString + $(VERSION) CFBundleVersion - 1.0 - CFBundleIconFile - ${ICON} - NSCameraUsageDescription + $(VERSION) + LSApplicationCategoryType + public.app-category.graphics-design + NSCameraUsageDescription This app needs to access the camera NSMicrophoneUsageDescription This app needs to access the microphone + NSHighResolutionCapable + diff --git a/example-Joint/openFrameworks-Info.plist b/example-Joint/openFrameworks-Info.plist index c4237f2..b829190 100644 --- a/example-Joint/openFrameworks-Info.plist +++ b/example-Joint/openFrameworks-Info.plist @@ -3,24 +3,32 @@ CFBundleDevelopmentRegion - English + $(DEVELOPMENT_LANGUAGE) CFBundleExecutable - ${EXECUTABLE_NAME} + $(EXECUTABLE_NAME) + CFBundleIconFile + $(ICON_NAME) CFBundleIdentifier - cc.openFrameworks.ofapp + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 + CFBundleName + $(TARGET_NAME) CFBundlePackageType APPL CFBundleSignature ???? + CFBundleShortVersionString + $(VERSION) CFBundleVersion - 1.0 - CFBundleIconFile - ${ICON} - NSCameraUsageDescription + $(VERSION) + LSApplicationCategoryType + public.app-category.graphics-design + NSCameraUsageDescription This app needs to access the camera NSMicrophoneUsageDescription This app needs to access the microphone + NSHighResolutionCapable + diff --git a/example-ParticleSystem/openFrameworks-Info.plist b/example-ParticleSystem/openFrameworks-Info.plist index c4237f2..b829190 100644 --- a/example-ParticleSystem/openFrameworks-Info.plist +++ b/example-ParticleSystem/openFrameworks-Info.plist @@ -3,24 +3,32 @@ CFBundleDevelopmentRegion - English + $(DEVELOPMENT_LANGUAGE) CFBundleExecutable - ${EXECUTABLE_NAME} + $(EXECUTABLE_NAME) + CFBundleIconFile + $(ICON_NAME) CFBundleIdentifier - cc.openFrameworks.ofapp + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 + CFBundleName + $(TARGET_NAME) CFBundlePackageType APPL CFBundleSignature ???? + CFBundleShortVersionString + $(VERSION) CFBundleVersion - 1.0 - CFBundleIconFile - ${ICON} - NSCameraUsageDescription + $(VERSION) + LSApplicationCategoryType + public.app-category.graphics-design + NSCameraUsageDescription This app needs to access the camera NSMicrophoneUsageDescription This app needs to access the microphone + NSHighResolutionCapable + diff --git a/example-ShapeTexturing/openFrameworks-Info.plist b/example-ShapeTexturing/openFrameworks-Info.plist index c4237f2..b829190 100644 --- a/example-ShapeTexturing/openFrameworks-Info.plist +++ b/example-ShapeTexturing/openFrameworks-Info.plist @@ -3,24 +3,32 @@ CFBundleDevelopmentRegion - English + $(DEVELOPMENT_LANGUAGE) CFBundleExecutable - ${EXECUTABLE_NAME} + $(EXECUTABLE_NAME) + CFBundleIconFile + $(ICON_NAME) CFBundleIdentifier - cc.openFrameworks.ofapp + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 + CFBundleName + $(TARGET_NAME) CFBundlePackageType APPL CFBundleSignature ???? + CFBundleShortVersionString + $(VERSION) CFBundleVersion - 1.0 - CFBundleIconFile - ${ICON} - NSCameraUsageDescription + $(VERSION) + LSApplicationCategoryType + public.app-category.graphics-design + NSCameraUsageDescription This app needs to access the camera NSMicrophoneUsageDescription This app needs to access the microphone + NSHighResolutionCapable + diff --git a/example-Simple/Project.xcconfig b/example-Simple/Project.xcconfig index 40adc89..6f5cd81 100644 --- a/example-Simple/Project.xcconfig +++ b/example-Simple/Project.xcconfig @@ -1,18 +1,41 @@ -//THE PATH TO THE ROOT OF OUR OF PATH RELATIVE TO THIS PROJECT. -//THIS NEEDS TO BE DEFINED BEFORE CoreOF.xcconfig IS INCLUDED OF_PATH = ../../.. +#include "../../../libs/openFrameworksCompiled/project/macos/CoreOF.xcconfig" -//THIS HAS ALL THE HEADER AND LIBS FOR OF CORE -#include "../../../libs/openFrameworksCompiled/project/osx/CoreOF.xcconfig" +//CLANG_CXX_LANGUAGE_STANDARD = c++23 +//CLANG_C_LANGUAGE_STANDARD = c17 +//MACOSX_DEPLOYMENT_TARGET = 11.5 +MARKETING_VERSION = 0.1.0 +CURRENT_PROJECT_VERSION = 1 -//ICONS - NEW IN 0072 -ICON_NAME_DEBUG = icon-debug.icns -ICON_NAME_RELEASE = icon.icns -ICON_FILE_PATH = $(OF_PATH)/libs/openFrameworksCompiled/project/osx/ +BUILD_ARCHS = +// Comment out next three lines to build universal or archive / send to app store +BUILD_ARCHS = -arch arm64 +ARCHS = arm64 +ONLY_ACTIVE_ARCH = YES -//IF YOU WANT AN APP TO HAVE A CUSTOM ICON - PUT THEM IN YOUR DATA FOLDER AND CHANGE ICON_FILE_PATH to: -//ICON_FILE_PATH = bin/data/ +//OF_CODESIGN = 1 +//OF_BUNDLE_DATA_FOLDER = 1 +//OF_BUNDLE_DYLIBS = 1 + +PRODUCT_NAME = $(TARGET_NAME) +PRODUCT_NAME[config=Debug] = $(TARGET_NAME)Debug +PRODUCT_BUNDLE_IDENTIFIER = cc.openFrameworks.${TARGET_NAME:rfc1034identifier} +DEVELOPMENT_LANGUAGE = English +CODE_SIGN_IDENTITY = - +INFOPLIST_FILE = openFrameworks-Info.plist +GENERATE_INFOPLIST_FILE = YES +ENABLE_USER_SCRIPT_SANDBOXING=NO +HIGH_RESOLUTION_CAPABLE = YES +INFOPLIST_KEY_LSApplicationCategoryType = public.app-category.games + +ICON_NAME = of.icns +ICON_NAME[config=Debug] = of_debug.icns +ICON_FILE = $(OF_PATH)/libs/openFrameworksCompiled/project/macos/$(ICON_NAME) + +#include? "App.xcconfig" OTHER_CFLAGS = $(OF_CORE_CFLAGS) OTHER_LDFLAGS = $(OF_CORE_LIBS) $(OF_CORE_FRAMEWORKS) HEADER_SEARCH_PATHS = $(OF_CORE_HEADERS) + +OF_CORE_BUILD_COMMAND = xcodebuild $BUILD_ARCHS -project $OF_PATH/libs/openFrameworksCompiled/project/macos/openFrameworksLib.xcodeproj -target openFrameworks -configuration ${CONFIGURATION} diff --git a/example-Simple/openFrameworks-Info.plist b/example-Simple/openFrameworks-Info.plist index c4237f2..c362e38 100644 --- a/example-Simple/openFrameworks-Info.plist +++ b/example-Simple/openFrameworks-Info.plist @@ -3,24 +3,32 @@ CFBundleDevelopmentRegion - English + ${DEVELOPMENT_LANGUAGE} CFBundleExecutable ${EXECUTABLE_NAME} + CFBundleIconFile + ${ICON_NAME} CFBundleIdentifier - cc.openFrameworks.ofapp + ${PRODUCT_BUNDLE_IDENTIFIER} CFBundleInfoDictionaryVersion 6.0 + CFBundleName + ${TARGET_NAME} CFBundlePackageType APPL CFBundleSignature ???? + CFBundleShortVersionString + ${MARKETING_VERSION} CFBundleVersion - 1.0 - CFBundleIconFile - ${ICON} - NSCameraUsageDescription + ${CURRENT_PROJECT_VERSION} + NSCameraUsageDescription This app needs to access the camera NSMicrophoneUsageDescription This app needs to access the microphone + NSHighResolutionCapable + ${HIGH_RESOLUTION_CAPABLE} + NSCameraUseContinuityCameraDeviceType + diff --git a/example-TwoWorlds/openFrameworks-Info.plist b/example-TwoWorlds/openFrameworks-Info.plist index c4237f2..b829190 100644 --- a/example-TwoWorlds/openFrameworks-Info.plist +++ b/example-TwoWorlds/openFrameworks-Info.plist @@ -3,24 +3,32 @@ CFBundleDevelopmentRegion - English + $(DEVELOPMENT_LANGUAGE) CFBundleExecutable - ${EXECUTABLE_NAME} + $(EXECUTABLE_NAME) + CFBundleIconFile + $(ICON_NAME) CFBundleIdentifier - cc.openFrameworks.ofapp + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 + CFBundleName + $(TARGET_NAME) CFBundlePackageType APPL CFBundleSignature ???? + CFBundleShortVersionString + $(VERSION) CFBundleVersion - 1.0 - CFBundleIconFile - ${ICON} - NSCameraUsageDescription + $(VERSION) + LSApplicationCategoryType + public.app-category.graphics-design + NSCameraUsageDescription This app needs to access the camera NSMicrophoneUsageDescription This app needs to access the microphone + NSHighResolutionCapable + diff --git a/src/ofxBox2d.cpp b/src/ofxBox2d.cpp index bbef972..6ee98f1 100755 --- a/src/ofxBox2d.cpp +++ b/src/ofxBox2d.cpp @@ -11,19 +11,19 @@ float ofxBox2d::getScale() { return ofxBox2d::scale; } -ofVec2f ofxBox2d::toOf(const b2Vec2 v) { - return ofVec2f(v.x * ofxBox2d::scale, v.y * ofxBox2d::scale); +glm::vec2 ofxBox2d::toOf(const b2Vec2 v) { + return glm::vec2(v.x * ofxBox2d::scale, v.y * ofxBox2d::scale); } -ofVec2f ofxBox2d::toOf(float x, float y) { - return ofVec2f(x * ofxBox2d::scale, y * ofxBox2d::scale); +glm::vec2 ofxBox2d::toOf(float x, float y) { + return glm::vec2(x * ofxBox2d::scale, y * ofxBox2d::scale); } float ofxBox2d::toOf(float f) { return f * ofxBox2d::scale; } -b2Vec2 ofxBox2d::toB2d(ofPoint pt) { +b2Vec2 ofxBox2d::toB2d(glm::vec2 pt) { return b2Vec2(pt.x / ofxBox2d::scale, pt.y / ofxBox2d::scale); } @@ -34,12 +34,12 @@ float ofxBox2d::toB2d(float f) { return f / ofxBox2d::scale; } -// ------------------------------------------------------ +// ------------------------------------------------------ ofxBox2d::ofxBox2d() { enableContactEvents = false; world = NULL; m_bomb = NULL; - + #ifdef TARGET_OPENGLES // touch grabbing for(int i=0; iDestroyBody(mouseBody); } #endif - + if(world) { - + // Fix from: https://github.com/vanderlin/ofxBox2d/issues/62 b2Body* f = world->GetBodyList(); while (f) { @@ -117,18 +117,19 @@ int ofxBox2d::calculateParticleIterations(float32 gravity, float32 radius, float // init // ------------------------------------------------------ void ofxBox2d::init(float _hz, float _gx, float _gy) { - + // settings bHasContactListener = false; bCheckBounds = false; bEnableGrabbing = true; ofxBox2d::scale = 30.0f; doSleep = true; - + // gravity - gravity.set(_gx, _gy); - -#ifdef TARGET_OPENGLES + gravity = {_gx, _gy}; +// gravity.set(_gx, _gy); + +#ifdef TARGET_OPENGLES // touch grabbing for( int i=0; iDestroyBody(ground); ground = nullptr; @@ -157,13 +158,13 @@ void ofxBox2d::init(float _hz, float _gx, float _gy) { world = new b2World(b2Vec2(gravity.x, gravity.y)); world->SetAllowSleeping(doSleep); //world->SetDebugDraw(&debugRender); - + // set the hz and interaction cycles hz = _hz; velocityIterations = 8; positionIterations = 3; particleIterations = world->CalculateReasonableParticleIterations(hz); - + ofLog(OF_LOG_NOTICE, "ofxBox2d:: - world created -"); } @@ -183,7 +184,7 @@ void ofxBox2d::disableEvents() { // ------------------------------------------------------ grab shapes void ofxBox2d::setContactListener(ofxBox2dContactListener * listener) { - + if(world != NULL) { bHasContactListener = true; world->SetContactListener(listener); @@ -228,40 +229,40 @@ void ofxBox2d::mouseReleased(ofMouseEventArgs &e) { } #endif -// ------------------------------------------------------ +// ------------------------------------------------------ void ofxBox2d::grabShapeDown(float x, float y, int id) { - + if(world == NULL) { ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -"); return; } - + if(bEnableGrabbing) { b2Vec2 p(x/ofxBox2d::scale, y/ofxBox2d::scale); - -#ifdef TARGET_OPENGLES + +#ifdef TARGET_OPENGLES if(id >= 0 && id < OF_MAX_TOUCH_JOINTS) { if(touchJoints[id] != NULL) - return; - + return; + if( touchBodies[id] == NULL) { b2BodyDef bd; touchBodies[id] = world->CreateBody(&bd); } } - else + else return; // invalid mouse / touch id. #else if (mouseJoint != NULL) { return; } - + if(mouseBody == NULL) { b2BodyDef bd; mouseBody = world->CreateBody(&bd); } - + #endif // Make a small box. @@ -270,14 +271,14 @@ void ofxBox2d::grabShapeDown(float x, float y, int id) { d.Set(0.001f, 0.001f); aabb.lowerBound = p - d; aabb.upperBound = p + d; - + // Query the world for overlapping shapes. QueryCallback callback(p); world->QueryAABB(&callback, aabb); - + if (callback.m_fixture) { b2Body* body = callback.m_fixture->GetBody(); - b2MouseJointDef md; + b2MouseJointDef md; md.bodyB = body; md.target = p; md.maxForce = 1000.0f * body->GetMass(); @@ -287,19 +288,19 @@ void ofxBox2d::grabShapeDown(float x, float y, int id) { #else md.bodyA = mouseBody; mouseJoint = (b2MouseJoint*)world->CreateJoint(&md); -#endif - +#endif + body->SetAwake(true); } - - + + } - + } -// ------------------------------------------------------ +// ------------------------------------------------------ void ofxBox2d::grabShapeUp(float x, float y, int id) { -#ifdef TARGET_OPENGLES +#ifdef TARGET_OPENGLES if(id >= 0 && id < OF_MAX_TOUCH_JOINTS) { if(touchJoints[id] && bEnableGrabbing){ if(world == NULL) { @@ -310,7 +311,7 @@ void ofxBox2d::grabShapeUp(float x, float y, int id) { touchJoints[id] = NULL; } } -#else +#else if(mouseJoint && bEnableGrabbing) { if(world == NULL) { ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -"); @@ -322,23 +323,23 @@ void ofxBox2d::grabShapeUp(float x, float y, int id) { #endif } -// ------------------------------------------------------ +// ------------------------------------------------------ void ofxBox2d::grabShapeDragged(float x, float y, int id) { b2Vec2 p = toB2d(x, y); -#ifdef TARGET_OPENGLES +#ifdef TARGET_OPENGLES if(id >= 0 && id < OF_MAX_TOUCH_JOINTS) { if (touchJoints[id] && bEnableGrabbing) touchJoints[id]->SetTarget(p); } -#else +#else if (mouseJoint && bEnableGrabbing) mouseJoint->SetTarget(p); #endif - - + + } -// ------------------------------------------------------ +// ------------------------------------------------------ int ofxBox2d::getBodyCount() { if(world) return world->GetBodyCount(); @@ -356,7 +357,7 @@ void ofxBox2d::wakeupShapes() { ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -"); return; } - + b2Body* bodies = world->GetBodyList(); while(bodies) { b2Body* b = bodies; @@ -365,7 +366,7 @@ void ofxBox2d::wakeupShapes() { } bodies = bodies->GetNext(); } - + } // ------------------------------------------------------ @@ -377,7 +378,7 @@ void ofxBox2d::setFPS(float _hz) { #pragma mark - gravity // ------------------------------------------------------ -void ofxBox2d::setGravity(ofPoint pt) { +void ofxBox2d::setGravity(glm::vec2 pt) { setGravity(pt.x, pt.y); } @@ -398,53 +399,53 @@ void ofxBox2d::setGravity(float x, float y) { return; } world->SetGravity(b2Vec2(x, y)); - + // update the particle iteration based on gravity particleIterations = world->CalculateReasonableParticleIterations(hz); - ofLogNotice() << "particleIterations " << ofToString(particleIterations); - +// ofLogNotice() << "particleIterations " << ofToString(particleIterations); + // wake up all shapes! wakeupShapes(); } //-------------------------------------------------------------- -ofPoint ofxBox2d::getGravity() { +glm::vec2 ofxBox2d::getGravity() { if(world == NULL) { ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -"); - return ofPoint(); + return glm::vec2(); } - return ofPoint(world->GetGravity().x, world->GetGravity().y); + return glm::vec2(world->GetGravity().x, world->GetGravity().y); } // ------------------------------------------------------ set bounds -void ofxBox2d::setBounds(ofPoint lowBounds, ofPoint upBounds) { +void ofxBox2d::setBounds(glm::vec2 lowBounds, glm::vec2 upBounds) { //TODO: still need to work on this... } // ------------------------------------------------------ create Ground void ofxBox2d::createGround(float x1, float y1, float x2, float y2) { - + if(world == NULL) { ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -"); return; } - + // if we have ground we need to destroy it if(ground != NULL) { world->DestroyBody(ground); ground = NULL; } - + b2BodyDef bd; ground = world->CreateBody(&bd); - + b2EdgeShape shape; shape.Set(toB2d(x1, y1), toB2d(x2, y2)); ground->CreateFixture(&shape, 0.0f); } // ------------------------------------------------------ create Ground -void ofxBox2d::createGround(const ofPoint & p1, const ofPoint & p2) { +void ofxBox2d::createGround(const glm::vec2 & p1, const glm::vec2 & p2) { createGround(p1.x, p1.y, p2.x, p2.y); } @@ -455,43 +456,43 @@ void ofxBox2d::createBounds(ofRectangle rec) { // ------------------------------------------------------ create bounds void ofxBox2d::createBounds(float x, float y, float w, float h) { - + if(world == NULL) { ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -"); return; } - + // if we have ground we need to destroy it if(ground != NULL) { world->DestroyBody(ground); ground = NULL; } - + b2BodyDef bd; bd.position.Set(0, 0); - ground = world->CreateBody(&bd); - + ground = world->CreateBody(&bd); + b2EdgeShape shape; - + ofRectangle rec(toB2d(x), toB2d(y), toB2d(w), toB2d(h)); - - + + // right wall shape.Set(b2Vec2(rec.x+rec.width, rec.y), b2Vec2(rec.x+rec.width, rec.y+rec.height)); ground->CreateFixture(&shape, 0.0f); - + // left wall shape.Set(b2Vec2(rec.x, rec.y), b2Vec2(rec.x, rec.y+rec.height)); ground->CreateFixture(&shape, 0.0f); - + // top wall shape.Set(b2Vec2(rec.x, rec.y), b2Vec2(rec.x+rec.width, rec.y)); ground->CreateFixture(&shape, 0.0f); - + // bottom wall shape.Set(b2Vec2(rec.x, rec.y+rec.height), b2Vec2(rec.x+rec.width, rec.y+rec.height)); ground->CreateFixture(&shape, 0.0f); - + } // ------------------------------------------------------ check if shapes are out of bounds @@ -499,20 +500,20 @@ void ofxBox2d::checkBounds(bool b) { bCheckBounds = b; } -// ------------------------------------------------------ +// ------------------------------------------------------ void ofxBox2d::setIterations(int velocityTimes, int positionTimes) { velocityIterations = velocityTimes; positionIterations = positionTimes; } -// ------------------------------------------------------ +// ------------------------------------------------------ void ofxBox2d::update() { if(world == NULL) { ofLog(OF_LOG_WARNING, "ofxBox2d:: - Need a world, call init first! -"); return; } - + // destroy the object if we are out of the bounds if(bCheckBounds) { /* @@ -520,7 +521,7 @@ void ofxBox2d::update() { float bottom = ofGetHeight(); float right = ofGetWidth(); float left = 0; - + b2Body* node = world->GetBodyList(); while(node) { b2Body* b = node; @@ -529,7 +530,7 @@ void ofxBox2d::update() { ofxBox2dBaseShape* base = (ofxBox2dBaseShape*)b->GetUserData(); if(base) { //printf("dead:%i\n", base->dead); - + if(p.y*OFX_BOX2D_SCALE > bottom) { base->dead = true; world->DestroyBody(b); @@ -547,10 +548,10 @@ void ofxBox2d::update() { world->DestroyBody(b); } */ - - + + } - + world->Step(getTimeStep(), velocityIterations, positionIterations, particleIterations); } @@ -559,11 +560,11 @@ float ofxBox2d::getTimeStep() { return hz > 0.0f ? 1.0f / hz : 0.0f; } -// ------------------------------------------------------ +// ------------------------------------------------------ void ofxBox2d::drawGround() { - + if(ground == NULL) return; - + // const b2Transform& xf = ground->GetTransform(); for (b2Fixture* f = ground->GetFixtureList(); f; f = f->GetNext()) { b2EdgeShape * edge = (b2EdgeShape*)f->GetShape(); @@ -575,8 +576,7 @@ void ofxBox2d::drawGround() { } } -// ------------------------------------------------------ +// ------------------------------------------------------ void ofxBox2d::draw() { drawGround(); } - diff --git a/src/ofxBox2d.h b/src/ofxBox2d.h index efb23e4..24c2df3 100755 --- a/src/ofxBox2d.h +++ b/src/ofxBox2d.h @@ -19,42 +19,42 @@ class ofxBox2dContactArgs : public ofEventArgs { public: - + b2Fixture * a; b2Fixture * b; }; class ofxBox2d : public b2ContactListener { - + private: - + bool enableContactEvents; float hz; int velocityIterations; int positionIterations; int particleIterations; - ofPoint gravity; + glm::vec2 gravity; static float scale; - + // Called when two fixtures begin to touch. - void BeginContact(b2Contact* contact) { + void BeginContact(b2Contact* contact) { static ofxBox2dContactArgs args; args.a = contact->GetFixtureA(); args.b = contact->GetFixtureB(); ofNotifyEvent( contactStartEvents, args, this); } - + // Called when two fixtures cease to touch. - void EndContact(b2Contact* contact) { + void EndContact(b2Contact* contact) { static ofxBox2dContactArgs args; args.a = contact->GetFixtureA(); args.b = contact->GetFixtureB(); ofNotifyEvent( contactEndEvents, args, this); } - - + + public: - + // b2AABB worldAABB; b2World * world; ofxBox2dRender debugRender; @@ -63,17 +63,17 @@ class ofxBox2d : public b2ContactListener { bool bEnableGrabbing; bool bCheckBounds; bool bHasContactListener; - + b2BodyDef bd; b2Body* m_bomb; - -#ifdef TARGET_OPENGLES + +#ifdef TARGET_OPENGLES b2MouseJoint* touchJoints[ OF_MAX_TOUCH_JOINTS ]; b2Body* touchBodies[ OF_MAX_TOUCH_JOINTS ]; #else - b2MouseJoint* mouseJoint; + b2MouseJoint* mouseJoint; b2Body* mouseBody; -#endif +#endif b2Body* ground; b2Body* mainBody; @@ -82,41 +82,41 @@ class ofxBox2d : public b2ContactListener { void disableEvents(); ofEvent contactStartEvents; ofEvent contactEndEvents; - - // ------------------------------------------------------ + + // ------------------------------------------------------ ofxBox2d(); ~ofxBox2d(); - + // init box2d with hz (fps) gravity x/y void init(float _hz=60.0f, float _gx=0.0f, float _gy=10.0f); - + // clear all bodies, joints void clear(); - + // set the hertz (fps) this will update the // iteration cycles for particle systems. void setFPS(float _hz); - + // world scale //------------------------------------------------ static void setScale(float s); static float getScale(); - + // helpers of -> box2d //------------------------------------------------ - static ofVec2f toOf(const b2Vec2 v); - static ofVec2f toOf(float x, float y); + static glm::vec2 toOf(const b2Vec2 v); + static glm::vec2 toOf(float x, float y); static float toOf(float f); - - static b2Vec2 toB2d(ofPoint pt); + + static b2Vec2 toB2d(glm::vec2 pt); static b2Vec2 toB2d(float x, float y); static float toB2d(float f); - + // helper to get particle iterations based on gravity radius and hz // this is from liquidfun docs. int calculateParticleIterations(float32 gravity, float32 radius, float32 timeStep); - - + + // touching and mouse events #ifdef TARGET_OPENGLES void touchDown(ofTouchEventArgs &touch); @@ -127,58 +127,58 @@ class ofxBox2d : public b2ContactListener { void mouseDragged(ofMouseEventArgs &e); void mouseReleased(ofMouseEventArgs &e); #endif - + // grabbing shapes void registerGrabbing(); void grabShapeDown(float x, float y, int id = -1 ); // -1 is reserved for mouse. void grabShapeUp(float x, float y, int id = -1 ); // -1 is reserved for mouse. void grabShapeDragged(float x, float y, int id = -1 ); // -1 is reserved for mouse. - + // get the main box2d world b2World * getWorld() { return world; } - + // number of bodies in the world int getBodyCount(); - + // number of joints in the world int getJointCount(); - + // get timestep float getTimeStep(); - + // grabbing of shapes with mouse void enableGrabbing() { bEnableGrabbing = true; }; void disableGrabbing() { bEnableGrabbing = false; }; - + // contact listening void setContactListener(ofxBox2dContactListener * listener); - + // wake up all shapes void wakeupShapes(); - + // set iterations for vel, and pos void setIterations(int velocityTimes, int positionTimes); - + // gravity void setGravityX(float x); void setGravityY(float y); void setGravity(float x, float y); - void setGravity(ofPoint pt); - ofPoint getGravity(); - + void setGravity(glm::vec2 pt); + glm::vec2 getGravity(); + // create bounds for the world - void setBounds(ofPoint lowBounds, ofPoint upBounds); + void setBounds(glm::vec2 lowBounds, glm::vec2 upBounds); void createBounds(float x=0, float y=0, float w=ofGetWidth(), float h=ofGetHeight()); void createBounds(ofRectangle rec); - + // make just a floor (bottom) - void createGround(const ofPoint & p1, const ofPoint & p2); + void createGround(const glm::vec2 & p1, const glm::vec2 & p2); void createGround(float x1=0, float y1=ofGetHeight(), float x2=ofGetWidth(), float y2=ofGetHeight()); void checkBounds(bool b); - + // main box2d cycle void update(); void draw(); void drawGround(); - + }; diff --git a/src/ofxBox2dBaseShape.cpp b/src/ofxBox2dBaseShape.cpp index 13a8cfa..314eade 100755 --- a/src/ofxBox2dBaseShape.cpp +++ b/src/ofxBox2dBaseShape.cpp @@ -12,11 +12,11 @@ //---------------------------------------- ofxBox2dBaseShape::ofxBox2dBaseShape() { - + setMassFromShape = true; alive = false; body = NULL; - + density = 0.0; bounce = 0.0; friction = 0.0; @@ -33,7 +33,7 @@ ofxBox2dBaseShape::~ofxBox2dBaseShape() { //------------------------------------------------ void ofxBox2dBaseShape::destroy() { - + if(getWorld() == NULL) { ofLog(OF_LOG_NOTICE, "ofxBox2dBaseShape:: - must have a valid world -"); return; @@ -42,7 +42,7 @@ void ofxBox2dBaseShape::destroy() { ofLog(OF_LOG_NOTICE, "ofxBox2dBaseShape:: - null body -"); return; } - + getWorld()->DestroyBody(body); body = NULL; alive = false; @@ -72,7 +72,7 @@ bool ofxBox2dBaseShape::isSleeping() { if(isBody()) { return !body->IsAwake(); } - else { + else { ofLog(OF_LOG_ERROR, "ofxBox2dBaseShape:: - body is not defined -"); return false; } @@ -88,7 +88,7 @@ b2World* ofxBox2dBaseShape::getWorld() { //---------------------------------------- void ofxBox2dBaseShape::create() {} -//------------------------------------------------ +//------------------------------------------------ void ofxBox2dBaseShape::setBounce(float val) { bounce = val; if (body) { @@ -98,7 +98,7 @@ void ofxBox2dBaseShape::setBounce(float val) { } } -//------------------------------------------------ +//------------------------------------------------ void ofxBox2dBaseShape::setDensity(float val) { density = val; if (body) { @@ -118,7 +118,7 @@ void ofxBox2dBaseShape::setFriction(float val) { } } -//------------------------------------------------ +//------------------------------------------------ void ofxBox2dBaseShape::setPhysics(float density, float bounce, float friction) { setFriction(friction); setDensity(density); @@ -126,14 +126,14 @@ void ofxBox2dBaseShape::setPhysics(float density, float bounce, float friction) } -//------------------------------------------------ +//------------------------------------------------ void* ofxBox2dBaseShape::setData(void*data) { - + if(data == NULL) { ofLog(OF_LOG_NOTICE, "ofxBox2dBaseShape:: - data is NULL -"); return NULL; } - + if(isBody()) { //ofLog(OF_LOG_NOTICE, "ofxBox2dBaseShape:: - custom data set %p", data); body->SetUserData(data); @@ -145,7 +145,7 @@ void* ofxBox2dBaseShape::setData(void*data) { return NULL; } -//------------------------------------------------ +//------------------------------------------------ void* ofxBox2dBaseShape::getData() { if(body) { return body->GetUserData(); @@ -163,7 +163,7 @@ void ofxBox2dBaseShape::setFilterData(b2Filter filter) { } } -//------------------------------------------------ +//------------------------------------------------ void ofxBox2dBaseShape::enableGravity(bool b) { if (body) { body->SetGravityScale(b ? 1 : 0); @@ -173,7 +173,7 @@ void ofxBox2dBaseShape::enableGravity(bool b) { } } -//------------------------------------------------ +//------------------------------------------------ void ofxBox2dBaseShape::setFixedRotation(bool b) { if(body) { body->SetFixedRotation(b); @@ -192,11 +192,11 @@ float ofxBox2dBaseShape::getRotation() { } void ofxBox2dBaseShape::setRotation(float angle){ - body->SetTransform(body->GetWorldCenter(), DEG_TO_RAD * angle); + body->SetTransform(body->GetWorldCenter(), glm::radians( angle )); } -//------------------------------------------------ +//------------------------------------------------ void ofxBox2dBaseShape::setPosition(float x, float y) { if(!body || body == NULL) { ofLog(OF_LOG_NOTICE, "ofxBox2dBaseShape:: - Body is NULL -"); @@ -206,21 +206,21 @@ void ofxBox2dBaseShape::setPosition(float x, float y) { body->SetAwake(true); } -void ofxBox2dBaseShape::setPosition(ofVec2f p) { +void ofxBox2dBaseShape::setPosition(glm::vec2 p) { setPosition(p.x, p.y); } //------------------------------------------------ -ofVec2f ofxBox2dBaseShape::toOf(const b2Vec2 v){ +glm::vec2 ofxBox2dBaseShape::toOf(const b2Vec2 v){ float scale = ofxBox2d::getScale(); - return ofVec2f(v.x * scale, v.y * scale); + return glm::vec2(v.x * scale, v.y * scale); } float ofxBox2dBaseShape::toOf(const float f) { float scale = ofxBox2d::getScale(); return f * scale; } -b2Vec2 ofxBox2dBaseShape::toB2d(const ofVec2f pt) { +b2Vec2 ofxBox2dBaseShape::toB2d(const glm::vec2 pt) { float scale = ofxBox2d::getScale(); return b2Vec2(pt.x / scale, pt.y / scale); } @@ -230,9 +230,9 @@ float ofxBox2dBaseShape::toB2d(const float f) { } -//------------------------------------------------ -ofVec2f ofxBox2dBaseShape::getPosition() { - ofVec2f p; +//------------------------------------------------ +glm::vec2 ofxBox2dBaseShape::getPosition() { + glm::vec2 p; if(body != NULL) { const b2Transform& xf = body->GetTransform(); b2Vec2 pos = body->GetLocalCenter(); @@ -243,11 +243,11 @@ ofVec2f ofxBox2dBaseShape::getPosition() { } //------------------------------------------------ -ofVec2f ofxBox2dBaseShape::getB2DPosition() { +glm::vec2 ofxBox2dBaseShape::getB2DPosition() { return getPosition() / ofxBox2d::getScale(); } -//------------------------------------------------ +//------------------------------------------------ void ofxBox2dBaseShape::setVelocity(float x, float y) { if(body != NULL) { body->SetLinearVelocity(b2Vec2(x, y)); @@ -256,11 +256,11 @@ void ofxBox2dBaseShape::setVelocity(float x, float y) { bodyDef.linearVelocity = b2Vec2(x, y); } } -void ofxBox2dBaseShape::setVelocity(ofVec2f p) { +void ofxBox2dBaseShape::setVelocity(glm::vec2 p) { setVelocity(p.x, p.y); } -ofVec2f ofxBox2dBaseShape::getVelocity() { - return ofVec2f(body->GetLinearVelocity().x, body->GetLinearVelocity().y); +glm::vec2 ofxBox2dBaseShape::getVelocity() { + return glm::vec2(body->GetLinearVelocity().x, body->GetLinearVelocity().y); } //------------------------------------------------ @@ -284,7 +284,7 @@ void ofxBox2dBaseShape::setAngularDamping(float f) { } //------------------------------------------------ -void ofxBox2dBaseShape::addForce(ofVec2f frc, float scale) { +void ofxBox2dBaseShape::addForce(glm::vec2 frc, float scale) { if(body != NULL) { frc *= scale; body->ApplyForce(b2Vec2(frc.x, frc.y), body->GetPosition(), true); @@ -292,17 +292,17 @@ void ofxBox2dBaseShape::addForce(ofVec2f frc, float scale) { } //------------------------------------------------ -void ofxBox2dBaseShape::addImpulseForce(ofVec2f point, ofVec2f force) { +void ofxBox2dBaseShape::addImpulseForce(glm::vec2 point, glm::vec2 force) { if(body != NULL) { body->ApplyLinearImpulse(b2Vec2(force.x, force.y), toB2d(point), true); } } //------------------------------------------------ -void ofxBox2dBaseShape::addRepulsionForce(ofVec2f pt, float radius, float amt) { +void ofxBox2dBaseShape::addRepulsionForce(glm::vec2 pt, float radius, float amt) { if(body != NULL) { b2Vec2 P = toB2d(pt); - b2Vec2 D = P - body->GetPosition(); + b2Vec2 D = P - body->GetPosition(); if(D.LengthSquared() < radius) {; P.Normalize(); b2Vec2 F = amt * D; diff --git a/src/ofxBox2dBaseShape.h b/src/ofxBox2dBaseShape.h index 878db9e..c6e7cb2 100755 --- a/src/ofxBox2dBaseShape.h +++ b/src/ofxBox2dBaseShape.h @@ -5,115 +5,109 @@ #include "ofxBox2dUtils.h" class ofxBox2dBaseShape { - + public: b2FixtureDef fixture; b2BodyDef bodyDef; b2Body* body; string name = "unset"; - + bool alive; bool setMassFromShape; - + float density; float bounce; float friction; - ofxBox2dBaseShape(); - + ofxBox2dBaseShape(); + //---------------------------------------- ~ofxBox2dBaseShape(); - + //---------------------------------------- bool isBody(); - + //---------------------------------------- static bool shouldRemove(shared_ptr shape); static bool shouldRemoveOffScreen(shared_ptr shape); bool isFixed(); bool isSleeping(); - + //---------------------------------------- b2World* getWorld(); - + //---------------------------------------- virtual void create(); - - //------------------------------------------------ + + //------------------------------------------------ virtual void setBounce(float val); - - //------------------------------------------------ + + //------------------------------------------------ virtual void setDensity(float val); - + //---------------------------------------- virtual void setFriction(float val); - - //------------------------------------------------ + + //------------------------------------------------ virtual void setPhysics(float density, float bounce, float friction); - - //------------------------------------------------ + + //------------------------------------------------ void* setData(void*data); - - //------------------------------------------------ + + //------------------------------------------------ void* getData(); - - //------------------------------------------------ + + //------------------------------------------------ virtual void setFilterData(b2Filter filter); - - //------------------------------------------------ + + //------------------------------------------------ virtual void enableGravity(bool b); - - //------------------------------------------------ + + //------------------------------------------------ virtual void setFixedRotation(bool b); float getRotation(); void setRotation(float angle); - - //------------------------------------------------ + + //------------------------------------------------ virtual void setPosition(float x, float y); - virtual void setPosition(ofVec2f p); - - //------------------------------------------------ - ofVec2f getPosition(); - ofVec2f getB2DPosition(); - - //------------------------------------------------ - ofVec2f toOf(const b2Vec2 v); + virtual void setPosition(glm::vec2 p); + + //------------------------------------------------ + glm::vec2 getPosition(); + glm::vec2 getB2DPosition(); + + //------------------------------------------------ + glm::vec2 toOf(const b2Vec2 v); float toOf(const float f); - - b2Vec2 toB2d(const ofVec2f pt); + + b2Vec2 toB2d(const glm::vec2 pt); float toB2d(const float f); - - //------------------------------------------------ + + //------------------------------------------------ virtual void setVelocity(float x, float y); - virtual void setVelocity(ofVec2f p); - ofVec2f getVelocity(); - + virtual void setVelocity(glm::vec2 p); + glm::vec2 getVelocity(); + //------------------------------------------------ virtual void setLinearDamping(float f); virtual void setAngularDamping(float f); - - - + + + //------------------------------------------------ - virtual void addForce(ofVec2f frc, float scale); - + virtual void addForce(glm::vec2 frc, float scale); + //------------------------------------------------ - virtual void addImpulseForce(ofVec2f point, ofVec2f force); - + virtual void addImpulseForce(glm::vec2 point, glm::vec2 force); + //------------------------------------------------ - virtual void addRepulsionForce(ofVec2f pt, float radius, float amt); - + virtual void addRepulsionForce(glm::vec2 pt, float radius, float amt); + //------------------------------------------------ virtual void destroy(); - + //------------------------------------------------ virtual void update(); virtual void draw(); - -}; - - - - - +}; diff --git a/src/ofxBox2dCircle.cpp b/src/ofxBox2dCircle.cpp index 671f0ff..fad8b92 100755 --- a/src/ofxBox2dCircle.cpp +++ b/src/ofxBox2dCircle.cpp @@ -16,98 +16,98 @@ ofxBox2dCircle::ofxBox2dCircle() { //------------------------------------------------ void ofxBox2dCircle::setup(b2World * b2dworld, float x, float y, float radius, bool is_sensor) { - - + + if(b2dworld == NULL) { ofLog(OF_LOG_NOTICE, "ofxBox2dCircle :: setup : - must have a valid world -"); return; } - + float scale = ofxBox2d::getScale(); - + // these are used to create the shape b2CircleShape shape; - + shape.m_p.SetZero(); shape.m_radius = radius / scale; this->radius = radius; - + fixture.shape = &shape; fixture.density = density; fixture.friction = friction; fixture.restitution = bounce; fixture.isSensor = is_sensor; - + if(density == 0.f) bodyDef.type = b2_staticBody; else bodyDef.type = b2_dynamicBody; - + bodyDef.position.Set(x/scale, y/scale); - + body = b2dworld->CreateBody(&bodyDef); body->CreateFixture(&fixture); - + alive = true; } //------------------------------------------------ -void ofxBox2dCircle::setup(b2World * b2dworld, ofVec2f &pts, float radius) { +void ofxBox2dCircle::setup(b2World * b2dworld, glm::vec2 &pts, float radius) { setup(b2dworld, pts.x, pts.y, radius); } //------------------------------------------------ void ofxBox2dCircle::addRepulsionForce(float x, float y, float amt) { - addRepulsionForce(ofVec2f(x, y), amt); + addRepulsionForce(glm::vec2(x, y), amt); } //------------------------------------------------ -void ofxBox2dCircle::addRepulsionForce(ofVec2f pt, float amt) { +void ofxBox2dCircle::addRepulsionForce(glm::vec2 pt, float amt) { const b2Transform& xf = body->GetTransform(); b2Vec2 P = toB2d(pt); - + float cx = toB2d(body->GetPosition().x); float cy = toB2d(body->GetPosition().y); float r = toB2d(getRadius()); - float ori = DEG_TO_RAD * getRotation(); - - b2Vec2 A(cx,cy); + float ori = glm::radians( getRotation() ); + + b2Vec2 A(cx,cy); b2Vec2 B(cx+r*cos(ori), cy+r*sin(ori)); - + b2Vec2 qtA = b2Mul(xf, A); b2Vec2 qtB = b2Mul(xf, B); - b2Vec2 DA = P - qtA; + b2Vec2 DA = P - qtA; b2Vec2 DB = P - qtB; b2Vec2 FA = amt * DA; b2Vec2 FB = amt * DB; - + body->ApplyForce(-FA, P, true); body->ApplyForce(-FB, P, true); } //------------------------------------------------ void ofxBox2dCircle::addAttractionPoint(float x, float y, float amt) { - addAttractionPoint(ofVec2f(x, y), amt); + addAttractionPoint(glm::vec2(x, y), amt); } //------------------------------------------------ -void ofxBox2dCircle::addAttractionPoint(ofVec2f pt, float amt) { +void ofxBox2dCircle::addAttractionPoint(glm::vec2 pt, float amt) { const b2Transform& xf = body->GetTransform(); b2Vec2 P = toB2d(pt); - + float cx = toB2d(body->GetPosition().x); float cy = toB2d(body->GetPosition().y); float r = toB2d(getRadius()); - float ori = DEG_TO_RAD * getRotation(); - - b2Vec2 A(cx,cy); + float ori = glm::radians( getRotation() ); + + b2Vec2 A(cx,cy); b2Vec2 B(cx+r*cos(ori), cy+r*sin(ori)); - + b2Vec2 qtA = b2Mul(xf, A); b2Vec2 qtB = b2Mul(xf, B); - b2Vec2 DA = P - qtA; + b2Vec2 DA = P - qtA; b2Vec2 DB = P - qtB; b2Vec2 FA = amt * DA; b2Vec2 FB = amt * DB; - + body->ApplyForce(FA, P, true); body->ApplyForce(FB, P, true); } @@ -117,22 +117,22 @@ float ofxBox2dCircle::getRadius() { return radius; } -//------------------------------------------------ +//------------------------------------------------ /* TODO: Should we even do this....? Im not sure about this it seems like a bad idea. I cant figure out another way to change the radius of a shape that we have - + -- any help here :) -- - + here is a solution for changing the radius on the fly without destroying the shape - chrispie - + */ void ofxBox2dCircle::setRadius(float r) { this->radius = r; - + for (b2Fixture* f= body->GetFixtureList(); f; f = f->GetNext()) { f->GetShape()->m_radius = toB2d(r); } @@ -140,14 +140,14 @@ void ofxBox2dCircle::setRadius(float r) { //------------------------------------------------ void ofxBox2dCircle::draw() { - + if(!isBody()) return; - + ofPushMatrix(); ofTranslate(getPosition().x, getPosition().y, 0); ofRotateDeg(getRotation(), 0, 0, 1); ofDrawCircle(0, 0, radius); - + ofPushStyle(); ofEnableAlphaBlending(); ofSetColor(0); @@ -157,21 +157,7 @@ void ofxBox2dCircle::draw() { ofDrawCircle(0, 0, radius); } ofPopStyle(); - - ofPopMatrix(); - -} - - - - - - - - - - - - + ofPopMatrix(); +} diff --git a/src/ofxBox2dCircle.h b/src/ofxBox2dCircle.h index b8093b4..698fd3e 100755 --- a/src/ofxBox2dCircle.h +++ b/src/ofxBox2dCircle.h @@ -5,46 +5,32 @@ #include "ofxBox2dBaseShape.h" class ofxBox2dCircle : public ofxBox2dBaseShape { - + private: - + float radius; - + public: - + //------------------------------------------------ ofxBox2dCircle(); - + //------------------------------------------------ void setup(b2World * b2dworld, float x, float y, float radius, bool is_sensor = false); - void setup(b2World * b2dworld, ofVec2f &pts, float radius); - + void setup(b2World * b2dworld, glm::vec2 &pts, float radius); + //------------------------------------------------ float getRadius(); void setRadius(float r); - + //------------------------------------------------ virtual void draw(); - + //------------------------------------------------ void addAttractionPoint(float x, float y, float amt=1); - void addAttractionPoint(ofVec2f pt, float amt=1); - + void addAttractionPoint(glm::vec2 pt, float amt=1); + void addRepulsionForce(float x, float y, float amt); - void addRepulsionForce(ofVec2f pt, float amt); + void addRepulsionForce(glm::vec2 pt, float amt); }; - - - - - - - - - - - - - - diff --git a/src/ofxBox2dContactListener.h b/src/ofxBox2dContactListener.h index 3d3e664..317e331 100755 --- a/src/ofxBox2dContactListener.h +++ b/src/ofxBox2dContactListener.h @@ -5,30 +5,27 @@ /* - *** THIS NEEDS WORK! *** + *** THIS NEEDS WORK! *** */ class ofxBox2dContactListener : public b2ContactListener { - + public: virtual void BeginContact(b2Contact* contact) { } - + virtual void EndContact(b2Contact* contact) { } ofxBox2dContactListener() { } - - - - virtual void contactAdd(ofPoint p) { - } - virtual void contactRemove(ofPoint p) { - } -}; + virtual void contactAdd(glm::vec2 p) { + } + virtual void contactRemove(glm::vec2 p) { + } +}; diff --git a/src/ofxBox2dConvexPoly.cpp b/src/ofxBox2dConvexPoly.cpp index 8036065..2286cd9 100755 --- a/src/ofxBox2dConvexPoly.cpp +++ b/src/ofxBox2dConvexPoly.cpp @@ -12,7 +12,7 @@ #include "ofxBox2d.h" ////---------------------------------------- -//ofxBox2dConvexPoly::~ofxBox2dPolygon() { +//ofxBox2dConvexPoly::~ofxBox2dPolygon() { //} // ////---------------------------------------- @@ -27,42 +27,45 @@ ofxBox2dConvexPoly::ofxBox2dConvexPoly() { //------------------------------------------------ void ofxBox2dConvexPoly::setup(b2World * b2dworld, ofPolyline & _line){ - + float scale = ofxBox2d::getScale(); - + ofPolyline line = ofxBox2dPolygonUtils::getConvexHull(_line); line.getVertices().erase(line.getVertices().end()-1); - - + + b2Vec2 * vertices; - int32 vertexCount = line.getVertices().size(); + std::size_t vertexCount = line.getVertices().size(); vertices = new b2Vec2[vertexCount]; - ofPoint pos; + glm::vec2 pos; ghettoRadius = 0; for (int i = 0; i < vertexCount; i++){ vertices[i].x = line.getVertices()[i].x; vertices[i].y = line.getVertices()[i].y; pos.x += line.getVertices()[i].x; pos.y += line.getVertices()[i].y; - + } pos /= (float)vertexCount; - + +// Invalid operands to binary expression ('glm::vec2' (aka 'vec<2, float, defaultp>') and 'value_type' (aka 'glm::vec<3, float>')) + for (int i = 0; i < vertexCount; i++){ - float dist = (pos - line.getVertices()[i]).length(); + float dist = glm::distance(glm::vec3(pos, 0.0f), line.getVertices()[i]); +// float dist = (pos - line.getVertices()[i]).length(); if (dist > ghettoRadius){ ghettoRadius = dist; } } - + for (int i = 0; i < vertexCount; i++){ vertices[i].x /= scale; vertices[i].y /= scale; } - - ofPoint posCent = ofPoint(200,200) - pos; - + + glm::vec2 posCent = glm::vec2(200,200) - pos; + pos /= scale; posCent /= scale; ghettoRadius /= scale; @@ -76,75 +79,75 @@ void ofxBox2dConvexPoly::setup(b2World * b2dworld, ofPolyline & _line){ path.lineTo(cur); } gpuCachedTesselation = path.getTessellation(); - - + + float x = pos.x ; float y = pos.y ; - + if(b2dworld == NULL) { ofLog(OF_LOG_NOTICE, "ofxBox2dConvexPoly :: setup : - must have a valid world -"); return; } - + // these are used to create the shape - + shape.Set(vertices, vertexCount); - + delete vertices; - + fixture.shape = &shape; fixture.density = density; fixture.friction = friction; fixture.restitution = bounce; - + if(density == 0.f) bodyDef.type = b2_staticBody; else bodyDef.type = b2_dynamicBody; - + bodyDef.position.Set(x,y); - + body = b2dworld->CreateBody(&bodyDef); body->CreateFixture(&fixture); - + scale = 1; } //------------------------------------------------ void ofxBox2dConvexPoly::setScale(float _scale){ - - + + if(!isBody()) return; - + b2Fixture* fix = body->GetFixtureList(); - + scale = _scale; - + b2PolygonShape* shape = (b2PolygonShape*) fix->GetShape(); - + for (int i = 0; i < polyPts.size(); i++){ - shape->m_vertices[i].Set(polyPts[i].x*scale, polyPts[i].y*scale); + shape->m_vertices[i].Set(polyPts[i].x*scale, polyPts[i].y*scale); } - - + + } -void ofxBox2dConvexPoly::addAttractionPoint (ofVec2f pt, float amt) { - // we apply forces at each vertex. +void ofxBox2dConvexPoly::addAttractionPoint (glm::vec2 pt, float amt) { + // we apply forces at each vertex. if(body != NULL) { const b2Transform& xf = body->GetTransform(); - + for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext()) { b2PolygonShape* poly = (b2PolygonShape*)f->GetShape(); - + if(poly) { b2Vec2 P = toB2d(pt); for(int i=0; iGetVertexCount(); i++) { b2Vec2 qt = b2Mul(xf, poly->GetVertex(i)); - b2Vec2 D = P - qt; + b2Vec2 D = P - qt; b2Vec2 F = amt * D; body->ApplyForce(F, P, true); - } + } } } } @@ -153,31 +156,31 @@ void ofxBox2dConvexPoly::addAttractionPoint (ofVec2f pt, float amt) { //---------------------------------------- void ofxBox2dConvexPoly::addAttractionPoint (float x, float y, float amt) { - addAttractionPoint(ofVec2f(x, y), amt); + addAttractionPoint(glm::vec2(x, y), amt); } //---------------------------------------- void ofxBox2dConvexPoly::addRepulsionForce(float x, float y, float amt) { - addRepulsionForce(ofVec2f(x, y), amt); + addRepulsionForce(glm::vec2(x, y), amt); } -void ofxBox2dConvexPoly::addRepulsionForce(ofVec2f pt, float amt) { - // we apply forces at each vertex. +void ofxBox2dConvexPoly::addRepulsionForce(glm::vec2 pt, float amt) { + // we apply forces at each vertex. if(body != NULL) { const b2Transform& xf = body->GetTransform(); - + for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext()) { b2PolygonShape* poly = (b2PolygonShape*)f->GetShape(); - + if(poly) { - + b2Vec2 P = toB2d(pt); - + for(int i=0; iGetVertexCount(); i++) { b2Vec2 qt = b2Mul(xf, poly->GetVertex(i)); - b2Vec2 D = P - qt; + b2Vec2 D = P - qt; b2Vec2 F = amt * D; body->ApplyForce(-F, P, true); - } + } } } } @@ -197,4 +200,3 @@ void ofxBox2dConvexPoly::draw() { gpuCachedTesselation.draw(); ofPopMatrix(); } - diff --git a/src/ofxBox2dConvexPoly.h b/src/ofxBox2dConvexPoly.h index af87fe5..440455a 100755 --- a/src/ofxBox2dConvexPoly.h +++ b/src/ofxBox2dConvexPoly.h @@ -5,52 +5,38 @@ #include "ofxBox2dBaseShape.h" class ofxBox2dConvexPoly : public ofxBox2dBaseShape { - + private: - + float radius; - + public: - + ~ofxBox2dConvexPoly(){}; //------------------------------------------------ ofxBox2dConvexPoly(); - + //------------------------------------------------ void setup(b2World * b2dworld, ofPolyline & line); - - + + void setScale(float scale); - + //------------------------------------------------ virtual void draw(); //------------------------------------------------ void addAttractionPoint(float x, float y, float amt=1); - void addAttractionPoint(ofVec2f pt, float amt=1); - + void addAttractionPoint(glm::vec2 pt, float amt=1); + void addRepulsionForce(float x, float y, float amt); - void addRepulsionForce(ofVec2f pt, float amt); - + void addRepulsionForce(glm::vec2 pt, float amt); + float ghettoRadius; - + b2PolygonShape shape; ofVboMesh gpuCachedTesselation; ofPolyline polyPts; float scale; - -}; - - - - - - - - - - - - - +}; diff --git a/src/ofxBox2dEdge.cpp b/src/ofxBox2dEdge.cpp index 03bb7ea..3a19c5f 100755 --- a/src/ofxBox2dEdge.cpp +++ b/src/ofxBox2dEdge.cpp @@ -24,24 +24,24 @@ void ofxBox2dEdge::destroy() { //---------------------------------------- void ofxBox2dEdge::create(b2World * b2dworld) { - + bFlagShapeUpdate = false; - + if(size() < 2) { printf("need at least 3 points\n"); return; } - + if (body != NULL) { b2dworld->DestroyBody(body); body = NULL; } - + // create the body from the world (1) b2BodyDef bd; bd.type = density <= 0.0 ? b2_staticBody : b2_dynamicBody; body = b2dworld->CreateBody(&bd); - + vector&pts = ofPolyline::getVertices(); for(int i=1; i<(int)size(); i++) { b2EdgeShape edge; @@ -52,9 +52,9 @@ void ofxBox2dEdge::create(b2World * b2dworld) { mesh.setUsage(body->GetType()==b2_staticBody?GL_STATIC_DRAW:GL_DYNAMIC_DRAW); mesh.setMode(OF_PRIMITIVE_LINE_STRIP); for(int i=0; i<(int)size(); i++) { - mesh.addVertex(ofVec3f(pts[i].x, pts[i].y)); + mesh.addVertex({ pts[i].x, pts[i].y, 0.0f }); } - + flagHasChanged(); alive = true; } @@ -63,7 +63,7 @@ void ofxBox2dEdge::create(b2World * b2dworld) { These were in ofPolyline and now are gone? */ //---------------------------------------- -void ofxBox2dEdge::addVertexes(vector &pts) { +void ofxBox2dEdge::addVertexes(vector &pts) { for (int i=0; iGetType()==b2_staticBody?GL_STATIC_DRAW:GL_DYNAMIC_DRAW); mesh.setMode(OF_PRIMITIVE_LINE_STRIP); - + for (b2Fixture * f = body->GetFixtureList(); f; f = f->GetNext()) { b2EdgeShape * edge = (b2EdgeShape*)f->GetShape(); - + if(edge) { - - ofVec2f a(ofxBox2d::toOf(edge->m_vertex1)); - ofVec2f b(ofxBox2d::toOf(edge->m_vertex2)); - + + glm::vec2 a(ofxBox2d::toOf(edge->m_vertex1)); + glm::vec2 b(ofxBox2d::toOf(edge->m_vertex2)); + ofPolyline::addVertex(a.x, a.y, 0); ofPolyline::addVertex(b.x, b.y, 0); - + mesh.addVertex(glm::vec3(a.x, a.y, 0)); mesh.addVertex(glm::vec3(b.x, b.y, 0)); } } - + bFlagShapeUpdate = true; flagHasChanged(); } @@ -111,19 +111,10 @@ void ofxBox2dEdge::updateShape() { //---------------------------------------- void ofxBox2dEdge::draw() { if(body==NULL) return; - + if(!bFlagShapeUpdate && body->GetType() != b2_staticBody) { printf("Need to update shape first\n"); } mesh.draw(); bFlagShapeUpdate = false; } - - - - - - - - - diff --git a/src/ofxBox2dEdge.h b/src/ofxBox2dEdge.h index ad6d5fc..db6a437 100755 --- a/src/ofxBox2dEdge.h +++ b/src/ofxBox2dEdge.h @@ -13,20 +13,20 @@ #include "ofxBox2dPolygonUtils.h" class ofxBox2dEdge : public ofxBox2dBaseShape, public ofPolyline { - + private: bool bFlagShapeUpdate; - + public: ofVboMesh mesh; void addVertexes(ofPolyline &polyline); - void addVertexes(vector &pts); - + void addVertexes(vector &pts); + void clear(); void destroy(); - + void create(b2World * b2dworld); void updateShape(); void draw(); -}; \ No newline at end of file +}; diff --git a/src/ofxBox2dJoint.cpp b/src/ofxBox2dJoint.cpp index 82a915e..4a78c3e 100755 --- a/src/ofxBox2dJoint.cpp +++ b/src/ofxBox2dJoint.cpp @@ -37,18 +37,18 @@ ofxBox2dJoint::ofxBox2dJoint(b2World* b2world, b2DistanceJointDef jointDef) { //---------------------------------------- void ofxBox2dJoint::setup(b2World* b2world, b2Body* body1, b2Body* body2, float frequencyHz, float damping, bool bCollideConnected) { - + if(body1 == NULL || body2 == NULL) { ofLog(OF_LOG_NOTICE, "ofxBox2dJoint :: setup : - box2d body is NULL -"); return; } - + b2Vec2 a1, a2; a1 = body1->GetWorldCenter(); a2 = body2->GetWorldCenter(); - + setup(b2world, body1, body2, a1, a2, frequencyHz, damping, bCollideConnected); - + alive = true; } @@ -64,8 +64,8 @@ void ofxBox2dJoint::setup(b2World* b2world, b2Body* body1, b2Body* body2, b2Vec2 jointDef.Initialize(body1, body2, anchor1, anchor2); jointDef.collideConnected = bCollideConnected; jointDef.frequencyHz = frequencyHz; - jointDef.dampingRatio = damping; - + jointDef.dampingRatio = damping; + setup(b2world, jointDef); } @@ -73,9 +73,9 @@ void ofxBox2dJoint::setup(b2World* b2world, b2Body* body1, b2Body* body2, b2Vec2 void ofxBox2dJoint::setup(b2World* b2world, b2DistanceJointDef jointDef) { setWorld(b2world); - + joint = (b2DistanceJoint*)world->CreateJoint(&jointDef); - + alive = true; } @@ -83,19 +83,19 @@ void ofxBox2dJoint::setup(b2World* b2world, b2DistanceJointDef jointDef) { void ofxBox2dJoint::setupMouseJoint(b2World* b2world, b2Body* bodyMouse, b2Body* bodyObj, float frequencyHz, float damping) { jointType = e_mouseJoint; b2MouseJointDef jointDef; - + jointDef.bodyA = bodyMouse; jointDef.bodyB = bodyObj; jointDef.target = bodyObj->GetWorldCenter(); jointDef.maxForce = bodyObj->GetMass()*1000; jointDef.frequencyHz = frequencyHz; jointDef.dampingRatio = damping; - - + + setWorld(b2world); - + joint = world->CreateJoint(&jointDef); - + alive = true; } @@ -109,7 +109,7 @@ void ofxBox2dJoint::updateTarget() { //---------------------------------------- void ofxBox2dJoint::setWorld(b2World* w) { if(w == NULL) { - ofLog(OF_LOG_NOTICE, "ofxBox2dJoint :: setWorld : - box2d world needed -"); + ofLog(OF_LOG_NOTICE, "ofxBox2dJoint :: setWorld : - box2d world needed -"); return; } world = w; @@ -132,8 +132,8 @@ bool ofxBox2dJoint::isSetup() { //---------------------------------------- void ofxBox2dJoint::draw() { if(!alive) return; - ofVec2f p1 = ofxBox2d::toOf(joint->GetAnchorA()); - ofVec2f p2 = ofxBox2d::toOf(joint->GetAnchorB()); + glm::vec2 p1 = ofxBox2d::toOf(joint->GetAnchorA()); + glm::vec2 p2 = ofxBox2d::toOf(joint->GetAnchorB()); ofDrawLine(p1, p2); } @@ -190,9 +190,9 @@ float ofxBox2dJoint::getDamping() { //---------------------------------------- -ofVec2f ofxBox2dJoint::getReactionForce(float inv_dt) const { +glm::vec2 ofxBox2dJoint::getReactionForce(float inv_dt) const { b2Vec2 vec = getReactionForceB2D(inv_dt); - return ofVec2f(vec.x, vec.y); + return glm::vec2(vec.x, vec.y); } b2Vec2 ofxBox2dJoint::getReactionForceB2D(float inv_dt) const { if(joint) { @@ -206,7 +206,3 @@ float ofxBox2dJoint::getReactionTorque(float inv_dt) const { } return 0; } - - - - diff --git a/src/ofxBox2dJoint.h b/src/ofxBox2dJoint.h index f9f703e..87d3b85 100755 --- a/src/ofxBox2dJoint.h +++ b/src/ofxBox2dJoint.h @@ -7,62 +7,50 @@ #define BOX2D_DEFAULT_DAMPING 0.5 class ofxBox2dJoint { - + public: - + b2World * world; b2Joint * joint; b2JointType jointType = e_distanceJoint; bool alive; - + //---------------------------------------- ofxBox2dJoint(); ofxBox2dJoint(b2World* b2world, b2Body* body1, b2Body* body2, float frequencyHz=4.f, float damping=.5f, bool bCollideConnected=true); ofxBox2dJoint(b2World* b2world, b2Body* body1, b2Body* body2, b2Vec2 anchor1, b2Vec2 anchor2, float frequencyHz=4.f, float damping=.5f, bool bCollideConnected=true); ofxBox2dJoint(b2World* b2world, b2DistanceJointDef jointDef); - + //---------------------------------------- void setWorld(b2World * w); void setup(b2World* b2world, b2Body* body1, b2Body* body2, float frequencyHz=4.f, float damping=.5f, bool bCollideConnected=true); void setup(b2World* b2world, b2Body* body1, b2Body* body2, b2Vec2 anchor1, b2Vec2 anchor2, float frequencyHz=4.f, float damping=.5f, bool bCollideConnected=true); void setup(b2World* b2world, b2DistanceJointDef jointDef); void setupMouseJoint(b2World* b2world, b2Body* body1, b2Body* body2, float frequencyHz=4.f, float damping=.5f); - + //---------------------------------------- bool isSetup(); void draw(); void destroy(); - + //---------------------------------------- // Manipulating the length can lead to non-physical behavior when the frequency is zero. - + // distance joints only void setLength(float len); float getLength(); - + void setFrequency(float freq); float getFrequency(); - + void setDamping(float ratio); float getDamping(); - + // mouse joints only void updateTarget(); - + //---------------------------------------- - ofVec2f getReactionForce(float inv_dt) const; + glm::vec2 getReactionForce(float inv_dt) const; b2Vec2 getReactionForceB2D(float inv_dt) const; float getReactionTorque(float inv_dt) const; }; - - - - - - - - - - - - diff --git a/src/ofxBox2dParticleSystem.cpp b/src/ofxBox2dParticleSystem.cpp index 188093d..40ba48e 100644 --- a/src/ofxBox2dParticleSystem.cpp +++ b/src/ofxBox2dParticleSystem.cpp @@ -16,7 +16,7 @@ int nPairs = m_particleSystem->GetContactCount(); for(int i=0; iGetPositionBuffer()[pairs[i].GetIndexA()]; b2Vec2 b = m_particleSystem->GetPositionBuffer()[pairs[i].GetIndexB()]; - + connectionsMesh.addVertex(ofVec3f(a.x, a.y)); connectionsMesh.addVertex(ofVec3f(b.x, b.y)); } @@ -52,12 +52,12 @@ ParticleSystem::ParticleSystem() { //-------------------------------------------------------------- void ParticleSystem::init(b2World * _world, int _maxParticles) { - + pointSizeOffset = 1; - + // set the world from world = _world; - + /*if (particleSystem != NULL) { delete particleSystem; particleSystem = NULL; @@ -66,16 +66,16 @@ void ParticleSystem::init(b2World * _world, int _maxParticles) { // create the main particle system const b2ParticleSystemDef particleSystemDef; particleSystem = world->CreateParticleSystem(&particleSystemDef); - + // gravity scale particleSystem->SetGravityScale(1);//0.4f); - + // density particleSystem->SetDensity(1.2f); - + // default particle flag particleFlag = b2_waterParticle; - + // set max particles this will allocate the mesh // and setup the b2dparticles systems setMaxParticles(_maxParticles); @@ -95,26 +95,26 @@ b2Body * ParticleSystem::createBody(const b2BodyDef* def) { #pragma mark - add particles //-------------------------------------------------------------- void ParticleSystem::addParticle(float x, float y, uint32 flags) { - + b2ParticleDef pd; pd.position = ofxBox2d::toB2d(x, y); pd.flags = flags; - + particleSystem->CreateParticle(pd); } //-------------------------------------------------------------- void ParticleSystem::addParticleGroup(float x, float y, uint32 flags, float rad) { - + b2CircleShape shape; shape.m_p = ofxBox2d::toB2d(x, y); shape.m_radius = ofxBox2d::toB2d(rad); - + b2ParticleGroupDef pg; pg.shape = &shape; pg.flags = flags; pg.groupFlags = b2_rigidParticleGroup; - + particleSystem->CreateParticleGroup(pg); } @@ -123,7 +123,7 @@ void ParticleSystem::addParticleGroup(float x, float y, uint32 flags, float rad) void ParticleSystem::clearParticles() { for (int i=0; iGetParticleCount(); i++) { particleSystem->DestroyParticle(i); - mesh.setVertex(i, ofVec3f(-1000, -1000, 0)); + mesh.setVertex(i, { -1000, -1000, 0 }); } } @@ -133,9 +133,9 @@ int ParticleSystem::removeOutsideBounds(const ofRectangle &bounds) { int n = particleSystem->GetParticleCount(); b2Vec2 * positions = particleSystem->GetPositionBuffer(); for(int i=0; iDestroyParticle(i); count ++; } @@ -162,23 +162,23 @@ void ParticleSystem::setPointSizeOffsetPercent(float pct) { //-------------------------------------------------------------- void ParticleSystem::setMaxParticles(int count) { - + // set the max particles particleSystem->SetMaxParticleCount(count); - + mesh.clear(); - + for(int i=0; iGetParticleCount(); @@ -232,7 +232,7 @@ uint32 ParticleSystem::setParticleType(int type) { particleSystem->SetParticleFlags(i, particleFlag); } } - + return particleFlag; } @@ -254,13 +254,13 @@ int ParticleSystem::getTotalParticles() { } //-------------------------------------------------------------- -vector ParticleSystem::getPositions() { +vector ParticleSystem::getPositions() { float scale = ofxBox2d::getScale(); int particleCount = particleSystem->GetParticleCount(); b2Vec2 * pos = particleSystem->GetPositionBuffer(); - vector positions; + vector positions; for(int i=0; iGetParticleCount(); const b2Vec2 * pos = particleSystem->GetPositionBuffer(); - + // this is great! (how hard are the contacting eachother 0 - 1 // const float32 * weights = particleSystem->GetWeightBuffer(); mesh.clear(); for (int i=0; iGetType()) { case b2Shape::e_circle: { b2CircleShape* circle = (b2CircleShape*)fixture->GetShape(); - + b2Vec2 center = b2Mul(xf, circle->m_p); float32 radius = circle->m_radius; b2Vec2 axis = b2Mul(xf.q, b2Vec2(1.0f, 0.0f)); - + ofPushMatrix(); ofTranslate(center.x, center.y); ofDrawCircle(0, 0, radius*scaleFactor); ofPopMatrix(); } break; - + case b2Shape::e_edge: { b2EdgeShape* edge = (b2EdgeShape*)fixture->GetShape(); @@ -323,13 +323,13 @@ void ParticleSystem::drawShape(b2Fixture* fixture, const b2Transform& xf, const ofDrawLine(v1.x, v1.y, v2.x, v2.y); } break; - + case b2Shape::e_chain: { b2ChainShape* chain = (b2ChainShape*)fixture->GetShape(); int32 count = chain->m_count; const b2Vec2* vertices = chain->m_vertices; - + b2Vec2 v1 = b2Mul(xf, vertices[0]); for (int32 i = 1; i < count; ++i) { @@ -340,14 +340,14 @@ void ParticleSystem::drawShape(b2Fixture* fixture, const b2Transform& xf, const } } break; - + case b2Shape::e_polygon: { b2PolygonShape* poly = (b2PolygonShape*)fixture->GetShape(); int32 vertexCount = poly->m_count; b2Assert(vertexCount <= b2_maxPolygonVertices); b2Vec2 vertices[b2_maxPolygonVertices]; - + ofBeginShape(); for (int32 i = 0; i < vertexCount; ++i) { @@ -357,7 +357,7 @@ void ParticleSystem::drawShape(b2Fixture* fixture, const b2Transform& xf, const ofEndShape(true); } break; - + default: break; } @@ -365,14 +365,14 @@ void ParticleSystem::drawShape(b2Fixture* fixture, const b2Transform& xf, const //-------------------------------------------------------------- void ParticleSystem::drawShapes(float scaleFactor) { - + float scale = ofxBox2d::getScale(); - + ofPushStyle(); ofPushMatrix(); ofTranslate(0, 0); ofScale(scale, scale); - + // render anything in the box2d world b2Body * bodyList = world->GetBodyList(); for (b2Body* b = bodyList; b; b = b->GetNext()) { @@ -381,7 +381,7 @@ void ParticleSystem::drawShapes(float scaleFactor) { drawShape(f, xf, b2Color(0.5f, 0.9f, 0.5f), scaleFactor); } } - + ofPopMatrix(); ofPopStyle(); } @@ -389,48 +389,48 @@ void ParticleSystem::drawShapes(float scaleFactor) { //-------------------------------------------------------------- void ParticleSystem::draw() { float scale = ofxBox2d::getScale(); - + ofPushStyle(); ofPushMatrix(); ofTranslate(0, 0); ofScale(scale, scale); - + float particlePointSize = getRenderRadius(); glPointSize(particlePointSize); mesh.draw(); glPointSize(1); - + ofPopMatrix(); ofPopStyle(); - + } //-------------------------------------------------------------- void ParticleSystem::drawConnections(ofColor color, bool withWeights) { - + const b2Vec2 * positions = particleSystem->GetPositionBuffer(); - + /* const b2ParticleTriad * triads = particleSystem->GetTriads(); int count = particleSystem->GetTriadCount(); - - + + for (int i=0; iGetContacts(); int count = particleSystem->GetContactCount(); - + for (int i=0; i namespace ofxBox2dParticleSystem { - + class ParticleSystem { - + ofVboMesh mesh; b2World * world; b2ParticleSystem * particleSystem; - + // rendering offset for glPointSize 0-1 float pointSizeOffset; - + // for now I have a single particle flag // I want a better system to combine flags uint32 particleFlag; - + public: - + ParticleSystem(); - + // helpers - ofVec2f toVec2f(const b2Vec2& p) { - return ofVec2f(p.x, p.y); + glm::vec2 toVec2f(const b2Vec2& p) { + return glm::vec2(p.x, p.y); } - - ofVec2f toOf(const b2Vec2& p) { + + glm::vec2 toOf(const b2Vec2& p) { float scale = ofxBox2d::getScale(); - return ofVec2f(p.x * scale, p.y * scale); + return glm::vec2(p.x * scale, p.y * scale); } - + float toOf(const float v) { float scale = ofxBox2d::getScale(); return v * scale; } - - b2Vec2 toB2d(const ofVec2f& p) { + + b2Vec2 toB2d(const glm::vec2& p) { float scale = ofxBox2d::getScale(); return b2Vec2(p.x / scale, p.y / scale); } - + // init the system creates the world and system void init(b2World * _world, int _maxParticles=1000); - + // tick void tick(); - + // update the mesh with new data void updateMesh(); - + // render the mesh void draw(); - + // render all shapes void drawShapes(float scaleFactor=1); - + // render out connection void drawConnections(ofColor color = ofColor::white, bool withWeights=false); - + // render any shapes in the b2d world void drawShape(b2Fixture* fixture, const b2Transform& xf, const b2Color& color, float scaleFactor=1); // add a single particle void addParticle(float x, float y, uint32 flags=b2_waterParticle); - + // add a grouping of particles void addParticleGroup(float x, float y, uint32 flags=b2_waterParticle, float rad=50); - + // remove all particles void clearParticles(); - + // remove any particles outside bounds // return total removed int removeOutsideBounds(const ofRectangle &bounds); - + // remove the particle at index // this will update the mesh void removeParticle(int index); - + // set the radius of the particle system void setRadius(float radius); - + // set the particle type uint32 setParticleType(int type); - + // set max particles void setMaxParticles(int count); - + // set the offset of the glPointsize void setPointSizeOffsetPercent(float pct); - + // get all postion (scaled) to world - vector getPositions(); + vector getPositions(); // get radius (scaled) float getRadius(); - + // radius plus buffer float getRenderRadius(); - + // get total particles int getTotalParticles(); - + // helper to create a body b2Body * createBody(const b2BodyDef* def); - + // box2d world b2World * getWorld(); - + // liquid fun particle system b2ParticleSystem * getParticleSystem(); - + // these are the possible particle types static vector particleTypesNames; - + }; } - diff --git a/src/ofxBox2dPolygon.cpp b/src/ofxBox2dPolygon.cpp index 740cdb9..c18456c 100755 --- a/src/ofxBox2dPolygon.cpp +++ b/src/ofxBox2dPolygon.cpp @@ -12,7 +12,7 @@ #include "ofxBox2d.h" //---------------------------------------- -ofxBox2dPolygon::ofxBox2dPolygon() { +ofxBox2dPolygon::ofxBox2dPolygon() { bIsTriangulated = false; bIsSimplified = false; ofPolyline::setClosed(true); @@ -20,7 +20,7 @@ ofxBox2dPolygon::ofxBox2dPolygon() { } //---------------------------------------- -ofxBox2dPolygon::~ofxBox2dPolygon() { +ofxBox2dPolygon::~ofxBox2dPolygon() { } //---------------------------------------- @@ -45,7 +45,7 @@ void ofxBox2dPolygon::addTriangle(const glm::vec2 &a, const glm::vec2 &b, const //---------------------------------------- void ofxBox2dPolygon::simplify(float tolerance) { - ofPolyline::simplify(tolerance); + ofPolyline::simplify(tolerance); bIsSimplified = true; } @@ -60,9 +60,9 @@ void ofxBox2dPolygon::simplifyToMaxVerts() { //---------------------------------------- void ofxBox2dPolygon::triangulate(float angleConstraint, float sizeConstraint) { - + triangles.clear(); - + if(size() > 0) { triangles = ofxBox2dPolygonUtils::triangulate(*this, angleConstraint, sizeConstraint); } @@ -83,9 +83,9 @@ void ofxBox2dPolygon::create(b2World * b2dworld) { if(size() < 3) { ofLog(OF_LOG_NOTICE, "need at least 3 points: %i\n", (int)size()); - return; + return; } - + if (body != NULL) { b2dworld->DestroyBody(body); body = NULL; @@ -93,17 +93,17 @@ void ofxBox2dPolygon::create(b2World * b2dworld) { float scale = ofxBox2d::getScale(); auto center = getCentroid2D(); - + b2BodyDef bd; bd.type = density <= 0.0 ? b2_staticBody : b2_dynamicBody; body = b2dworld->CreateBody(&bd); if(bIsTriangulated) { - + b2PolygonShape shape; b2FixtureDef fixture; b2Vec2 verts[3]; - + // move all the triangles center offset for (auto &tri : triangles) { for(int i=0; i<3; i++) { @@ -111,24 +111,24 @@ void ofxBox2dPolygon::create(b2World * b2dworld) { tri[i].y -= center.y; } } - - + + for (auto &tri : triangles) { - + verts[0] = toB2d(tri.a); verts[1] = toB2d(tri.b); verts[2] = toB2d(tri.c); - + shape.Set(verts, 3); - + fixture.density = density; fixture.restitution = bounce; fixture.friction = friction; fixture.shape = &shape; - + body->CreateFixture(&fixture); } - + // move the body to the center body->SetTransform(toB2d(center), 0); @@ -143,38 +143,38 @@ void ofxBox2dPolygon::create(b2World * b2dworld) { } mesh = path.getTessellation(); mesh.setUsage(GL_STATIC_DRAW); - + } else { - + makeConvexPoly(); - + auto & pts = ofPolyline::getVertices(); vectorverts; - + // move all the points to 0, 0 for(auto &pnt : pts) { pnt -= center; } - + // double down safety - for (int i=0; iCreateFixture(&fixture); - + // move the body to the center body->SetTransform(toB2d(center), 0); - + // build the mesh mesh.clear(); ofPath path; @@ -185,29 +185,29 @@ void ofxBox2dPolygon::create(b2World * b2dworld) { mesh = path.getTessellation(); mesh.setUsage(GL_STATIC_DRAW); } - + flagHasChanged(); alive = true; } //------------------------------------------------ -void ofxBox2dPolygon::addAttractionPoint (ofVec2f pt, float amt) { - // we apply forces at each vertex. +void ofxBox2dPolygon::addAttractionPoint (glm::vec2 pt, float amt) { + // we apply forces at each vertex. if(body != NULL) { const b2Transform& xf = body->GetTransform(); - + for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext()) { b2PolygonShape* poly = (b2PolygonShape*)f->GetShape(); - + if(poly) { b2Vec2 P = toB2d(pt); - + for(int i=0; iGetVertexCount(); i++) { b2Vec2 qt = b2Mul(xf, poly->GetVertex(i)); - b2Vec2 D = P - qt; + b2Vec2 D = P - qt; b2Vec2 F = amt * D; body->ApplyForce(F, P, true); - } + } } } } @@ -216,32 +216,32 @@ void ofxBox2dPolygon::addAttractionPoint (ofVec2f pt, float amt) { //---------------------------------------- void ofxBox2dPolygon::addAttractionPoint (float x, float y, float amt) { - addAttractionPoint(ofVec2f(x, y), amt); + addAttractionPoint(glm::vec2(x, y), amt); } //---------------------------------------- void ofxBox2dPolygon::addRepulsionForce(float x, float y, float amt) { - addRepulsionForce(ofVec2f(x, y), amt); + addRepulsionForce(glm::vec2(x, y), amt); } //---------------------------------------- -void ofxBox2dPolygon::addRepulsionForce(ofVec2f pt, float amt) { - // we apply forces at each vertex. +void ofxBox2dPolygon::addRepulsionForce(glm::vec2 pt, float amt) { + // we apply forces at each vertex. if(body != NULL) { const b2Transform& xf = body->GetTransform(); - + for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext()) { b2PolygonShape* poly = (b2PolygonShape*)f->GetShape(); - + if(poly) { b2Vec2 P = toB2d(pt); - + for(int i=0; iGetVertexCount(); i++) { b2Vec2 qt = b2Mul(xf, poly->GetVertex(i)); - b2Vec2 D = P - qt; + b2Vec2 D = P - qt; b2Vec2 F = amt * D; body->ApplyForce(-F, P, true); - } + } } } } @@ -250,12 +250,12 @@ void ofxBox2dPolygon::addRepulsionForce(ofVec2f pt, float amt) { //---------------------------------------- vector & ofxBox2dPolygon::getPoints() { if(body != NULL) { - + const b2Transform& xf = body->GetTransform(); - + for (b2Fixture * f = body->GetFixtureList(); f; f = f->GetNext()) { b2PolygonShape * poly = (b2PolygonShape*)f->GetShape(); - + if(poly) { ofPolyline::clear(); for(int i=0; iGetVertexCount(); i++) { @@ -302,16 +302,3 @@ void ofxBox2dPolygon::drawTriangles() { } ofPopMatrix(); } - - - - - - - - - - - - - diff --git a/src/ofxBox2dPolygon.h b/src/ofxBox2dPolygon.h index 5715422..2d7dc63 100755 --- a/src/ofxBox2dPolygon.h +++ b/src/ofxBox2dPolygon.h @@ -7,51 +7,51 @@ class ofxBox2dPolygon : public ofxBox2dBaseShape, public ofPolyline { private: - + bool bIsSimplified; bool bIsTriangulated; float area; - ofVec2f center; - + glm::vec2 center; + void calculateCentroid(); float calculateArea(); void makeConvexPoly(); public: - + ofVboMesh mesh; ofRectangle bounds; vector triangles; - + //---------------------------------------- ofxBox2dPolygon(); ~ofxBox2dPolygon(); void destroy(); void clear(); - + //---------------------------------------- void addTriangle(const glm::vec2 &a, const glm::vec2 &b, const glm::vec2 &c); - + //---------------------------------------- // Polygon helper functions //---------------------------------------- void simplify(float tolerance=0.3); void simplifyToMaxVerts(); void triangulate(float angleConstraint = -1, float sizeConstraint = -1); - + //---------------------------------------- vector &getPoints(); bool isGoodShape() { return calculateArea() > 15; } bool isTriangulated() { return bIsTriangulated; } //------------------------------------------------ - void addAttractionPoint(ofVec2f pt, float amt=1); + void addAttractionPoint(glm::vec2 pt, float amt=1); void addAttractionPoint(float x, float y, float amt=1); void addRepulsionForce(float x, float y, float amt); - void addRepulsionForce(ofVec2f pt, float amt); - + void addRepulsionForce(glm::vec2 pt, float amt); + //---------------------------------------- void create(b2World * b2dworld); void draw(); void drawTriangles(); - + }; diff --git a/src/ofxBox2dPolygonUtils.cpp b/src/ofxBox2dPolygonUtils.cpp index 1942005..67fe673 100644 --- a/src/ofxBox2dPolygonUtils.cpp +++ b/src/ofxBox2dPolygonUtils.cpp @@ -16,12 +16,12 @@ void triangulatePoints(char * flags, triangulateio * in, triangulateio * mid, tr //------------------------------------------------------------------- vector ofxBox2dPolygonUtils::triangulate(ofPolyline contour, float angleConstraint, float sizeConstraint) { - + vector triangles; - vector outputPts; - + vector outputPts; + int bSize = contour.size(); - + struct triangulateio in, out; in.numberofpoints = bSize; in.numberofpointattributes = 0; @@ -29,12 +29,12 @@ vector ofxBox2dPolygonUtils::triangulate(ofPolyline contour, flo in.pointlist = (REAL *) malloc(bSize * 2 * sizeof(REAL)); in.numberofregions = 0; in.regionlist = NULL; - + for(int i = 0; i < bSize; i++) { in.pointlist[i*2+0] = contour[i].x; in.pointlist[i*2+1] = contour[i].y; } - + out.pointlist = (REAL *) NULL; out.pointattributelist = (REAL *) NULL; out.pointmarkerlist = (int *) NULL; @@ -45,34 +45,34 @@ vector ofxBox2dPolygonUtils::triangulate(ofPolyline contour, flo out.segmentmarkerlist = (int *) NULL; out.edgelist = (int *) NULL; out.edgemarkerlist = (int *) NULL; - - + + bool bConstrainAngle = false; bool bConstrainSize = false; - + if (angleConstraint > 0) bConstrainAngle = true; if (sizeConstraint > 0) bConstrainSize = true; - - - + + + string triangulateParams; - + triangulateParams += "z"; // start from zero triangulateParams += "Y"; // Prohibits the insertion of Steiner points (extra points) on the mesh boundary. triangulateParams += "Q"; // quiet! change to V is you want alot of info - + if (bConstrainAngle == true){ triangulateParams += "q" + ofToString( angleConstraint ); } - + if (bConstrainSize == true){ triangulateParams += "a" + ofToString( (int)sizeConstraint ); } - - + + triangulatePoints((char *) triangulateParams.c_str(), &in, &out, NULL); - - + + /* printf("Initial triangulation:\n\n"); //report(&mid, 1, 1, 1, 1, 1, 0); @@ -82,41 +82,41 @@ vector ofxBox2dPolygonUtils::triangulate(ofPolyline contour, flo printf(" %.6g", out.pointlist[i * 2 + j]); } printf("\n"); - + } */ - - + + int nTriangles = 0; triangles.clear(); - - - std::map < int , ofPoint > goodPts; - + + + std::map < int , glm::vec2 > goodPts; + for (int i = 0; i < out.numberoftriangles; i++) { TriangleShape triangle; - + int whichPt; - + for (int j = 0; j < 3; j++){ whichPt = out.trianglelist[i * 3 + j]; triangle[j] = glm::vec2( out.pointlist[ whichPt * 2 + 0], out.pointlist[ whichPt * 2 + 1]); triangle.index[j] = whichPt; } - - ofPoint tr[3]; - tr[0] = ofPoint(triangle[0].x, triangle[0].y); - tr[1] = ofPoint(triangle[1].x, triangle[1].y); - tr[2] = ofPoint(triangle[2].x, triangle[2].y); - + + glm::vec2 tr[3]; + tr[0] = glm::vec2(triangle[0].x, triangle[0].y); + tr[1] = glm::vec2(triangle[1].x, triangle[1].y); + tr[2] = glm::vec2(triangle[2].x, triangle[2].y); + // here we check if a triangle is "inside" a contour to drop non inner triangles auto center = getTriangleCenter(tr[0], tr[1], tr[2]); - + if(contour.inside(center.x, center.y)) { triangle.color = ofColor(ofRandom(0,255)); triangles.push_back(triangle); - - + + // store the good points in a map for (int j = 0; j < 3; j++){ goodPts[triangle.index[j]] = triangle[j]; @@ -124,23 +124,23 @@ vector ofxBox2dPolygonUtils::triangulate(ofPolyline contour, flo nTriangles++; } } - + // put all good points in a vector and handle the remapping of indices. // the indices stored above were for all points, but since we drop triangles, we // can drop non used points, and then remap all the indces. // that happens here: - + outputPts.clear(); std::map < int, int > indexChanges; - std::map< int , ofPoint >::iterator iter; + std::map< int , glm::vec2 >::iterator iter; for (iter = goodPts.begin(); iter != goodPts.end(); ++iter) { //cout << iter->first << " " << iter->second << endl; indexChanges[iter->first] = outputPts.size(); outputPts.push_back(iter->second); } - + // now, with the new, potentially smaller group of points, update all the indices of the triangles so their indices point right: - + for (int i = 0; i < triangles.size(); i++){ for (int j = 0; j < 3; j++){ triangles[i].index[j] = indexChanges[triangles[i].index[j]]; @@ -149,7 +149,7 @@ vector ofxBox2dPolygonUtils::triangulate(ofPolyline contour, flo triangles[i][j] = glm::vec2(outputPts[triangles[i].index[j]].x, outputPts[triangles[i].index[j]].y); } } - + ofLogNotice() << outputPts.size(); // now make a mesh, using indices: /* @@ -158,58 +158,58 @@ vector ofxBox2dPolygonUtils::triangulate(ofPolyline contour, flo for (int i = 0; i < outputPts.size(); i++){ triangulatedMesh.addVertex(outputPts[i]); } - + for (int i = 0; i < triangles.size(); i++){ triangulatedMesh.addIndex(triangles[i].index[0]);; triangulatedMesh.addIndex(triangles[i].index[1]);; triangulatedMesh.addIndex(triangles[i].index[2]);; }*/ - + // depending on flags, we may need to adjust some of the memory clearing // (see tricall.c for full listings) // TODO: this should be agressively tested. - + free(in.pointlist); free(out.pointlist); if (out.pointattributelist != NULL) free(out.pointattributelist); if (out.pointmarkerlist != NULL) free(out.pointmarkerlist); free(out.trianglelist); if (out.triangleattributelist != NULL) free(out.triangleattributelist); - + return triangles; } //------------------------------------------------------------------- -ofPoint ofxBox2dPolygonUtils::getTriangleCenter(ofPoint &a, ofPoint &b, ofPoint &c) { - ofPoint tr[3]; +glm::vec2 ofxBox2dPolygonUtils::getTriangleCenter(glm::vec2 &a, glm::vec2 &b, glm::vec2 &c) { + glm::vec2 tr[3]; tr[0] = a; tr[1] = b; tr[2] = c; return getTriangleCenter(tr); } //------------------------------------------------------------------- -ofPoint ofxBox2dPolygonUtils::getTriangleCenter(ofPoint *tr) { +glm::vec2 ofxBox2dPolygonUtils::getTriangleCenter(glm::vec2 *tr) { float c_x = (tr[0].x + tr[1].x + tr[2].x) / 3; float c_y = (tr[0].y + tr[1].y + tr[2].y) / 3; - return ofPoint(c_x, c_y); + return glm::vec2(c_x, c_y); } //------------------------------------------------------------------- -bool ofxBox2dPolygonUtils::isPointInsidePolygon(const ofPoint & p, const vector &polygon) { +bool ofxBox2dPolygonUtils::isPointInsidePolygon(const glm::vec2 & p, const vector &polygon) { if(polygon.size() < 1) return false; int counter = 0; int i; double xinters; - ofVec2f p1,p2; + glm::vec2 p1,p2; float x = p.x; float y = p.y; - int N = polygon.size(); - + std::size_t N = polygon.size(); + p1 = polygon[0]; for (i=1;i<=N;i++) { p2 = polygon[i % N]; - if (y > MIN(p1.y,p2.y)) { - if (y <= MAX(p1.y,p2.y)) { - if (x <= MAX(p1.x,p2.x)) { + if (y > std::min(p1.y,p2.y)) { + if (y <= std::max(p1.y,p2.y)) { + if (x <= std::max(p1.x,p2.x)) { if (p1.y != p2.y) { xinters = (y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x; if (p1.x == p2.x || x <= xinters) @@ -220,7 +220,7 @@ bool ofxBox2dPolygonUtils::isPointInsidePolygon(const ofPoint & p, const vector< } p1 = p2; } - + if (counter % 2 == 0) return false; else return true; } @@ -236,7 +236,7 @@ typedef long long CoordType; struct hPoint { CoordType x, y; - + bool operator <(const hPoint &p) const { return x < p.x || (x == p.x && y < p.y); } @@ -256,48 +256,48 @@ static CoordType cross(const hPoint &O, const hPoint &A, const hPoint &B) { static vector calcConvexHull(vector P) { int n = P.size(), k = 0; vector H(2*n); - + // Sort points lexicographically sort(P.begin(), P.end()); - + // Build lower hull for (int i = 0; i < n; i++) { while (k >= 2 && cross(H[k-2], H[k-1], P[i]) <= 0) k--; H[k++] = P[i]; } - + // Build upper hull for (int i = n-2, t = k+1; i >= 0; i--) { while (k >= t && cross(H[k-2], H[k-1], P[i]) <= 0) k--; H[k++] = P[i]; } - + H.resize(k); return H; } //------------------------------------------------------------------- ofPolyline ofxBox2dPolygonUtils::getConvexHull(vector&linePts){ - + vector < hPoint > ptsIn; - + for (size_t i = 0; i < linePts.size(); i++){ hPoint pt; pt.x = linePts[i].x; pt.y = linePts[i].y; - + ptsIn.push_back(pt); } vector < hPoint > ptsOut; - + ptsOut = calcConvexHull(ptsIn); - + ofPolyline outLine; - + for (size_t i = 0; i < ptsOut.size(); i++){ - outLine.addVertex(ofPoint(ptsOut[i].x, ptsOut[i].y)); + outLine.addVertex({ ptsOut[i].x, ptsOut[i].y, 0.0f }); } - + return outLine; } diff --git a/src/ofxBox2dPolygonUtils.h b/src/ofxBox2dPolygonUtils.h index d3822e1..03f9af6 100755 --- a/src/ofxBox2dPolygonUtils.h +++ b/src/ofxBox2dPolygonUtils.h @@ -36,15 +36,15 @@ class TriangleShape { class ofxBox2dPolygonUtils { public: - - + + // from zach ofxTriangleMesh // way better imp https://github.com/ofZach/ofxTriangleMesh static vector triangulate(ofPolyline contour, float angleConstraint = 28, float sizeConstraint = -1); - static bool isPointInsidePolygon(const ofPoint & p, const vector & polygon); + static bool isPointInsidePolygon(const glm::vec2 & p, const vector & polygon); static ofPolyline getConvexHull(vector&linePts); static ofPolyline getConvexHull(ofPolyline &line); - static ofPoint getTriangleCenter(ofPoint &a, ofPoint &b, ofPoint &c); - static ofPoint getTriangleCenter(ofPoint *tr); + static glm::vec2 getTriangleCenter(glm::vec2 &a, glm::vec2 &b, glm::vec2 &c); + static glm::vec2 getTriangleCenter(glm::vec2 *tr); }; diff --git a/src/ofxBox2dRect.cpp b/src/ofxBox2dRect.cpp index 43e9083..9a2c7e3 100755 --- a/src/ofxBox2dRect.cpp +++ b/src/ofxBox2dRect.cpp @@ -23,12 +23,12 @@ void ofxBox2dRect::setup(b2World * b2dworld, ofRectangle rec, float angle) { //------------------------------------------------ void ofxBox2dRect::setup(b2World * b2dworld, float x, float y, float w, float h, float angle) { - + if(b2dworld == NULL) { ofLog(OF_LOG_NOTICE, "- must have a valid world -"); return; } - + w /= 2; h /= 2; width = w; height = h; @@ -39,13 +39,13 @@ void ofxBox2dRect::setup(b2World * b2dworld, float x, float y, float w, float h, fixture.density = density; fixture.friction = friction; fixture.restitution = bounce; - + b2BodyDef bodyDef; if(density == 0.f) bodyDef.type = b2_staticBody; else bodyDef.type = b2_dynamicBody; bodyDef.position.Set(toB2d(x), toB2d(y)); bodyDef.angle = ofDegToRad(angle); - + body = b2dworld->CreateBody(&bodyDef); body->CreateFixture(&fixture); @@ -99,11 +99,11 @@ ofPolyline& ofxBox2dRect::getRectangleShape() { //------------------------------------------------ void ofxBox2dRect::addRepulsionForce(float fx, float fy, float amt) { - addRepulsionForce(ofVec2f(fx,fy), amt); + addRepulsionForce(glm::vec2(fx,fy), amt); } //------------------------------------------------ -void ofxBox2dRect::addRepulsionForce(ofVec2f pt, float amt) { +void ofxBox2dRect::addRepulsionForce(glm::vec2 pt, float amt) { if(body != NULL) { const b2Transform& xf = body->GetTransform(); for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext()) { @@ -128,11 +128,11 @@ void ofxBox2dRect::addRepulsionForce(ofVec2f pt, float amt) { // We compute the force for all four (transformed) corners of the rect. // This keeps the rect's orientation correct! void ofxBox2dRect::addAttractionPoint (float fx, float fy, float amt) { - addAttractionPoint(ofVec2f(fx, fy), amt); + addAttractionPoint(glm::vec2(fx, fy), amt); } //------------------------------------------------ -void ofxBox2dRect::addAttractionPoint (ofVec2f pt, float amt) { +void ofxBox2dRect::addAttractionPoint (glm::vec2 pt, float amt) { if(body != NULL) { const b2Transform& xf = body->GetTransform(); @@ -158,7 +158,7 @@ void ofxBox2dRect::draw() { if(body == NULL) { return; } - + ofPushMatrix(); ofTranslate(ofxBox2dBaseShape::getPosition()); ofRotateDeg(getRotation()); @@ -206,19 +206,3 @@ void ofxBox2dRect::draw() { } */ } - - - - - - - - - - - - - - - - diff --git a/src/ofxBox2dRect.h b/src/ofxBox2dRect.h index 815dbe7..792f458 100755 --- a/src/ofxBox2dRect.h +++ b/src/ofxBox2dRect.h @@ -11,42 +11,28 @@ class ofxBox2dRect : public ofxBox2dBaseShape { private: float width, height; - + public: ofVboMesh mesh; //------------------------------------------------ - ofxBox2dRect(); - + ofxBox2dRect(); + //------------------------------------------------ void setup(b2World * b2dworld, ofRectangle rec, float angle=0); void setup(b2World * b2dworld, float x, float y, float w, float h, float angle=0); - + //------------------------------------------------ float getWidth() { return width * 2; } float getHeight() { return height * 2; } - + void updateMesh(); void draw(); //------------------------------------------------ void addAttractionPoint(float fx, float fy, float amt); - void addAttractionPoint(ofVec2f pt, float amt); + void addAttractionPoint(glm::vec2 pt, float amt); void addRepulsionForce(float fx, float fy, float amt); - void addRepulsionForce(ofVec2f pt, float amt); + void addRepulsionForce(glm::vec2 pt, float amt); }; - - - - - - - - - - - - - - diff --git a/src/ofxBox2dRender.cpp b/src/ofxBox2dRender.cpp index 6c82a06..39dd8f0 100644 --- a/src/ofxBox2dRender.cpp +++ b/src/ofxBox2dRender.cpp @@ -17,22 +17,22 @@ void ofxBox2dRender::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, cons ofSetColor(color.r*255.0, color.g*255.0, color.b*255.0); ofBeginShape(); for (int i = 0; i < vertexCount; ++i) { - ofVec2f p = ofxBox2d::toOf(vertices[i]); + glm::vec2 p = ofxBox2d::toOf(vertices[i]); ofVertex(p.x, p.y); } ofEndShape(); } void ofxBox2dRender::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) { - + ofSetHexColor(0xffffff); ofBeginShape(); for(int i=0; i