DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 16th April 2012
xmorg xmorg is offline
Real Name: Tim Cooper
Fdisk Soldier
 
Join Date: Sep 2010
Location: San Diego
Posts: 56
Question 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
Now every time I click the mouse I get a x and y from the mouse click, lets just say mousecx, and mousecy I also have and x and y from the player sprites rectangle(rcSprite->x and rcSprite->y) Now, how do I move my rect to the mouse click coordinates? I need it to move incrementally every iteration until it reaches its destination. Maybe its Geometry? CPCTE???

I put my code up on my site
http://www.cooperlabs.net/packages/sdldf.tar.gz
Reply With Quote
  #2   (View Single Post)  
Old 16th April 2012
roddierod's Avatar
roddierod roddierod is offline
Real Name: Rod Person
VPN Cryptographer
 
Join Date: Apr 2008
Location: Pittsburgh, Pa
Posts: 437
Default

If I'm reading your question correctly...

Add one(or what every you want your increment to be) to the x and y of the sprite until it equals the the x and y of the mouse.
__________________
"The basic tool for the manipulation of reality is the manipulation of words. If you can control the meaning of words, you can control the people who must use the words." -Philip K. Dick
Reply With Quote
  #3   (View Single Post)  
Old 17th April 2012
teig teig is offline
Port Guard
 
Join Date: May 2008
Posts: 20
Default

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.
Reply With Quote
  #4   (View Single Post)  
Old 17th April 2012
xmorg xmorg is offline
Real Name: Tim Cooper
Fdisk Soldier
 
Join Date: Sep 2010
Location: San Diego
Posts: 56
Default

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
Reply With Quote
Reply

Tags
c programming, sdl

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT. The time now is 11:51 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick