Skip to content

Commit

Permalink
add sin,cos
Browse files Browse the repository at this point in the history
  • Loading branch information
corax89 committed Nov 21, 2019
1 parent d8ae6da commit f0ddd2d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
Binary file modified ESP8266_little_game_engine.ino.d1_mini.bin
Binary file not shown.
32 changes: 32 additions & 0 deletions cpu.ino
Expand Up @@ -291,6 +291,30 @@ uint16_t loadData(uint16_t arrayAddress){
return c;
}

int16_t fixed_sin(int x) {
//Bhaskara I's sine approximation sin(x°) = 4·x·(180−x)/(40500−x·(180−x))
char pos = 1; // positive - keeps an eye on the sign.
if (x < 0){
x = -x;
pos = !pos;
}
if (x >= 360)
x %= 360;
if (x > 180){
x -= 180;
pos = !pos;
}
int16_t nv = x * (180 - x);
int32_t s = (nv * 4 * (1 << MULTIPLY_FP_RESOLUTION_BITS))/(40500 - nv);
if (pos)
return (int16_t)s;
return (int16_t)-s;
}

inline int16_t fixed_cos(int16_t g){
return fixed_sin(g+90);
}

#ifdef ESPBOY
void setLedColor(uint16_t r5g6b5){
uint8_t r,g,b;
Expand Down Expand Up @@ -908,6 +932,14 @@ void cpuStep(){
else if (reg2 == 0x10) {
reg[reg1] = reg[reg1] / (1 << MULTIPLY_FP_RESOLUTION_BITS);
}
// SIN R C3 2R
else if (reg2 == 0x20) {
reg[reg1] = fixed_sin(reg[reg1]);
}
// SIN R C3 3R
else if (reg2 == 0x30) {
reg[reg1] = fixed_cos(reg[reg1]);
}
break;
case 0xC4:
// MULF R,R C4 RR
Expand Down
20 changes: 10 additions & 10 deletions display.ino
Expand Up @@ -155,17 +155,11 @@ void memoryAlloc(){
Serial.println(F("Out of memory"));
}

int16_t getCos(int16_t g){
g = g % 360;
if(g < 0)
g += 360;
inline int16_t getCos(int16_t g){
return (int16_t)(int8_t)pgm_read_byte_near(cosT + g);
}

int16_t getSin(int16_t g){
g = g % 360;
if(g < 0)
g += 360;
inline int16_t getSin(int16_t g){
return (int16_t)(int8_t)pgm_read_byte_near(sinT + g);
}

Expand Down Expand Up @@ -302,6 +296,9 @@ void setParticle(int8_t gravity, uint8_t count, uint16_t time){
}

void setEmitter(uint16_t time, int16_t dir, int16_t dir1, int16_t speed){
dir = dir % 360;
if(dir < 0)
dir += 360;
emitter.time = time;
emitter.speedx = (int8_t)((speed * getCos(dir)) >> 6);
emitter.speedy = (int8_t)((speed * getSin(dir)) >> 6);
Expand Down Expand Up @@ -626,6 +623,9 @@ void setSprPosition(int8_t n, uint16_t x, uint16_t y){
}

void spriteSetDirectionAndSpeed(int8_t n, uint16_t speed, int16_t dir){
dir = dir % 360;
if(dir < 0)
dir += 360;
sprite_table[n].speedx = ((speed * getCos(dir)) >> 6);
sprite_table[n].speedy = ((speed * getSin(dir)) >> 6);
}
Expand Down Expand Up @@ -1445,14 +1445,14 @@ void printc(char c, uint8_t fc, uint8_t bc){
}


void printfix(uint16_t value, uint8_t fc, uint8_t bc){
void printfix(int16_t value, uint8_t fc, uint8_t bc){
char sbuffer[10];
const uint16_t fractPartMask = (1 << MULTIPLY_FP_RESOLUTION_BITS) - 1;
int16_t j;
if(value == 0){
printc('0', color, bgcolor);
}
uint16_t intPart = value >> MULTIPLY_FP_RESOLUTION_BITS;
int16_t intPart = value >> MULTIPLY_FP_RESOLUTION_BITS;
value &= fractPartMask;
// преобразуем целую часть
itoa(intPart, sbuffer, 10);
Expand Down
2 changes: 1 addition & 1 deletion settings.h
Expand Up @@ -33,6 +33,6 @@
#define SCREEN_ARRAY_DEF SCREEN_SIZE
#define SCREEN_ADDR(x, y) ((int(y) << 6) + int(x))

#define MULTIPLY_FP_RESOLUTION_BITS 6
#define MULTIPLY_FP_RESOLUTION_BITS 8
#define PARTICLE_COUNT 32
#define EEPROM_SIZE 512

0 comments on commit f0ddd2d

Please sign in to comment.