@t.espinoza wrote:
Hi all! I'm trying to access the final circle in this ofxBox2d joint and have it rotate around my mouse; ultimately I want it to look as if it was coiling around it but I'm not sure how to get there. As I mentioned, I'm working with ofxBox2d to try to accomplish this.
I was thinking I could access it the same way I would access a specific element in any array, by just saying (in this case)
circles[30].get()->setPosition(x, y);
but I am getting a getting a green highlight that says
EXC_BAD_ACCESS (code=EXC-l386_GPFLT)
I tried to debug, and it was recommended I enable Zombie objects, but I don't think the output has changed. Why is this an incorrect method of attempting to do this?
Here is the code in the cpp file:
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup(){ ofSetVerticalSync(true); ofBackgroundHex(000000); ofSetLogLevel(OF_LOG_NOTICE); ofDisableAntiAliasing(); box2d.init(); box2d.setGravity(0, 0); box2d.createBounds(); box2d.setFPS(60.0); box2d.registerGrabbing(); // anchor.setup(box2d.getWorld(), ofGetWidth()/2, ofGetHeight()/2, 4); //add a few circles for (int i = 0; i < 30; i++) { shared_ptr<ofxBox2dCircle> circle = shared_ptr<ofxBox2dCircle>(new ofxBox2dCircle); circle.get()->setPhysics(3.0, 0.53, 0.1); circle.get()->setup(box2d.getWorld(), ofGetWidth()/2, 100+(i*20), 8); circles.push_back(circle); } //connect each circle with joint for (int i = 0; i < circles.size(); i++) { shared_ptr<ofxBox2dJoint> joint = shared_ptr<ofxBox2dJoint>(new ofxBox2dJoint); //if first point, connect to top anchor if (i == 0) { joint.get()->setup(box2d.getWorld(), anchor.body, circles[i].get()->body); } else { joint.get()->setup(box2d.getWorld(), circles[i-1].get()->body, circles[i].get()->body); } joint.get()->setLength(25); joints.push_back(joint); } } //-------------------------------------------------------------- void ofApp::update(){ box2d.update(); timeElapsed = ofGetElapsedTimef(); ofPoint mouse(ofGetMouseX(), ofGetMouseY()); //"friendly" values: 25, 50 float repelDist = 150; float attractDist = 400; for (int i = 0; i < circles.size(); i++) { float dist = mouse.distance(circles[i].get()->getPosition()); float force = ofMap(dist, 0, 1262, 1.3, 0.1); // circles[i].get()->addRepulsionForce(mouse, force); if (dist < repelDist) { // circles[i].get()->addRepulsionForce(mouse, 5.5); } if (dist > attractDist) { // circles[i].get()->addAttractionPoint(mouse, 1.2); } //std::cout << dist << endl; } } //-------------------------------------------------------------- void ofApp::draw(){ ofSetHexColor(0xf2ab01); ofSetLineWidth(5); // anchor.draw(); for (int i = 0; i < circles.size(); i++) { ofFill(); ofSetHexColor(0x01b1f2); float radius = 1.0; for (int deg = 0; deg < 360*6; deg += 10) { angle = ofDegToRad(deg); float x = ofGetMouseX() + sin(angle) * radius; float y = ofGetMouseY() + cos(angle) * radius; circles[30].get()->setPosition(x, y); } circles[i].get()->draw(); std::cout << circles[i].get()->getPosition() << endl; } for (int i = 0; i < joints.size(); i++) { ofSetHexColor(0xFFFFFF); joints[i].get()->draw(); } string info = ""; }
Posts: 4
Participants: 3