Author Topic: [Ndless] Tunnel  (Read 27756 times)

0 Members and 2 Guests are viewing this topic.

Offline Spenceboy98

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 547
  • Rating: +59/-2
    • View Profile
[Ndless] Tunnel
« on: June 17, 2013, 04:08:44 pm »
Hello all. I saw this tutorial on how to make a tunnel and I decided to port it to Nspire. I'm having problems though. It only displays half of the tunnel correctly(the right side).

Could you guys help(I directly ported the code from the tutorial)?

Gif is attached(faster on calc).
I like milk.

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: [Ndless] Tunnel
« Reply #1 on: June 17, 2013, 05:24:58 pm »
Well, most likely you made a mistake while porting, so we'd need to look at your code to help?

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [Ndless] Tunnel
« Reply #2 on: June 17, 2013, 05:26:48 pm »
You might have to post the code so C users can help you easier. By the way this looks kinda nice so far. How will obstacles be drawn? Also are the vertical lines due to some compression to save speed? Not that it looks bad, but just curious.

Offline Spenceboy98

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 547
  • Rating: +59/-2
    • View Profile
Re: [Ndless] Tunnel
« Reply #3 on: June 17, 2013, 06:05:05 pm »
I don't know why the lines are there. :P

Here is my code:
Spoiler For Code:
Code: [Select]
#include <os.h>
#include "utils.c"
#include "graphics3.c"
#include "touchpad.c"

#define texWidth 256
#define texHeight 256
#define screenWidth 240
#define screenHeight 240

int w = 240, h = 240, x, y;

int texture[texWidth][texHeight];

int distanceTable[screenWidth][screenHeight];
int angleTable[screenWidth][screenHeight];
int buffer[screenWidth][screenHeight];

void masksprite(void *buffer, int *data, int x, int y, int width, int height, int mask){
int i, j;
for(i = 0; i < height; i++){
for(j = 0; j < width; j++){
if(data[i*width+j] != mask && x+j < 320 && x+j > 0 && y+i < 240 && y+i > 0){
setPixelBuf(buffer, x+j, y+i, data[i*width+j]);
}
}
}
}

uint16_t RGBColor(uint8_t r, uint8_t g, uint8_t b)
{
  return ((r / 8) << 11) | ((g / 4) << 5) | (b / 8);
}

int main(void) {
char *screen;
screen = (char*)malloc(SCREEN_BYTES_SIZE * sizeof(char));     // just a buffer
if(!screen)
{
exit(0);
}
initTP();
lcd_ingray();
for(x = 0; x < texWidth; x++)
    for(y = 0; y < texHeight; y++)
    {
uint8_t c = x ^ y;
        texture[x][y] = RGBColor(c, c, c);
    }
for(x = 0; x < w; x++)
    for(y = 0; y < h; y++)
    {
        int angle, distance;
        float ratio = 32.0;
        distance = (int)(ratio * texHeight / sqrt((x - w / 2.0) * (x - w / 2.0) + (y - h / 2.0) * (y - h / 2.0))) % texHeight;
        angle = (unsigned int)(0.5 * texWidth * atan2(y - h / 2.0, x - w / 2.0) / M_PI);
        distanceTable[x][y] = distance;
        angleTable[x][y] = angle;
    }
float animation = 0;
int shiftY = (int)(texHeight * 0.25);
    //begin the loop
    while(!isKeyPressed(KEY_NSPIRE_ESC))
    {
        animation = animation+0.01;
readTP();
int TZ = getTouchedZone4();
        //calculate the shift values out of the animation value
        int shiftX = (int)(texWidth * 1.0 * animation);
if(isKeyPressed(KEY_NSPIRE_RIGHT) || TZ==6)
shiftY++;
if(isKeyPressed(KEY_NSPIRE_LEFT) || TZ==4)
shiftY--;
        for(x = 0; x < w; x++)
        for(y = 0; y < h; y++)
        {
            //get the texel from the texture by using the tables, shifted with the animation values
            int color = texture[(unsigned int)(distanceTable[x][y] + shiftX)  % texWidth][(unsigned int)(angleTable[x][y] + shiftY) % texHeight];
            buffer[x][y] = color;
        }
        masksprite(screen, buffer[0], 0, 0, 240, 240, 0xf800);
refresh(screen);
clearBuf(screen);
    } 
free(screen);
endTP();
return 0;
}

