Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

Help me with a project on fluid resistance

$
0
0

@jewel wrote:

HI!! I want to know why the ball doesn’t slow down in the water.

ofApp.h

class MovingBall : public ofApp {
public:
int x;
int y;
int radius;
ofVec2f location;
ofVec2f velocity;
ofVec2f acceleration;
ofVec2f force;
ofVec2f wind = ofVec2f(0,0);
ofVec2f gravity= ofVec2f(0.0,0.3);;
float mass;

MovingBall(ofVec2f l,float m){
    location = l;
    mass = m;
}
MovingBall() {}

void setForce(ofVec2f &force){
    acceleration += force;
}
void update(){
    force += gravity+wind;
    force /= mass;
    acceleration += force;
    velocity += acceleration;
    location += velocity;
}

void display(){
    ofSetColor(0, 0, 0);
    ofDrawCircle(location,mass*6);
}

void checkEdge(){
    
    if(location.x + 30 > ofGetWidth() || location.x + 30 <0) {
        velocity.x *= -1;
    }
    if (location.y + 30 > ofGetHeight() || location.y + 30 < 0) {
        velocity.y *= -1;
    }
    acceleration *= 0;
}

};

class Liquid : public MovingBall {
private:
float x , y, w, h;
float c;
public:
Liquid(float x_,float y_, float w_, float h_, float c_){
x=x_;
y=y_;
w=w_;
h=h_;
c=c_;
}

void display() {
    ofSetColor(0,0,250,100);
    ofDrawRectangle(x,y,w,h);
}

bool isInside(MovingBall &l){
     if(l.x > x && l.x<x+w &&
        l.y > y && l.y<y+h) {
         return true;
     }else {
         return false;
     }
}
void drag(Liquid &l){
    float speed = velocity.length();
    float dragMagnitude = l.c * velocity.length() * velocity.length();
    ofVec2f drag = velocity.getNormalized();
    drag *= dragMagnitude;
    acceleration += drag;
}

};

ofApp.cpp

#include “ofApp.h”
//--------------------------------------------------------------
void ofApp::setup(){

}

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

}

//--------------------------------------------------------------
void ofApp::draw(){
ofBackground(255);
static MovingBall balls(ofVec2f(ofGetWidth()/2-20,0),5);
static Liquid liquid(0,ofGetHeight()/2,ofGetWidth(),ofGetHeight()/2,10);

liquid.display();
if(liquid.isInside(balls)){
    liquid.drag(liquid);
}
ofVec2f gravity = ofVec2f(0.0,0.3);
ofVec2f wind = ofVec2f(0,0);

balls.setForce(gravity);
balls.setForce(wind);

balls.display();
balls.update();
balls.checkEdge();
balls.acceleration *= 0;

}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles