I need to make a candy bar

edited October 2016 in Questions about Code

For my coding class I have to make a 3D Toblerone bar that rotates in processing for an assignment. I am able to make the triangle shape and make it rotate but I cannot for the life of me figure out how to make the ridges! Here is what I have to make the shape now so if anyone can help me it would be so greatly appreciated!

pushMatrix();
translate( width * 0.5, height * 0.5 );
rotateY(radians(frameCount));
fill( 139,69,19 );
cylinder(3,200,50);
popMatrix();
Tagged:

Answers

  • What is cylinder()? Are you, perhaps, using p5.js?

    A runnable example is always better.

  • Here is what I have so far!

    void setup() { size(400,400, P3D); }

    void draw() { background( 255 );

    pushMatrix();
    translate( width * 0.5, height * 0.5 );
    rotateY(radians(frameCount));
    fill( 139,69,19 );
    cylinder(3,200,50);
    popMatrix();
    

    }

    void cylinder( int numSegments, float h, float r ) { float angle = 360.0 / (float)numSegments;

    beginShape();
    for ( int i = 0; i < numSegments; i++ ) {
        float x = cos( radians( angle * i ) ) * r;
        float y = sin( radians( angle * i ) ) * r;
        vertex( x, y, -h/2 );
    }
    endShape();
    
    
    beginShape( QUAD_STRIP );
    for ( int i = 0; i < numSegments + 1; i++ ) {
        float x = cos( radians( angle * i ) ) * r;
        float y = sin( radians( angle * i ) ) * r;
        vertex( x, y, -h/2 );
        vertex( x, y, h/2 );
    }
    endShape();
    
    
    beginShape();
    for ( int i = 0; i < numSegments; i++ ) {
        float x = cos( radians( angle * i ) ) * r;
        float y = sin( radians( angle * i ) ) * r;
        vertex( x, y, h/2 );
    }
    endShape();
    

    }

  • Edit post, highlight code, press ctrl-o to format your code for the forum.

  • It's obvious though, isn't it, that you can't treat the entire thing as one cylinder when it's segmented like that. You need to call cylinder multiple times, once for each segment.

  • I have tried that but then all the segments aren't connected like they would be on the actual candy bar

  • Yes, part two of the problem is adding the bit that joins all the segments. You can't use cylinder for this because it's not a regular shape. box() might do it, depends on how accurate a model you want.

  • For reference:

  • I'll try that in the morning and let you know how it goes! Don't know why I didn't think of using box() before!

  • Box would give you a rectangular cross section for the bottom bit, but it's actually tapered to match the cross section of the triangle.

    You could use 4 rectangles for this instead (no need to bother with the ends)

  • @lbrez16 -- Hope your complete bar worked out -- you are definitely on the right track. There is a useful related example of a pyramid on the P3D Tutorial page:

    Custom 3D shapes are drawn using beginShape(), endShape(), and vertex()

Sign In or Register to comment.