Created
August 9, 2015 04:56
-
-
Save nurettin/aa3f1cd4be68b6047fba to your computer and use it in GitHub Desktop.
ode gravity lost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <ode/ode.h> | |
#include <SDL.h> | |
#include <SDL2_gfxPrimitives.h> | |
dWorldID earth; | |
dJointGroupID contact_joint_group; | |
std::size_t const max_contacts = 10; | |
dContact contact[max_contacts]; | |
void collision_callback(void*, dGeomID g1, dGeomID g2){ | |
for(std::size_t i= 0; i< max_contacts; ++ i){ | |
contact[i].surface.mode = dContactBounce; | |
contact[i].surface.mu = 100.f; | |
contact[i].surface.bounce = .5f; | |
//contact[i].surface.bounce_vel = 0.f; | |
} | |
int n_contacts= dCollide(g1, g2, max_contacts, &contact[0].geom, sizeof(dContact)); | |
if(!n_contacts) | |
return; | |
auto b1= dGeomGetBody(g1); | |
auto b2= dGeomGetBody(g2); | |
for(int i= 0; i< n_contacts; ++ i){ | |
auto contact_joint= dJointCreateContact(earth, contact_joint_group, &contact[i]); | |
dJointAttach(contact_joint, b1, b2); | |
} | |
} | |
int main(){ | |
int err = SDL_Init(SDL_INIT_VIDEO); | |
if(err == -1){ | |
std::cerr<< "Error initializing SDL\n"; | |
return EXIT_FAILURE; | |
} | |
dInitODE(); | |
// Create earth with gravity | |
earth = dWorldCreate(); | |
//dWorldSetERP(earth, 0.9); | |
//dWorldSetCFM(earth, 0.00001); | |
// Define ball and platform geometries | |
auto collision_space= dSimpleSpaceCreate(0); | |
auto ball_geom= dCreateSphere(collision_space, 40.f); | |
auto platform_geom= dCreateBox(collision_space, 100.f, 40.f, 1.f); | |
auto contact_joint_group = dJointGroupCreate(0); | |
// Define ball body and mass | |
auto ball = dBodyCreate(earth); | |
dGeomSetBody(ball_geom, ball); | |
dMass ball_mass; | |
dMassSetSphereTotal(&ball_mass, 10.0, 40.0); | |
dBodySetMass(ball, &ball_mass); | |
dGeomSetPosition(ball_geom, 320.f, 50.f, 0.f); | |
dGeomSetPosition(platform_geom, 320.f, 430.f, 0.f); | |
// Prepare drawing window | |
auto window = SDL_CreateWindow("test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL); | |
auto renderer = SDL_CreateRenderer(window, -1, 0); | |
// Main Loop | |
SDL_Event e; | |
for(bool run = true; run;){ | |
dWorldSetGravity(earth, 0, 9.81, 0); | |
// Handle Events | |
while(SDL_PollEvent(&e)){ | |
switch(e.type){ | |
case SDL_QUIT: | |
run = false; | |
} | |
} | |
// Animate | |
auto ball_pos = dBodyGetPosition(ball); | |
auto platform_pos = dGeomGetPosition(platform_geom); | |
dSpaceCollide(collision_space, 0, collision_callback); | |
dWorldStep(earth, 0.01); | |
dJointGroupEmpty(contact_joint_group); | |
dBodySetGravityMode(ball, 1); // But I seem to be losing gravity attached to this object | |
// Draw | |
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | |
SDL_RenderClear(renderer); | |
filledCircleRGBA(renderer, ball_pos[0], ball_pos[1], 40, 0, 255, 0, 255); | |
boxRGBA(renderer, platform_pos[0]- 50, platform_pos[1]- 20, platform_pos[0] + 50, platform_pos[1]+ 20, 255, 0, 0, 255); | |
SDL_RenderPresent(renderer); | |
} | |
// Cleanup ODE | |
dBodyDestroy(ball); | |
dWorldDestroy(earth); | |
dCloseODE(); | |
// Cleanup SDL | |
SDL_DestroyRenderer(renderer); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment