Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7839553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:28:16+00:00 2026-06-02T15:28:16+00:00

At the moment I am building a cloth physics app using OpenFrameworks. I’m new

  • 0

At the moment I am building a cloth physics app using OpenFrameworks. I’m new to C++, for a heads up.

In my app, two ‘neighbor’ particle objects are passed to a spring object as pointers. As a test, I have the spring object draw lines between the two particles (between their 3d vector positions). For some reason, these lines are different every time I run the program, even though no random values are involved. When I cout the values of the particle positions from the spring struct, they often are ridiculous values like -4.15301e-12. I’m following example code almost verbatim, so I’m not really sure where I’m going wrong.

Here is the example code I’m following:

https://sites.google.com/site/ofauckland/examples/17-cloth-physics

Here is my Spring struct:

#pragma once
#include "ofMain.h"
#include "Particle.h"

struct Spring {

    float k, restLength;
    Particle *a, *b;
    ofVec3f posA, posB;

    Spring(Particle *a, Particle *b, float k = .2) : a(a), b(b), k(k) {
        restLength = (b->pos - a->pos).length();
    }

    void update() {
        posA = a->pos;
        posB = b->pos;
    }
    void draw() {
        ofSetLineWidth(5);
        ofSetColor(0, 255, 0);
        ofLine(posA.x, posA.y, posB.x, posB.y);
    }
};                             

The particle struct:

#pragma once
#include "ofMain.h"

struct Particle {

    ofVec3f pos;

    Particle(ofVec3f pos) : pos(pos) {
    }

    void update() {
    }
    void draw() {
        ofSetColor(ofRandom(255), 0, 0);
        ofFill();
        ofCircle(pos.x, pos.y, 3); 
    }
};       

And this is where I pass the two particles to the spring as pointers:

#pragma once
#include "ofMain.h" 
#include "Particle.h"
#include "Spring.h"

struct Petal {

    float maxWidth, spacing;
    vector<Particle> particles;
    vector<Spring> springs;

    Petal(float maxWidth, float spacing) : maxWidth(maxWidth), spacing(spacing) {        
        setupPoints();
    }

    void setupPoints() {
        float x = 0;
        float y = 0;

        for(int r = 1; r <= maxWidth; r++) {
            x = (int)(r / 2) * -spacing;
            y += spacing;
            for(int c = 1; c <= r; c++) { 
                ofVec3f pos = ofVec3f(x, y, 0);
                Particle p(pos);
                particles.push_back(p); 
                x+=spacing;
            }
        }

        for(int r = maxWidth; r > 0; r--) {
            x = (int)(r / 2) * -spacing;
            y += spacing;
            for(int c = 1; c <= r; c++) { 
                ofVec3f pos = ofVec3f(x, y, 0);  
                Particle p(pos);
                particles.push_back(p); 
                x+=spacing;
            }
        }

        //find neighbors
        for(int i = 0; i < particles.size(); i++) {
            Spring s(&particles[i], &particles[findNeighbor(i)]);
            springs.push_back(s);
        }
    }

    int findNeighbor(int pIndex) {
        float leastDist = 0;
        float leastDistIndex = 0;
        for(int i = 0; i < particles.size(); i++) {
            if(i != pIndex) {
                float distance = particles[pIndex].pos.distance(particles[i].pos);
                if(abs(distance) < abs(leastDist) || leastDist == 0) {
                        leastDist = distance;
                        leastDistIndex = i;
                }
            }
        }
        return leastDistIndex;

    }

    void update() {   
        for(int i = 0; i < particles.size(); i++) {
            particles[i].update();   
        }
        for(int s = 0; s < springs.size(); s++) {
            springs[s].update();
        }
    }
    void draw() {
        for(int i = 0; i < particles.size(); i++) {
            particles[i].draw();   
        }
        for(int s = 0; s < springs.size(); s++) {
            springs[s].draw();
        }
    }
};

This is what happens. What’s strange is that some of the springs seem to be in the correct position.

Please let me know if I can clarify something.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:28:19+00:00Added an answer on June 2, 2026 at 3:28 pm

    Your particles vector holds Particles by value, and the vector can copy and move these values around. When you pass Particles as pointers to the Spring, you are passing the address of something that might not be there at some point in the future. I am not sure if this is the problem, but it certainly is something that needs fixing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a cms at the moment using the zend framework and have
At the moment I am building a big project using: ASP.Net MVC 4 jQuery
At this moment we are building a new architecture that is based on the
I'm building a simple web app at the moment that I'll one day open
Am building an app using Django as my workhorse. All has been well so
At the moment I'm building a list using the following code: $('<li id=li' +
I am building an app in Zend Framework at the moment and testing it
I'm building an interactive anagram feature at the moment for my iPad app and
I'm building out a stock management system at the moment, using Entity Framework 4.
At the moment I'm building a login Script on powershell basis. This login script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.