I like milk.

Offline Spenceboy98

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 547
  • Rating: +59/-2
    • View Profile
Re: [Ndless] Tunnel
« Reply #4 on: June 18, 2013, 02:04:51 pm »
*BUMP*
Could someone please help? I have no idea why it's doing this. :/
I like milk.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [Ndless] Tunnel
« Reply #5 on: June 18, 2013, 02:35:06 pm »
I can't see your code since my phone is real shit, but seeing at the result it seems that somehow your implementation of the algorithm doesn't work with negative numbers. I'd recommend casting the angle and depth to unsigned int before using the modulo when you get the texels.

Offline Spenceboy98

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 547
  • Rating: +59/-2
    • View Profile
Re: [Ndless] Tunnel
« Reply #6 on: June 18, 2013, 02:51:37 pm »
It has
Code: [Select]
(unsigned int)(distanceTable[x][y] + shiftX) already, but I made it
Code: [Select]
(unsigned int)((unsigned int)distanceTable[x][y] + shiftX) and there's no change. Any other ideas?
« Last Edit: June 18, 2013, 02:54:54 pm by Spenceboy98 »
I like milk.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [Ndless] Tunnel
« Reply #7 on: June 18, 2013, 03:16:42 pm »
No, I didn't mean that ; how do you calculate x and y from the code you posted ? Those are the ones you need to cast to unsigned int before moduling them.

Offline Spenceboy98

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 547
  • Rating: +59/-2
    • View Profile
Re: [Ndless] Tunnel
« Reply #8 on: June 18, 2013, 03:26:29 pm »
x and y are just from a for loop:
Code: [Select]
for(x = 0; x < w; x++)
for(y = 0; y < h; y++){
    color=texture[blah][blah];
    buffer[x][y]=color;
}
I like milk.

Offline Augs

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 306
  • Rating: +30/-29
    • View Profile
Re: [Ndless] Tunnel
« Reply #9 on: June 18, 2013, 03:47:48 pm »
I'm not an expert(In fact I have no idea how this works) but it looks to me that you are stretching the bottom of the texture across half the tunnel. Make sure that your variable(One of the texture drawing ones) isn't saying constant. 
« Last Edit: June 18, 2013, 03:48:26 pm by Augs »

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [Ndless] Tunnel
« Reply #10 on: June 18, 2013, 04:02:10 pm »
^ that, but also I was actually asking for the calculations you replaced by "blah" ;D

Offline Spenceboy98

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 547
  • Rating: +59/-2
    • View Profile
Re: [Ndless] Tunnel
« Reply #11 on: June 18, 2013, 04:12:41 pm »
Here are the values:
Code: [Select]
texture[((unsigned int)(distanceTable[x][y] + shiftX)  % texWidth)][((unsigned int)(angleTable[x][y] + shiftY) % texHeight)];
I like milk.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [Ndless] Tunnel
« Reply #12 on: June 18, 2013, 04:21:18 pm »
And that doesn't work ? Weird .__.

Try replacing the cast with abs(), just to be sure.

Offline Spenceboy98

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 547
  • Rating: +59/-2
    • View Profile
Re: [Ndless] Tunnel
« Reply #13 on: June 18, 2013, 04:25:27 pm »
And that doesn't work ? Weird .__.

Try replacing the cast with abs(), just to be sure.

Nope. Still doesn't work.
I like milk.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [Ndless] Tunnel
« Reply #14 on: June 19, 2013, 01:02:01 am »
Then be sure that you don't cast to unsigned too early in the calculations.

Can you post the code on pastebin or whatever, that I could look at it ?