WMP Visualization help
-
Hi, i am making a Windows Media Player Visualization and i need some help with an algorithm. I am making a snake like curve that moves around the screen. It uses 3 point bezier curves joined together to form one long curve. The head continually grows while the end disappears. I need to make each new one connect to the old head but my code doesn't work. Here is some of the code:
typedef struct tagBezier{
float x, y; // Curve position
POINTF P0, P1, P2; // Control points
float t, t2; // Time variable [0-1]
} Bezier;void CViz3D::calculateBezier(Bezier& b, float time) {
b.t2 = b.t; b.t = time;b.x = (pow(b.P0.x\*(1 - b.t),2) + b.P1.x\*2\*b.t\*(1 - b.t) + pow(b.P2.x\*b.t,2)); b.y = (pow(b.P0.y\*(1 - b.t),2) + b.P1.y\*2\*b.t\*(1 - b.t) + pow(b.P2.y\*b.t,2));
}
void CViz3D::drawBezierCurve(Bezier b[CURVES]) {
float iStart = 0, iEnd = 1;if (b\[start\].t < b\[start\].t2) { // Change position if finished int sOld = start; // Previous start pos start = (start+1)%CURVES; end = (end+1)%CURVES; float distX=b\[sOld\].P2.x-b\[sOld\].P1.x, distY=b\[sOld\].P2.y-b\[sOld\].P1.y; b\[start\].P0.x = b\[sOld\].P2.x; b\[start\].P0.y = b\[sOld\].P2.y; b\[start\].P1.x = b\[sOld\].P2.x+distX; b\[start\].P1.x = b\[sOld\].P2.y+distY; b\[start\].P2.x = (RAND-0.5f); b\[start\].P2.y = (RAND-0.5f); } for (int j = 0; j < CURVES; j++) { if (j == start) { iStart = 0; iEnd = bezTime; } else if (j == end){ iStart = bezTime; iEnd = 1; } else { iStart = 0; iEnd = 1; } glBegin(GL\_LINE\_STRIP); for (float i = iStart; i < iEnd; i += 0.01f) { calculateBezier(b\[j\], i); glVertex2f(b\[j\].x, b\[j\].y); } glEnd(); }
}
Can someone figure out what's going on. Thanx
-
Hi, i am making a Windows Media Player Visualization and i need some help with an algorithm. I am making a snake like curve that moves around the screen. It uses 3 point bezier curves joined together to form one long curve. The head continually grows while the end disappears. I need to make each new one connect to the old head but my code doesn't work. Here is some of the code:
typedef struct tagBezier{
float x, y; // Curve position
POINTF P0, P1, P2; // Control points
float t, t2; // Time variable [0-1]
} Bezier;void CViz3D::calculateBezier(Bezier& b, float time) {
b.t2 = b.t; b.t = time;b.x = (pow(b.P0.x\*(1 - b.t),2) + b.P1.x\*2\*b.t\*(1 - b.t) + pow(b.P2.x\*b.t,2)); b.y = (pow(b.P0.y\*(1 - b.t),2) + b.P1.y\*2\*b.t\*(1 - b.t) + pow(b.P2.y\*b.t,2));
}
void CViz3D::drawBezierCurve(Bezier b[CURVES]) {
float iStart = 0, iEnd = 1;if (b\[start\].t < b\[start\].t2) { // Change position if finished int sOld = start; // Previous start pos start = (start+1)%CURVES; end = (end+1)%CURVES; float distX=b\[sOld\].P2.x-b\[sOld\].P1.x, distY=b\[sOld\].P2.y-b\[sOld\].P1.y; b\[start\].P0.x = b\[sOld\].P2.x; b\[start\].P0.y = b\[sOld\].P2.y; b\[start\].P1.x = b\[sOld\].P2.x+distX; b\[start\].P1.x = b\[sOld\].P2.y+distY; b\[start\].P2.x = (RAND-0.5f); b\[start\].P2.y = (RAND-0.5f); } for (int j = 0; j < CURVES; j++) { if (j == start) { iStart = 0; iEnd = bezTime; } else if (j == end){ iStart = bezTime; iEnd = 1; } else { iStart = 0; iEnd = 1; } glBegin(GL\_LINE\_STRIP); for (float i = iStart; i < iEnd; i += 0.01f) { calculateBezier(b\[j\], i); glVertex2f(b\[j\].x, b\[j\].y); } glEnd(); }
}
Can someone figure out what's going on. Thanx
Hi, may I suggest you expand on "does not work" before you expect anyone to dig in ? Does it compile ? run ? behave for a while ? then what ? :)
Luc Pattyn [My Articles]
-
Hi, may I suggest you expand on "does not work" before you expect anyone to dig in ? Does it compile ? run ? behave for a while ? then what ? :)
Luc Pattyn [My Articles]
Sorry i should have mentioned, the curves don't match up. That is, when a new curve is made it should smoothly match up with the one before it. Here is an image that explains it. I can't be bothered posting an image of the actual result but basically point B2 seems to be at any random spot instead of being in line with A2. http://img89.imageshack.us/img89/318/bezierlt5.png
-
Sorry i should have mentioned, the curves don't match up. That is, when a new curve is made it should smoothly match up with the one before it. Here is an image that explains it. I can't be bothered posting an image of the actual result but basically point B2 seems to be at any random spot instead of being in line with A2. http://img89.imageshack.us/img89/318/bezierlt5.png
Hi, some remarks: 1. you managed to confuse yourself and me by using different numbering in code and drawing; 2. your code makes two distances equal: A3-A2 = B2-B1 in drawing's notation, although drawing's annotation says otherwise. 3. you are aware I trust that RAND is called twice and will return two different numbers; this yields independent x and y for B3 (is OK). 4. but I would make the distance B3-B2 random, so I prefer something like:
b[start].P2.x = b[start].P1.x+maxStepX*(RAND-0.5f);
b[start].P2.y = b[start].P1.y+maxStepY*(RAND-0.5f);with maxStepX and maxStepY some reasonable constants (maybe equal). Hope this helps.
Luc Pattyn [My Articles]
-
Hi, some remarks: 1. you managed to confuse yourself and me by using different numbering in code and drawing; 2. your code makes two distances equal: A3-A2 = B2-B1 in drawing's notation, although drawing's annotation says otherwise. 3. you are aware I trust that RAND is called twice and will return two different numbers; this yields independent x and y for B3 (is OK). 4. but I would make the distance B3-B2 random, so I prefer something like:
b[start].P2.x = b[start].P1.x+maxStepX*(RAND-0.5f);
b[start].P2.y = b[start].P1.y+maxStepY*(RAND-0.5f);with maxStepX and maxStepY some reasonable constants (maybe equal). Hope this helps.
Luc Pattyn [My Articles]
"1. you managed to confuse yourself and me by using different numbering in code and drawing" yeah, sorry about that. I draw the picture before i wrote the code, to give me an idea of what to do... that didn't help :) "2. your code makes two distances equal:..." yes, i changed that to make it easier to code "4. but I would make the distance B3-B2 random..." isn't that basically the same as what i did, except i picked random points. Thanks for your help. But my problem it that B2 isn't lined up with A2 and i don't know why?
-
"1. you managed to confuse yourself and me by using different numbering in code and drawing" yeah, sorry about that. I draw the picture before i wrote the code, to give me an idea of what to do... that didn't help :) "2. your code makes two distances equal:..." yes, i changed that to make it easier to code "4. but I would make the distance B3-B2 random..." isn't that basically the same as what i did, except i picked random points. Thanks for your help. But my problem it that B2 isn't lined up with A2 and i don't know why?
Hi, I looked at your code again and discovered you are squaring the coordinates !?!? b.P0.x and the like should not be inside pow(...,2) ! Also I would suggest to keep the bezier curves large enough, i.e. I hope you are not trying to draw a bezier that only spans a few pixels (would waste CPU cycles and might be off due to quantization effects). :)
Luc Pattyn [My Articles]
-
Hi, I looked at your code again and discovered you are squaring the coordinates !?!? b.P0.x and the like should not be inside pow(...,2) ! Also I would suggest to keep the bezier curves large enough, i.e. I hope you are not trying to draw a bezier that only spans a few pixels (would waste CPU cycles and might be off due to quantization effects). :)
Luc Pattyn [My Articles]
-
Thanks for that. :) I never would have found that out. I got the formula for Bezier curves off the internet and coded it in C++ but i didn't keep the original formula so i just assumed i copied it right. Thanks again.
You're welcome. When in doubt about some formula, I check the edge conditions, in your case the Bezier must yield P0 and P2 for the special values t=0 and t=1. Furthermore, squaring a physical thing (such as a coordinate) does not occur often. Best regards,
Luc Pattyn [My Articles]
-
You're welcome. When in doubt about some formula, I check the edge conditions, in your case the Bezier must yield P0 and P2 for the special values t=0 and t=1. Furthermore, squaring a physical thing (such as a coordinate) does not occur often. Best regards,
Luc Pattyn [My Articles]
There is still just one little problem. Sometimes the curves don't go from one to the other and stay there. That is, one curve keeps repeating over and over as if
if (b[start].t < b[start].t2)
doesn't evaluate to true??? Here is a link to download the viz so you can see for yourself. http://www.savefile.com/files/550244[^] Read the readme, it tells you how to install it. If you can't get it to install tell me and i'll post some pics instead. -
You're welcome. When in doubt about some formula, I check the edge conditions, in your case the Bezier must yield P0 and P2 for the special values t=0 and t=1. Furthermore, squaring a physical thing (such as a coordinate) does not occur often. Best regards,
Luc Pattyn [My Articles]
thanks: https://movied.org
-
"1. you managed to confuse yourself and me by using different numbering in code and drawing" yeah, sorry about that. I draw the picture before i wrote the code, to give me an idea of what to do... that didn't help :) "2. your code makes two distances equal:..." yes, i changed that to make it easier to code "4. but I would make the distance B3-B2 random..." isn't that basically the same as what i did, except i picked random points. Thanks for your help. But my problem it that B2 isn't lined up with A2 and i don't know why?
thanks: https://movied.org
-
There is still just one little problem. Sometimes the curves don't go from one to the other and stay there. That is, one curve keeps repeating over and over as if
if (b[start].t < b[start].t2)
doesn't evaluate to true??? Here is a link to download the viz so you can see for yourself. http://www.savefile.com/files/550244[^] Read the readme, it tells you how to install it. If you can't get it to install tell me and i'll post some pics instead.thanks: https://movied.org
-
Hi, I looked at your code again and discovered you are squaring the coordinates !?!? b.P0.x and the like should not be inside pow(...,2) ! Also I would suggest to keep the bezier curves large enough, i.e. I hope you are not trying to draw a bezier that only spans a few pixels (would waste CPU cycles and might be off due to quantization effects). :)
Luc Pattyn [My Articles]
thanks: https://movied.org
-
Hi, some remarks: 1. you managed to confuse yourself and me by using different numbering in code and drawing; 2. your code makes two distances equal: A3-A2 = B2-B1 in drawing's notation, although drawing's annotation says otherwise. 3. you are aware I trust that RAND is called twice and will return two different numbers; this yields independent x and y for B3 (is OK). 4. but I would make the distance B3-B2 random, so I prefer something like:
b[start].P2.x = b[start].P1.x+maxStepX*(RAND-0.5f);
b[start].P2.y = b[start].P1.y+maxStepY*(RAND-0.5f);with maxStepX and maxStepY some reasonable constants (maybe equal). Hope this helps.
Luc Pattyn [My Articles]
thanks: https://movied.org
-
Thanks for that. :) I never would have found that out. I got the formula for Bezier curves off the internet and coded it in C++ but i didn't keep the original formula so i just assumed i copied it right. Thanks again.
thanks: https://movied.org
-
Hi, may I suggest you expand on "does not work" before you expect anyone to dig in ? Does it compile ? run ? behave for a while ? then what ? :)
Luc Pattyn [My Articles]
thanks: https://movied.org
-
Sorry i should have mentioned, the curves don't match up. That is, when a new curve is made it should smoothly match up with the one before it. Here is an image that explains it. I can't be bothered posting an image of the actual result but basically point B2 seems to be at any random spot instead of being in line with A2. http://img89.imageshack.us/img89/318/bezierlt5.png
thanks: https://movied.org
-
Hi, i am making a Windows Media Player Visualization and i need some help with an algorithm. I am making a snake like curve that moves around the screen. It uses 3 point bezier curves joined together to form one long curve. The head continually grows while the end disappears. I need to make each new one connect to the old head but my code doesn't work. Here is some of the code:
typedef struct tagBezier{
float x, y; // Curve position
POINTF P0, P1, P2; // Control points
float t, t2; // Time variable [0-1]
} Bezier;void CViz3D::calculateBezier(Bezier& b, float time) {
b.t2 = b.t; b.t = time;b.x = (pow(b.P0.x\*(1 - b.t),2) + b.P1.x\*2\*b.t\*(1 - b.t) + pow(b.P2.x\*b.t,2)); b.y = (pow(b.P0.y\*(1 - b.t),2) + b.P1.y\*2\*b.t\*(1 - b.t) + pow(b.P2.y\*b.t,2));
}
void CViz3D::drawBezierCurve(Bezier b[CURVES]) {
float iStart = 0, iEnd = 1;if (b\[start\].t < b\[start\].t2) { // Change position if finished int sOld = start; // Previous start pos start = (start+1)%CURVES; end = (end+1)%CURVES; float distX=b\[sOld\].P2.x-b\[sOld\].P1.x, distY=b\[sOld\].P2.y-b\[sOld\].P1.y; b\[start\].P0.x = b\[sOld\].P2.x; b\[start\].P0.y = b\[sOld\].P2.y; b\[start\].P1.x = b\[sOld\].P2.x+distX; b\[start\].P1.x = b\[sOld\].P2.y+distY; b\[start\].P2.x = (RAND-0.5f); b\[start\].P2.y = (RAND-0.5f); } for (int j = 0; j < CURVES; j++) { if (j == start) { iStart = 0; iEnd = bezTime; } else if (j == end){ iStart = bezTime; iEnd = 1; } else { iStart = 0; iEnd = 1; } glBegin(GL\_LINE\_STRIP); for (float i = iStart; i < iEnd; i += 0.01f) { calculateBezier(b\[j\], i); glVertex2f(b\[j\].x, b\[j\].y); } glEnd(); }
}
Can someone figure out what's going on. Thanx
thanks: https://movied.org