|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|||
Its a math problem isnt it?
I must have been sleeping in word problem class. Problem: I am trying to move a sprite to the mouse click coordinates with SDL.
Every iteration I use this function (if game mode is hero moving) Code:
draw_sprites(gr, player, pdirection, ???, ??? ); //<---- void draw_sprites(GameRes *gr, Actor *a, int direction, int movex, int movey) { //GameRes*gr is a pointer to struct holding lots of game resources //int direction is only being used for which frame(s) our sprite should be displaying //Actor *a points to the player sprite, only x/y relevant gr->rcSprite->x += movex; //<----- gr->rcSprite->y += movey; //A lot of sprite frame code below I put my code up on my site http://www.cooperlabs.net/packages/sdldf.tar.gz |
|
|||
Assuming you want your sprite to move in a straight line, you would need to calculate the line between the two points and select a number of points on that line and move to them in each iteration.
Sounds simple doesn't it! Or look as the line as a vector, (vx, vy), divide by, say 10, and add deltavx,vy to your sprite 10 times. And then make sure it doesn't go past mousex,y in the last iteration. Something like that. |
|
|||
So basically, a lot of if/else logic, accounting for every direction?
like so: Code:
if(gc->mousecx < gr->rcSprite->x && gc->mousecy < gr->rcSprite->y) {} else if(gc->mousecx > gr->rcSprite->x && gc->mousecy > gr->rcSprite->y) {} else if(gc->mousecx < gr->rcSprite->x && gc->mousecy > gr->rcSprite->y) {} else if(gc->mousecx > gr->rcSprite->x && gc->mousecy < gr->rcSprite->y) {} if(gc->mousecx > gr->rcSprite->x) {player->screen_x = 1; pdirection = EAST; player->moving = 1;} else if(gc->mousecx < gr->rcSprite->x) {player->screen_x = -1; pdirection = WEST; player->moving = 1;} else if( gr->rcSprite->x == gc->mousecx ) {player->screen_x = 0; player->moving=0; gamestate = GAME_HERO_SELECTED;} else if(gc->mousecy > gr->rcSprite->y) {player->screen_y = 1; pdirection = SOUTH; player->moving = 1;} else if(gc->mousecy < gr->rcSprite->y) {player->screen_y = -1; pdirection = NORTH; player->moving = 1;} else if( gr->rcSprite->y == gc->mousecy ) {player->screen_y = 0; player->moving=0; gamestate = GAME_HERO_SELECTED;} //dont move |
Tags |
c programming, sdl |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Boot problem. Geometry problem? | gulanito | FreeBSD Installation and Upgrading | 0 | 3rd July 2009 03:03 AM |
Math resources | ephemera | Off-Topic | 16 | 3rd April 2009 12:06 PM |
My D-Link DFE530TX ethernet adapter isnt found. Help! | Quaxo | OpenBSD Installation and Upgrading | 15 | 10th February 2009 12:23 AM |