Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8006169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:25:27+00:00 2026-06-04T17:25:27+00:00

I have been trying to make a cylinder in renderscript. This is the code

  • 0

I have been trying to make a cylinder in renderscript. This is the code I’ve tried:

public Mesh cylinder(){
    float radius=1.25f, halfLength=5;
    int slices=16;

    Mesh.TriangleMeshBuilder mbo= new TriangleMeshBuilder(mRSGL,3, Mesh.TriangleMeshBuilder.TEXTURE_0);
    for(int i=0; i<slices; i++) {
    float theta = (float) (((float)i)*2.0*Math.PI);
    float nextTheta = (float) (((float)i+1)*2.0*Math.PI);

        /*vertex at middle of end*/
    mbo.addVertex(0.0f, halfLength, 0.0f);

        /*vertices at edges of circle*/
    mbo.addVertex((float)(radius*Math.cos(theta)), halfLength, (float)(radius*Math.sin(theta)));
        mbo.addVertex((float)(radius*Math.cos(nextTheta)), halfLength, (float)(radius*Math.sin(nextTheta)));

        /* the same vertices at the bottom of the cylinder*/

    mbo.addVertex((float)(radius*Math.cos(nextTheta)), -halfLength, (float)(radius*Math.sin(nextTheta)));
    mbo.addVertex((float)(radius*Math.cos(theta)), halfLength, (float)(radius*Math.sin(theta)));
    mbo.addVertex(0.0f, -halfLength, 0.0f);

    mbo.addTriangle(0, 1, 2);
    mbo.addTriangle(3, 4, 5);


}
    return mbo.create(true);

}

But this code gives me a rectangle of length 5. Any ideas where I’m going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:25:29+00:00Added an answer on June 4, 2026 at 5:25 pm

    You actually have a few problems here. First, your angles are always equal to multiples of 2pi. You need to divide by the number of sectors when you calculate your angles. Additionally in this step you have an unnecessary explicit type conversion, java will handle the conversion of integer to double for you.

    Second, you are constantly adding the same two triangles to the mesh and not adding any triangles for the side of the cylinder, just the two end faces. In your loop when calling addTriangle() you should use indices, for example addTriangle(n, n+1, n+2).

    Finally, you were missing a negative sign when you created your 4th vertex, so it was actually at halfLength, not -halfLength.

    Try this:

    public Mesh cylinder(){
        float radius=1.25f, halfLength=5;
        int slices=16;
    
        Mesh.TriangleMeshBuilder mbo= new TriangleMeshBuilder(mRSGL,3, Mesh.TriangleMeshBuilder.TEXTURE_0);
    
        /*vertex at middle of end*/
        mbo.addVertex(0.0f, halfLength, 0.0f);
        mbo.addVertex(0.0f, -halfLength, 0.0f);
    
        for(int i=0; i<slices; i++) {
             float theta = (float) (i*2.0*Math.PI / slices);
             float nextTheta = (float) ((i+1)*2.0*Math.PI / slices);
    
             /*vertices at edges of circle*/
             mbo.addVertex((float)(radius*Math.cos(theta)), halfLength, (float)(radius*Math.sin(theta)));
             mbo.addVertex((float)(radius*Math.cos(nextTheta)), halfLength, (float)(radius*Math.sin(nextTheta)));
    
             /* the same vertices at the bottom of the cylinder*/
             mbo.addVertex((float)(radius*Math.cos(nextTheta)), -halfLength, (float)(radius*Math.sin(nextTheta)));
             mbo.addVertex((float)(radius*Math.cos(theta)), -halfLength, (float)(radius*Math.sin(theta)));
    
             /*Add the faces for the ends, ordered for back face culling*/
             mbo.addTriangle(4*i+3, 4*i+2, 0); 
             //The offsets here are to adjust for the first two indices being the center points. The sector number (i) is multiplied by 4 because the way you are building this mesh, there are 4 vertices added with each sector
             mbo.addTriangle(4*i+5, 4*i+4, 1);
             /*Add the faces for the side*/
             mbo.addTriangle(4*i+2, 4*i+4, 4*i+5); 
             mbo.addTriangle(4*i+4, 4*i+2, 4*i+3);
        }
    return mbo.create(true);
    
    }
    

    I have also added a slight optimization where the vertices for the centers of the circles are created only once, thus saving memory. The order of indices here is for back face culling. Reverse it if you want front face. Should your needs require a more efficient method eventually, allocation builders allow for using trifans and tristrips, but for a mesh of this complexity the ease of triangle meshes is merited. I have run this code on my own system to verify that it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to make this to be a little jQuery plugin that
Using MVC 2 I have been trying to make this record store project. Creating
I have been trying to make a cash register like code, where the user
I have been trying to make OOP PHP5 code. But I think my attempts
I have been trying to make a simple app with 3 fields like this:
I have been trying to make this search engine for a MySQL database. Taking
I have been trying to make this work for a long time now. In
I have been trying to make a VBA code to do the following. I
I have been trying to make this work for couple of days now, my
Have have been trying to make a validator for my xml files. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.