WIllJBD
WIllJBD

Reputation: 6164

why is a float returning 0 when dividing 1 by 2 or 4?

This is the method i use to fly through my sprite sheet. In the object class i keep track of the information and do all the cycling, this basically just needs to assign the texture-mapping coords. only it doesn't seem to be working. But i logged the way i flip through the rows/columns and it is cycling correctly. But I see nothing as far as a texture. at all. am I missing something?

    public void SetToAnimatedTexture(int NumFramesWide, int NumFramesTall, int CurrentRow, int CurrentColumn) {
    /***************************************************************************************** 
     * This Assembles our Vertices in the correct spots by disassembling
     * our custom rectangles corners, and adds animation. Call this after creating the meshRect
     * *****************************************************************************************/

    float frameWidth = 1/NumFramesWide;
    float frameHeight = 1/NumFramesTall;

    float u1 = frameWidth*CurrentColumn;
    float u2 = (frameWidth*CurrentColumn) + frameWidth;
    float u3 = (frameWidth*CurrentColumn) + frameWidth;
    float u4  = frameWidth*CurrentColumn;

    float v1 = (frameHeight*CurrentRow) + frameHeight;
    float v2 = (frameHeight*CurrentRow) + frameHeight;
    float v3 = frameHeight*CurrentRow;
    float v4 = frameHeight*CurrentRow;

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * VERTEX_SIZE);
    byteBuffer.order(ByteOrder.nativeOrder());
    vertices = byteBuffer.asFloatBuffer();

    float x1 = (float) rect.BottomLeftCorner.x;
    float x2 = (float) rect.BottomRightCorner.x;
    float x3 = (float) rect.TopRightCorner.x;
    float x4 = (float) rect.TopLeftCorner.x;

    float y1 = (float) rect.BottomLeftCorner.y;
    float y2 = (float) rect.BottomRightCorner.y;
    float y3 = (float) rect.TopRightCorner.y;
    float y4 = (float) rect.TopLeftCorner.y;

    vertices.put(new float[] { x1, y1, 1, 1, 1, 1, u1, v1,
                                x2, y2, 1, 1, 1, 1, u2, v2,
                                x3, y3, 1, 1, 1, 1, u3, v3,
                                x4, y4, 1, 1, 1, 1, u4, v4 });
                            //  x   y   r  g  b  A   u|s  v|t
                            //  x,y are coordinates
                            //  rgba = color red green blue alpha
                            //  u|s v|t = UV or ST texture coordinates
    vertices.flip();

    byteBuffer = ByteBuffer.allocateDirect(6 * 2);
    byteBuffer.order(ByteOrder.nativeOrder());
    indices = byteBuffer.asShortBuffer();
    indices.put(new short[] { 0, 1, 2,
                                2, 3, 0 });
    indices.flip();

}

this is part of a framework i have been setting up for myself.

Ok figured out the issue by doing some logging. this right here is the probelm

    float frameWidth = 1/NumFramesWide;
    float frameHeight = 1/NumFramesTall;

it is returning both values as 0. it says

    1/2=0 , 1/4=0

i checked the value of all the variables and this is the issue.

Upvotes: 0

Views: 1683

Answers (1)

digitalbreed
digitalbreed

Reputation: 4070

It's doing integer division first and then assignment. Since both 1 and NumFrames* are integers, you end up with 0.

You can fix this by changing your code to

float frameWidth = 1.f/NumFramesWide;
float frameHeight = 1.f/NumFramesTall;

Upvotes: 9

Related Questions