Sin and cos not working.
Posted: Sat Jan 22, 2022 1:04 am
Hi! Why is sin and cos not working?
I have math.h imported. but sin and cos are not working. Why is this?
Code:Why is this not working?
P.S. I am following a tutorial.
I have math.h imported. but sin and cos are not working. Why is this?
Code:
Code: Select all
#include "Intellisense.h"
#include <gba_interrupt.h>
#include <gba_systemcalls.h>
#include "gba_input.h"
#include <stdint.h>
#include <math.h>
#include <stdbool.h>
typedef uint8_t u8; typedef int8_t s8;
typedef uint16_t u16; typedef int16_t s16;
typedef uint32_t u32; typedef int32_t s32;
typedef volatile uint8_t v_u8; typedef volatile int8_t v_s8;
typedef volatile uint16_t v_u16; typedef volatile int16_t v_s16;
typedef volatile uint32_t v_u32; typedef volatile int32_t v_s32;
#define REG_DISPCNT *((v_u32*)(0x04000000))
#define VIDEOMODE_3 0x0003
#define BGMODE_2 0x0400
#define SCREENBUFFER ((v_u16*)(0x06000000))
#define SCREEN_W 240
#define SCREEN_H 160
#define PI 3.14159265
double sin(double o){
if (o > 6)
{
double y = o - 6;
} else {
double y = o;
}
if (o == 0.1)
{
return 0.09983341664;
}
if (o == 0.2)
{
return 0.19866933079;
}
if (o == 0.3)
{
return 0.29552020666;
}
if (o == 0.4)
{
return 0.3894183423;
}
if (o == 0.5)
{
return 0.4794255386;
}
if (o == 0.6)
{
return 0.56464247339;
}
if (o == 0.7)
{
return 0.64421768723
}
if (o == 0.8)
{
return 0.7173560909;
}
if (o == 0.9)
{
return 0.78332690962;
}
if (o == 0)
{
return 0;
}
}
void delay(int ms)
{
for (int i = 0; i < ms; i++)
{
VBlankIntrWait();
}
}
s32 abs(s32 a_val)
{
s32 mask = a_val >> 31;
return (a_val ^ mask) - mask;
}
u16 setColor(u8 a_red, u8 a_green, u8 a_blue)
{
return (a_red & 0x1F) | (a_green & 0x1F) << 5 | (a_blue & 0x1f) << 10;
}
void plotPixel( u32 a_x, u32 a_y, u16 a_colour)
{
SCREENBUFFER[a_y * SCREEN_W + a_x] = a_colour;
}
void drawRect(u32 a_left, u32 a_top, u32 a_width, u32 a_height, u16 a_color)
{
for (u32 y = 0; y < a_height; ++y)
{
for (u32 x = 0; x < a_width; ++x)
{
SCREENBUFFER[ (a_top + y) * SCREEN_W + a_left + x ] = a_color;
}
}
}
void drawLine(u32 a_x, u32 a_y, u32 a_x2, u32 a_y2, u16 a_colour)
{
//Get the horizontal and vertical displacement of the line
s32 w = a_x2 - a_x; //w is width or horizontal distance
s32 h = a_y2 - a_y; //h is the height or vertical displacement
//work out what the change in x and y is with the d in these variables stands for delta
s32 dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
if (w<0) dx1 = dx2 = -1; else if (w>0) dx1 = dx2 = 1;
if (h<0) dy1 = -1; else if (h>0) dy1 = 1;
//which is the longest the horizontal or vertical step
s32 longest = abs(w); //assume that width is the longest displacement
s32 shortest = abs(h);
if ( shortest > longest ) //oops it's the other way around reverse it
{
//use xor to swap longest and shortest around
longest ^= shortest; shortest ^= longest; longest ^= shortest;
if (h<0) dy2 = -1; else if (h>0) dy2 = 1;
dx2 = 0;
}
//geta value that is half the longest displacement
s32 numerator = longest >> 1;
//for each pixel across the longest span
for (s32 i = 0; i <= longest; ++i)
{
//fill the pixel we're currently at
plotPixel( a_x, a_y, a_colour);
//increase the numerator by the shortest span
numerator += shortest;
if (numerator>longest)
{
//if the numerator is now larger than the longest span
//subtract the longest value from the numerator
numerator -= longest;
//increment x & y by their delta1 values
a_x += dx1;
a_y += dy1;
}
else
{
//numerator is smaller than the longst side
//increment x & y by their delta 2 values
a_x += dx2;
a_y += dy2;
}
}
}
int x = 5, y = 5, pa = PI/2, pdx, pdy;
int main(void) {
REG_DISPCNT = VIDEOMODE_3 | BGMODE_2;
irqInit();
irqEnable(IRQ_VBLANK);
while (1) {
PollKeys();
if (keyDown(LEFT))
{
pa-=0.1;
if (pa < 2*PI){ pa -= 2*PI; }
pdx = cos(pa) * 5;
pdy = sin(pa) * 5;
}
if (keyDown(RIGHT))
{
pa+=0.1; if (pa > 2*PI){ pa += 2*PI; }
pdx = cos(pa) * 5;
pdy = sin(pa) * 5;
}
if (keyDown(UP))
{
x += pdx;
y += pdy;
}
if (keyDown(DOWN))
{
x -= pdx;
y -= pdy;
}
plotPixel(x, y, setColor(31, 31, 0));
delay(3);
plotPixel(x, y, setColor(0, 0, 0));
}
}
P.S. I am following a tutorial.