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

Wrapping 2D Shapes Seamlessly

$
0
0

@Caroline_Record wrote:

I am using some flocking boids as a texture on a sphere. Consequentially I need the shapes to wrap over the texture perfectly, so there will be no seam when it is wrapped around the sphere. The simple wrapping already implemented is not complex enough to be totally seamless.

//  r is the radius
if (loc.x < -r) loc.x = ofGetWidth()+r;
if (loc.y < -r) loc.y = ofGetHeight()+r;

if (loc.x > ofGetWidth()+r) loc.x = -r;
if (loc.y > ofGetHeight()+r) loc.y = -r;

So this is what I am doing to make it seamless. (This is just with a single ellipse, but I’ll eventually apply it to each boid within the flocking addon I’m using)

.h

bool isDoubleX;
bool isDoubleY;
ofVec2f pos;
ofVec2f pos2;
int width;

.cpp

setup:

pos = ofVec2f(ofGetWidth()/2, ofGetHeight()/2);
pos2 = pos;
width = 100;
ofSetColor(255,0,0);
isDoubleX = false;
isDoubleY = false;

update:

pos.x += ofMap(ofNoise(200, ofGetElapsedTimef()/5), 0, 1, 0, 7);
pos.y += ofMap(ofNoise(400, ofGetElapsedTimef()/5), 0, 1, 0,7);
pos2 = pos;

isDoubleY = false;
isDoubleX = false;

// too far right, it is on the seam
if ((pos.x + width/2 >= ofGetWidth()) & ((pos.x - width/2) < ofGetWidth()) ){
    pos2.x =pos.x - ofGetWidth();
    isDoubleX = true;
}
// it has gone past the seam
else if ((pos.x - width/2) >= ofGetWidth()){
    pos.x = width/2;
    isDoubleX = false;
}
// this is for when it has gone too far to the left
else if ((pos.x-width/2 <= 0)& (pos.x + width/2 > 0) ){
    pos2.x =pos.x + ofGetWidth();
    isDoubleX = true;
}
// it has gone past the seam
else if ((pos.x + width/2 <= 0)){
    isDoubleX = false;
    pos.x = ofGetWidth() - width/2;
}

// same but for y-axis
if ((pos.y + width/2 >= ofGetHeight()) & ((pos.y - width/2) < ofGetHeight()) ){
    pos2.y =pos.y - ofGetHeight();
    isDoubleY = true;
}
else if ((pos.y - width/2) >= ofGetHeight()){
    pos.y = width/2;
    isDoubleY = false;
}
else if ((pos.y-width/2 <= 0)& (pos.y + width/2 > 0) ){
    pos2.y =pos.y + ofGetHeight();
    isDoubleY = true;
}
else if ((pos.y + width/2 <= 0)){
    isDoubleY = false;
    pos.y = ofGetHeight() - width/2;
}

draw

if (isDoubleX | isDoubleY){
    ofDrawEllipse(pos.x, pos.y, width, width);
    ofDrawEllipse(pos2.x, pos2.y, width, width);
}
else{
    ofDrawEllipse(pos.x, pos.y, width, width);
}

This works pretty well, but sometimes the circle appears to triple around the corners. Anybody know of a better way?

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles