0
\$\begingroup\$

I am trying to learn and teach myself openGL and I wanted to draw a figure 8 in a 3D setting but I'm having a problem. Please forgive me, I am very new at this but I'll attempt to explain what I have done so far

I understand that I can use the Lemniscate of Bernoulli to draw a nice figure 8, so I've have adopted that idea into my code. I just want to draw it along the x and z plane. My code looks like this

#define PI 3.1415926535f #define MAX_STEPS 100.0f //... Vector3 PointOnCurve1(Vector3 p1, float t) { Vector3 tempPoint = {0.0f, 0.0f, 0.0f}; float scaler = 2/ (3- cos(2*t)); varX = scaler * cos(t); varZ = scaler * sin(2*t) / 2 tempPoint.x = varX+p1.x; tempPoint.y += p1.y; tempPoint.z =varX+p1.z; return tempPoint; } Vector3 g_Point = {-4, 0, 0}; glBegin(GL_LINE_STRIP); for(float t = -PI; t <= PI; t += PI / MAX_STEPS) { vPoint = PointOnCurve1(g_Point, t); glVertex3f(vPoint.x, vPoint.y, vPoint.z); } glEnd(); 

All I'm getting is one straight green line instead of a figure 8. any ideas? Am I missing something here? I'm basing this equation from this gamedev article

How can I move an object in an "infinity" or "figure 8" trajectory?

\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$

Looks like a simple typo - you are updating tempPoint.z using varX, not varZ, so your coordinates move in unison and the shape collapses to a line. This is a common sort of mistake when writing componentwise math and you should learn to recognize the symptom.

Avoiding this mistake can be helped by using vector operations where possible, like p1 + var where var is a vector instead of varX and varZ.

\$\endgroup\$
2
  • \$\begingroup\$ What do you think of operator overloading instead? \$\endgroup\$ Commented Mar 11, 2013 at 3:54
  • \$\begingroup\$ @user955880 That's what I meant in the second paragraph. \$\endgroup\$ Commented Mar 11, 2013 at 5:33
0
\$\begingroup\$

You're viewing the figure 8 from the side, so it just looks like a line. Try building it on the XY plane instead of the XZ plane.

\$\endgroup\$
2
  • \$\begingroup\$ Actually... It's even dumber than than. I didn't update the z variable accordingly. tempPoint.z =varX+p1.z; Is what I had. \$\endgroup\$ Commented Mar 11, 2013 at 3:50
  • \$\begingroup\$ Hah, nice, just noticed that varX showed up twice. Glad you got it figured out. \$\endgroup\$ Commented Mar 11, 2013 at 3:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.