Flipping cards

Greetings,fellow members! I need help with my school project. My teacher don't understand this matter very well.
So my problem is the card I used always go upwards,despite the gravity I give. What does make the card go upwards? Any help is appreciated,I'm still new at this.
This is my code :
#define dDOUBLE
#include<ode\ode.h>
#include<SDL.h>
#include"C:\Users\UMUM\Desktop\odemeshstl\drawing.h"
#include"C:\Users\UMUM\Desktop\odemeshstl\bodydrawer.h"
#include<cstdlib>
#define NKONTAKMAKS 32
#define WWIDTH 800
#define WHEIGHT 600
#define HWIDTH 11
#define HHEIGHT 19
using namespace std;
dWorldID w;
dGeomID gb2,glantai;
dBodyID b2;
dJointGroupID kontakgroup;
dSpaceID ruang;

static void kontakcallback(void *data, dGeomID g1,dGeomID g2){
dBodyID b3,b4;
b3 = dGeomGetBody(g1);
b4 = dGeomGetBody(g2);
/*
//ambil posisi body
dReal r1[3], r2[3];
for(int i=0;i<3;i++){
if(b1) r1[i]=dBodyGetPosition(b1)[i];
if(b2) r2[i]=dBodyGetPosition(b2)[i];
}
*/
dContact kontak[NKONTAKMAKS];
int n = dCollide(g1,g2,NKONTAKMAKS,&(kontak[0].geom), sizeof(dContact));
if(n>0){
//cout<<"kontak:"<<n<<endl;
for(int i=0;i<n;i++){
/* contact[i].surface.mode = dContactBounce | dContactSoftCFM;
contact[i].surface.mu = dInfinity;
contact[i].surface.mu2 = 0;
contact[i].surface.bounce = 0.1;
contact[i].surface.bounce_vel = 0.1;
contact[i].surface.soft_cfm = 0.01;
*/
kontak[i].surface.mu = 0.3;
kontak[i].surface.bounce = 0;
kontak[i].surface.bounce_vel = 0;
kontak[i].surface.soft_cfm = 0.1;
kontak[i].surface.soft_erp = 0.1;
kontak[i].surface.mode = dContactBounce | dContactSoftCFM | dContactSoftERP | dContactApprox1_N;
dJointID c = dJointCreateContact(w,kontakgroup,&(kontak[i]));
dJointAttach(c,b3,b4);
}
}
}
int main(int argc,char *argv[])
{
SDL_Window *window;
SDL_Surface *fontsurf=NULL;
SDL_Surface *mainsurf=NULL;
SDL_Event E;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Test Mesh STL",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,WWIDTH,WHEIGHT,0);
mainsurf=SDL_GetWindowSurface(window);
bersih(mainsurf, SDL_MapRGB(mainsurf->format,0,0,0));

SDL_FillRect(mainsurf, 0, 0x008000FF);
//fontsurf=SDL_LoadBMP("font.bmp");
//if(fontsurf==NULL) return 1;
dInitODE();

dReal x,y,z,ax,ay,az,angle;
// dWorldID w;
w = dWorldCreate();
dWorldSetGravity(w,0,0,-9.81);
//dWorldSetContactSurfaceLayer(w,0.0005);
// dBodyID b2;
//dGeomID gb2;
dJointGroupID kontakgroup;
kontakgroup = dJointGroupCreate(0);
dTriMeshDataID mesh1;
// dSpaceID ruang;
ruang = dSimpleSpaceCreate(0);

dReal sudut,xs,ys,zs;
dQuaternion q1;
b2=dBodyCreate(w);
dMass m2;
dMassSetBoxTotal(&m2, 0.1,0.087,0.0625,0.0003);
dBodySetMass(b2, &m2);
//gb2 = dCreateBox(ruang,0.087,0.0625,0.00003);
mesh1 = dGeomTriMeshDataCreate();
stlreader s1("kartu6.stl");
//s1.geserketengah();
//s1.geserkeorigin();
s1.jadikantrimesh(mesh1);
gb2 = dCreateTriMesh(ruang, mesh1,0,0,0);
dGeomSetBody(gb2, b2);

dBodySetPosition(b2,0,0,0.0003);
//dBodySetLinearVel(b2,0,0,1.5);
// dQFromAxisAndAngle (q1,0.1,0.1,0.1,0);
//dBodySetQuaternion(b2,q1);
dBodySetAngularVel(b2,0,50,0);
glantai = dCreatePlane(ruang,0,0,1,0);
bodystlconnector bodystl1(&s1, b2);
bodydrawer drawer(mainsurf);
drawer.setorigin(WWIDTH/2, WHEIGHT*3/4);
drawer.setwarna(255,0,0);
drawer.setskala(1000);
drawer.setproyeksi(PROYEKSI_XZ);
dReal deltat=0.01;
// dReal x,y,z;
//ofstream Morison_File ("linear_wave_loading.csv");
//Morison_File <<"x"<<","<<"y"<<","<<"z"<<","<<"sudut"<<","<<"xs"<<","<<"ys"<<","<<"zs"<< endl;
//for(int i=1;i<=300;i++){


int running=1;
while(running){dSpaceCollide(ruang,0,&kontakcallback);
//dWorldSetGravity(w,0,0,-9.81);
dWorldQuickStep(w, deltat);

dJointGroupEmpty(kontakgroup);
// drawer.tampiltitik(b2);
bersih(mainsurf, SDL_MapRGB(mainsurf->format,0,0,0));
drawer.tampilstl(&bodystl1);
garis(mainsurf,0,450,800,450,0x00800000);
if(SDL_PollEvent(&E)){
switch(E.type)
{
case SDL_KEYDOWN:
switch(E.key.keysym.sym)
{
case SDLK_RETURN:
running=0;
break;
default:;
}
}
}
// bersih(mainsurf,SDL_MapRGB(mainsurf->format,0,0,0));
//tulis("spasi untuk reset, enter untuk keluar",mainsurf,0,0,fontsurf);
// drawer.tampilstl(&bodystl1);
/*
float x2 = dBodyGetPosition(b2)[0];
float y2 = dBodyGetPosition(b2)[2];
lingkaran(mainsurf,x2*10+WWIDTH/2, WHEIGHT-y2*10,40,SDL_MapRGB(mainsurf->format,0,255,255));
*/
//drawer.tampillingkaran(b2,4);
SDL_UpdateWindowSurface(window);
//SDL_Delay(50);

}

return 0;
}
Last edited on
Have you realized that SDL goes "opposite way". That being top left corner is 0,0 and bottom right is window height and width?

Flipping gravity from -9.81 to 9.81 should fix something if it is not correctly handled on physics engine which is not seen on the code. If there is compensation on the physics side already (-1)*(-1)=1 and that makes issue
Last edited on
Thank you for your response! I tried that,and it doesn't solve the problem. When I set the angular velocity to 0,0,0 , The card isn't just lying on the ground. It did at first,but then it's flying. I tried to modify the erp and cfm part, and found that if I set the erp to zero then it wouldn't fly,but it wouldn't flip if I set the angular velocity either.
If I set the erp to 0.x(say,0.5) the card would flip but then bounce indefinitely. I also tried to disable erp and cfm,enabling only bounce,but this still happens. Also tried to disable bounce but enable erp and cfm,still the same. Is there any workaround? TIA!
Last edited on
Update - Just noticed there are double lines. Solved.
Topic archived. No new replies allowed.