Problem with debugging

Hello, I am new to OpenFrameworks coming from Processing. I keep receiving Thread 1: EXC_BAD_ACESS (code=1, address= 0x0).
The program animates an attractor of mouse position.

This is my program :slight_smile:

#pragma once

#include “ofMain.h”
#include

class ofApp : public ofBaseApp{

public:
	void setup();
	void update();
	void draw();

	void keyPressed(int key);
	void keyReleased(int key);
	void mouseMoved(int x, int y );
	void mouseDragged(int x, int y, int button);
	void mousePressed(int x, int y, int button);
	void mouseReleased(int x, int y, int button);
	void mouseEntered(int x, int y);
	void mouseExited(int x, int y);
	void windowResized(int w, int h);
	void dragEvent(ofDragInfo dragInfo);
	void gotMessage(ofMessage msg);

int num;
vector <int> vx;
vector <int> vy;
vector <int> x;
vector <int> y;
vector <int> ax;
vector <int> ay;

int magnetism;
int radius;

float size;

int decerleration;
int speed;

int xPos;
int yPos;

int distance;

int touchX;
int touchY;

int r;
int g;
int b; 

};

#include “ofApp.h”

//--------------------------------------------------------------
void ofApp::setup(){

ofBackground(0);

num = 1000;
magnetism = 10.0;
radius = 1;
decerleration = 0.95;

ofNoFill();
ofEnableBlendMode(OF_BLENDMODE_ADD);

//x.resize(num);

for(int i = 0; i< num; i++){
    x[i]= ofRandom(ofGetWidth());
    y[i] = ofRandom(ofGetHeight());
    vx[i] = 0;
    vy[i] = 0;
    ax[i] = 0;
    ay[i] = 0;

// x.push_back(x);
// x[i].set(x);
}

}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(0, 0, 0);
ofDrawRectangle(0, 0, ofGetHeight(), ofGetWidth());

for(int i= 0; i< num; i++){
    distance = ofDist(touchX, touchY, x[i], y[i]);
    if(distance > 3){
        ax[i] = magnetism * (touchX - x[i]) / (distance * distance);
        ay[i] = magnetism * (touchY - y[i]) / (distance * distance);
    }
    vx[i] += ax[i];
    vy[i] += ay[i];
    
    vx[i] = vx[i] *decerleration;
    vy[i] = vy[i] *decerleration;
    
    x[i] += vx[i];
    y[i] += vy[i];
    
    speed = ofDist(0, 0, vx[i], vy[i]);
    r = ofMap(speed, 0, 5, 0, 255);
    g = ofMap(speed, 0, 5, 64, 255);
    b = ofMap(speed, 0, 5, 128, 255);
    ofSetColor(r, g, b, 32);
    ofDrawEllipse(x[i], y[i], radius, radius);
}

}

Hi @Ha99
Can you maybe go back to your post and reformat it so all the code is formatted properly? It is kind of hard to read like this.
Also, try to only include relevant code; try to figure out what part is causing the problem and post only that code instead of your entire project.

ofApp.h

int num;
vector <int> vx;
vector <int> vy;
vector <int> x;
vector <int> y;
vector <int> ax;
vector <int> ay;

int magnetism;
int radius;

float size;

void ofApp::setup()

for(int i = 0; i< num; i++){
    x[i]= ofRandom(ofGetWidth());
    y[i] = ofRandom(ofGetHeight());
    vx[i] = 0;
    vy[i] = 0;
    ax[i] = 0;
    ay[i] = 0;

}

void ofApp::draw()

for(int i= 0; i< num; i++){
    distance = ofDist(touchX, touchY, x[i], y[i]);
    if(distance > 3){
        ax[i] = magnetism * (touchX - x[i]) / (distance * distance);
        ay[i] = magnetism * (touchY - y[i]) / (distance * distance);
    }
    vx[i] += ax[i];
    vy[i] += ay[i];
    
    vx[i] = vx[i] *decerleration;
    vy[i] = vy[i] *decerleration;
    
    x[i] += vx[i];
    y[i] += vy[i];

Hello, thank you for getting back. Hopefully, it is easier to read the code now. I have a feeling the array is causing the problem.
:slight_smile:

When I comment out your entire ofApp::draw() code, I still get the same error, meaning it is somewhere in the ofApp::setup() function.
You are adding data to a couple of vectors (x, y, vx, vy, ax, ay) that don’t have a size. So either use x.resize(num) to resize them before trying to add data (you actually have a line to do this commented, maybe you forgot to resize every vector?), or use the x.push_back(<value>) syntax to expand the vectors as you add the new data.
I don’t know if the rest of the code is working (I only see some faint blue dots) but at least the error is gone :wink:

By the way, you don’t have to do this:

ofSetColor(0, 0, 0);
ofDrawRectangle(0, 0, ofGetHeight(), ofGetWidth());

on every draw. By default the screen gets cleared on overy draw. You can set the color using the following line in your ofApp::setup()

ofSetBackgroundColor(0, 0, 0);

If you don’t want the screen to auto-clear, it can be turned off using:

ofSetBackgroundAuto(false); // or true to turn on (default)

Hello, thank you so much for the help :slight_smile: I managed to get rid of the error! so nice when there is no error.

This is how it looks on processing but it ended up looking totally different haha :sob:

Happy to help. I’m sure you’ll be able to fix it. Maybe start by removing some things like the blendmode to make it very simple. See if you can get the movement to work the way you expected and than slowly add the fancy visuals.

thanks! I’ll try my best :wink: