Sin() and Cos() Problem

This is a discussion on Sin() and Cos() Problem within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make basically just the game snake. I'm using the Allegro graphics library. I'm trying to use sin ...

  1. #1
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55

    Question Sin() and Cos() Problem

    I'm trying to make basically just the game snake. I'm using the Allegro graphics library. I'm trying to use sin and cos to find the direction the snake should go, but I keep getting the error 'sin': ambiguous call to overloaded function. I don't know what that means or what the problem is. But here's my code:
    Code:
    //Includes
    #include <allegro.h>
    #include<iostream>
    #include<string>
    #include <stdlib.h>
    #include <stdlib.h> 
    #include <cstdlib>
    #include <time.h>
    #include <vector>
    #include <complex>
    #include <math.h>
    
    
    
    //Graphics
    BITMAP *snakeimg = NULL;
    BITMAP *buffer = NULL;
    
    //Sounds
    
    //Variables
    int x,y;
    int dir = 0;
    int dx = sin(dir);
    int dy = cos(dir);
    
    
    //Structs
    struct snake{
    	int x;
    	int y;
    };
    
    struct fruit{
    	int x;
    	int y;
    };
    
    //Vectors
    vector<snake> snakes(0);
    
    //Functions
    void UpdateSnake();
    
    int main(int argc, char argv[1])
    {
    	using namespace std;
    	allegro_init();
    	install_keyboard();
    	set_color_depth(24);
    	set_gfx_mode(GFX_AUTODETECT_WINDOWED,400,400,0,0);
    
    	//BITMAPS
    	snakeimg = load_bitmap("Snake.bmp",NULL);
    	buffer = create_bitmap(400,400);
    
    	while(!key[KEY_ESC])
    	{
    	UpdateSnake();
    	draw_sprite(screen,buffer,0,0);
    	}
    
    	allegro_exit();
    	return 0;
    
    	//Cleanup
    	for(int i = 0;i < snakes.size();
    	{
    		snakes.erase(snakes.begin()+i);
    	}
    }
    
    	destroy_bitmap(snakeimg);
    	destroy_bitmap(buffer);
    END_OF_MAIN();
    
    void UpdateSnake()
    {
    	dx = Sin(dir);
    	dy = Cos(dir);
    	x = x + dx;
    	y = y + dy;
    
    	draw_sprite(buffer,snakeimg,x,y);
    }
    Please help if you have any idea what the problem is. I'm pretty sure I have the right include file.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    It should be sin, not Sin, and sin takes radians as a floating point value. The compiler cannot figure out whether you want the sin that works with double, float, or long double.

  3. #3
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    Oh I see. I changed all the variables to floats and it works now, except that the snake only moves if dir=0. otherwise, the snake just sits there.

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Not part of your problem, but whats up with using <stdlib.h>, <stdlib.h>, and <cstdlib>. The same header twice? wh..why, and cstdlib is the C++ version of stdlib.h, so you dont need either of those stdlib.h's. It should also be <cmath>, not <math.h>, and <ctime>, not <time.h>.

    The only way I could get sin / cos to work right is: the_degree * PI / 180.0. Check cplusplus: http://www.cplusplus.com/ref/cmath/sin.html, PI radians.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    I did a lot of copy and paste with the includes. I must have done it on accident.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with cos, sin fuctions
    By xxxhunter in forum C Programming
    Replies: 7
    Last Post: 11-16-2007, 02:33 AM
  2. Help with cos, sin and tan^-1 (arctan);
    By xIcyx in forum C Programming
    Replies: 15
    Last Post: 04-23-2007, 09:14 AM
  3. Help creating function of sin cos and tan
    By Niz in forum C Programming
    Replies: 12
    Last Post: 01-09-2007, 04:55 PM
  4. sin() and cos() that use degrees
    By dwks in forum C Programming
    Replies: 3
    Last Post: 05-14-2005, 04:29 PM
  5. integral of 1/x
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 03-19-2004, 06:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21