iMiniB3D Rolling Ball Problems

BlitzMax Forums/MiniB3D Module/iMiniB3D Rolling Ball Problems

Rob Pearmain(Posted 2012) [#1]
Hi,

i am trying to convert some old Rolling ball code to iMiniB3D.

Original post from a much younger version of me ;-):

[url=http://www.blitzbasic.com/Community/posts.php?topic=14125#142651/]Link here[/url]

However, the turnentity just doesn't seem to want to play ball. I am not sure if it is something to do with World Co-ordinates or anything.

Any help would be really appreciated:

    bool Entity_Hit = b.entity->EntityCollided(world_col);
    
    float Velocity = sqrt((b.vx*b.vx) + (b.vy*b.vy) + (b.vz*b.vz));
    
    if(Velocity > 0)
    {
        float direction_x = b.vx / Velocity;
        float direction_y = b.vy / Velocity;
        float direction_z = b.vz / Velocity;
        
        float air_friction_force = AIR_FRICTION_CONSTANT * (Velocity * Velocity);
        Velocity -= air_friction_force;
        
        if(Entity_Hit)
        {
            Velocity -= GROUND_FRICTION_CONSTANT;
        }
        
        if(Velocity < 0) { Velocity+=0.001f; }
        
        b.vx = direction_x * Velocity;
        b.vy = direction_y * Velocity;
        b.vz = direction_z * Velocity;
        
        if(Entity_Hit)
        {
            float Nx = b.entity->CollisionNX(0);
            float Ny = b.entity->CollisionNY(0);
            float Nz = b.entity->CollisionNZ(0);
            
            float VdotN = (b.vx*Nx) + (b.vy*Ny) + (b.vz * Nz);
            
            float NFx = -2.0f * Nx * VdotN;
            float NFy = -2.0f * Ny * VdotN;
            float NFz = -2.0f * Nz * VdotN;
            
            b.vx+=NFx;
            b.vy+=NFy;
            b.vz+=NFz;
            
            
        }
    }
    
    if(Entity_Hit)
    {
        //b.vx -= 0.01f;
        //b.vz += 0.001f;
    }
    
    b.vy -= GRAVITY;
    
    b.oldx = b.newx;
    b.oldz = b.newz;

    b.entity->TranslateEntity(b.vx, b.vy, b.vz, true  );

    b.newx = b.entity->EntityX(true);
    b.newz = b.entity->EntityZ(true);
    
    float Mx = (b.newx - b.oldx);
    float Mz = (b.newz - b.oldz);
    
    float xAngleAdjust  = Mx / BALL_SIZE * 180.0/PI;
    //float xAngleAdjust  = Mx;
    float zAngleAdjust  = Mz / BALL_SIZE * 180.0/PI;
    //float zAngleAdjust  = Mz;
    b.entity->TurnEntity(zAngleAdjust, 0, -xAngleAdjust, true);


Last edited 2012

Last edited 2012

Last edited 2012


Rob Pearmain(Posted 2012) [#2]
.

Last edited 2012


ima747(Posted 2012) [#3]
Don't have time to read this line by line but the problem with rolling is usually due to gimbal lock (search and you will find tons of threads relating to gimbal lock in here). Not sure exactly what you're going for but for the "effect" of something rolling you can sometimes use turnmesh as a bit of a hack then just slide the object around... just a passing thought.

Last edited 2012


Rob Pearmain(Posted 2012) [#4]
Thanks for the tips, was just intrigued to see if there was any difference between iMiniB3D and Blitz3D in respects to the code.

I shall begin my Gimble Lock search ;-)

...

Got it, the following code works...

Thanks for pointing me in the right direction

    b.vy -= GRAVITY;
    
    b.oldx = b.newx;
    b.oldz = b.newz;

    b.entity->TranslateEntity(b.vx, b.vy, b.vz, true  );

    b.newx = b.entity->EntityX();
    b.newz = b.entity->EntityZ();
    
    float Mx = (b.newx - b.oldx);
    float Mz = (b.newz - b.oldz);
    
    float xAngleAdjust  = (Mx / BALL_SIZE) * (180.0/PI);
    //float xAngleAdjust  = Mx;
    float zAngleAdjust  = (Mz / BALL_SIZE) * (180.0/PI);
    //float zAngleAdjust  = Mz;
    float fPitch = b.entity->EntityPitch();
    float fYaw = b.entity->EntityYaw();
    float fRoll = b.entity->EntityRoll();
    
    fPitch += zAngleAdjust;
    fRoll -= xAngleAdjust;
    b.entity->RotateMesh(fPitch,fYaw,fRoll);


Last edited 2012