move - slow down rectangle in SDL2/C -
move - slow down rectangle in SDL2/C -
hallo seek have real gravity in programme smaller rectangle should slow downwards when dont press key. code tried out, nil happened(in aspect):
#include <stdio.h> #include <sdl2/sdl.h> int main() { //request successful execute if(sdl_init(sdl_init_everything)!=0) { printf("fehler"); homecoming 1; } //create window 800x600 sdl_window *win; win = sdl_createwindow("test",100,100,800,600,sdl_window_shown); //create renderwindow vysnc enabled sdl_renderer *ren = sdl_createrenderer(win, -1, sdl_renderer_accelerated | sdl_renderer_presentvsync); if(ren==null) { printf("fehler"); sdl_quit(); homecoming 1; } //boolean info type not supported int gamerunning = 1; sdl_event event; const uint8 *keystate=sdl_getkeyboardstate(null); //create rectangle whole window sdl_rect rect={ 0,0,800,600 }; //smaller rectangle sdl_rect rect2={ 10,10,50,50 }; float v_x = 0; float v_y = 0; float speedup = 1.f; //while loop hold window opened while (gamerunning) { if (sdl_pollevent(&event))//close window { if (event.type == sdl_quit) { gamerunning = 0; } } //color of first rectangle(red) sdl_setrenderdrawcolor(ren, 255,0,0,255); sdl_renderclear(ren); sdl_renderfillrect( ren, &rect ); //second rectangle (blue) sdl_setrenderdrawcolor(ren, 0,0,255,255); sdl_renderfillrect( ren, &rect2 ); //change/update render sdl_renderpresent(ren); if(keystate[sdl_scancode_lshift]) { if (keystate[sdl_scancode_a] ) { v_x -= speedup; } if (keystate[sdl_scancode_d] ) { v_x += speedup; } if (keystate[sdl_scancode_s] ) { v_y += speedup; } if (keystate[sdl_scancode_w] ) { v_y -= speedup; } } else if(sdl_keydown) { if (keystate[sdl_scancode_a] ) { v_x -= 0.1f; } if (keystate[sdl_scancode_d] ) { v_x += 0.1f; } if (keystate[sdl_scancode_s] ) { v_y += 0.1f; } if (keystate[sdl_scancode_w] ) { v_y -= 0.1f; } } else { //make slower(doesnt work) v_x= -v_x * 1.f; v_y= -v_y * 1.f; if(v_x<0.005f || v_y<0.005f) { v_x=0; v_y=0; } } rect2.x += v_x; rect2.y += v_y; if(rect2.x<0) v_x= -v_x * 0.8f; else if(rect2.x>=(800-50)) v_x= -v_x * 0.8f; if(rect2.y<0) v_y= -v_y * 0.8f; else if(rect2.y>=(600-50)) v_y= -v_y * 0.8f; } //give heap free sdl_destroyrenderer(ren); sdl_destroywindow(win); sdl_quit(); homecoming 0; }
my thought overwrite gravity v_x , v_y every frame. whats problem? when rect nail border v_x/v_y becomes slower , same code. ps:i utilize c , lib sdl2.
current codepart:
else if(event.type==sdl_keydown) { if (keystate[sdl_scancode_a] ) { v_x -= 0.1f; } if (keystate[sdl_scancode_d] ) { v_x += 0.1f; } if (keystate[sdl_scancode_s] ) { v_y += 0.1f; } if (keystate[sdl_scancode_w] ) { v_y -= 0.1f; } } //make slower(doesnt work) else { v_x-=0.0000005; v_y-=0.0000005; if(v_x<0.000005f || v_y<0.000005f) { v_x=0; v_y=0; } }
your first problem had:
else if (sdl_keydown)
which true else
branch never reached. should be:
else if (event.type == sdl_keydown)
the next problem velocity slowing code (note: spaces free , makes easier read):
v_x -= 0.0000005; v_y -= 0.0000005; if (v_x < 0.000005f || v_y < 0.000005f) { v_x = 0; v_y = 0; }
this works if initial velocity positive. if either velocity negative sets both 0. there few ways correctly, simplest explicitly checking:
const float min_speed = 0.000005f; // add together top of function or global ... if (v_x > min_speed) v_x -= min_speed; else if (v_x < -min_speed) v_x += min_speed; else v_x = 0;
and same check v_y
.
c move sdl-2 rect
Comments
Post a Comment