Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: Positron on September 12, 2007

Title: Twister problem
Post by: Positron on September 12, 2007
I'm trying to code a twister, but it's looking wrong.
I can't find the problem. It would be fine, if someone has an idea.

Code: [Select]
m_hr = mpBack->LockRect(&Rect,NULL,0);
if (m_hr == D3D_OK)
{
DWORD *pMem = (DWORD*) Rect.pBits;
int corPitch = Rect.Pitch /4;
for(int x=x_max-1;x>0;x--)
{

lstRubber[x].py[0] = (float)lstRubber[x-1].py[0];
lstRubber[x].py[1] = (float)lstRubber[x-1].py[1];
lstRubber[x].py[2] = (float)lstRubber[x-1].py[2];
lstRubber[x].py[3] = (float)lstRubber[x-1].py[3];
}

lstRubber[0].py[0] = (y_mul * sinf((mPos*oneD)))+y_ofs;
lstRubber[0].py[1] = (y_mul * sinf((mPos+90.0f)*oneD))+y_ofs;
lstRubber[0].py[2] = (y_mul * sinf((mPos+180.0f)*oneD))+y_ofs;
lstRubber[0].py[3] = (y_mul * sinf((mPos+270.0f)*oneD))+y_ofs;
DWORD* pTex = (DWORD*) Textures[0];
float xTexStep = x_max/256.0f;
float xTexPos  = 0.0f;
float yTexStep = 0.0f;

for(int x=0;x<x_max;x++)
{
int y2;
for (int i = 0; i<4;i++)
{
if (i==3)
y2 = 0;
else
y2 = i+1;

if (lstRubber[x].py[y2]>lstRubber[x].py[i])
{
float linLength = lstRubber[x].py[y2]-lstRubber[x].py[i];

float colorMul =linLength/y_mul;
//float colorMul = 1.0f;
yTexStep = linLength/256.0f;
float yTexPos = 0.0f;
int tmpX = xTexPos;
for (int y = lstRubber[x].py[i]; y <= lstRubber[x].py[y2]; y++)
{
int tmpY = yTexPos;

DWORD thisColor = pTex[tmpY*256+tmpX];
DWORD red = (UINT)(((thisColor & 0x00ff0000) >> 16) * colorMul);
DWORD green = (UINT)(((thisColor & 0x0000ff00) >> 8) * colorMul);
DWORD blue = (UINT)((thisColor & 0x000000ff) * colorMul);
thisColor = 0xff000000 | (red <<16) | (green<<8) | blue;
pMem[(y* corPitch)+x] = thisColor;
yTexPos += yTexStep;
}
}
}
xTexPos += xTexStep;

}
Title: Re: Twister problem
Post by: Shockwave on September 12, 2007
I can't start your application here for some reason, looking through your code though I think you are over complicating it.

You should be able to draw the whole twister in one loop.

I will explain the technique which is very simple. I should know, I've made loads of them :)
Let's suppose that you have 4 sides on your twister and that your screen is 480 pixels tall, you could use any resolution you want.

This will be done in pseudo code as C++ is not my own language of choice.


FOR Y = 0 TO 479

First calculate 4 points;

X1 = 100 + 50 * SIN ( ANGLE + Y )
X2 = 100 + 50 * SIN ( (ANGLE+90) + Y )
X3 = 100 + 50 * SIN ( (ANGLE+180) + Y )
X4 = 100 + 50 * SIN ( (ANGLE+270) + Y )

Then we do a simple test to see if the line is visible and then draw it if it is;

IS X2>X1? IF YEST DRAW LINE FROM X1 TO X2
IS X3>X2? IF YEST DRAW LINE FROM X2 TO X3
IS X4>X3? IF YEST DRAW LINE FROM X3 TO X4
IS X1>X4? IF YEST DRAW LINE FROM X4 TO X1

NEXT Y

A few points of interest, you should add something to the variable ANGLE each frame so that the twister rotates.

You'll notice also that ANGLE+90 etc, as the points of a square are at 90 degree intervals, that's why we do this.


You can make the calculation to generate the points a little bit more interesting if you want to make a more pleasing twister.

I am working on one at the moment, the source is in Freebasic though if you'd like to look at it it is not a problem.

Title: Re: Twister problem
Post by: Paul on September 12, 2007
I don't know about your twister but the rest is looking good except for one thing, the round vista loading pointer is showing in the middle of everything.
Title: Re: Twister problem
Post by: Positron on September 12, 2007
I think you are over complicating it.

This is the result of two days error seeking.  ;D

You can make the calculation to generate the points a little bit more interesting if you want to make a more pleasing twister.
I am working on one at the moment, the source is in Freebasic though if you'd like to look at it it is not a problem.
Sure I'm interested in. It's allway nice to have the chance to learn.

I will throw away the source and start coding again. I think tomorrow I can tell more.

Btw. how that karma thing is working?

And thanks of course!

Quote from: Paul
I don't know about your twister but the rest is looking good except for one thing, the round vista loading pointer is showing in the middle of everything.
Just now I haven't got Vista, but I try to get my hands on soon.
Title: Re: Twister problem
Post by: Positron on September 12, 2007
I can't start your application here for some reason,
This exe don't start here neither. The next exe will.  ;)
Title: Re: Twister problem
Post by: Jim on September 12, 2007
It starts OK for me :)
Here's the source for another twisting bar demo
http://dbfinteractive.com/index.php?topic=752.0 (http://dbfinteractive.com/index.php?topic=752.0)
I thnk it's a little less complicated than some others, but the maths might not be as obvious!
You can download the yabasic player here http://dbfinteractive.com/index.php?topic=758.0 (http://dbfinteractive.com/index.php?topic=758.0)

Jim
Title: Re: Twister problem
Post by: va!n on September 13, 2007
@Jim:
Very very nice example! K++

@all:
Here is my first try to code a nice and cool oldskool twister effect... i will release the PB source when i have cleaned it up a bit ^^
Title: Re: Twister problem
Post by: Positron on September 13, 2007
@Jim, Va!n
Thanks!

After I have coded the whole thing again everything works fine. I don't know what was wrong by the first attempt.
I will post the twister-source after I have released this intro.
Title: Re: Twister problem
Post by: benny! on September 13, 2007
@sw:
Again, awesome kind of little tutorial. Well done!

@va!n:
Nice PB conversion - runs like a charme here !!!

@Positron:
Looking forward to see the intro. When do you think you gonna release it ?
Title: Re: Twister problem
Post by: Positron on September 13, 2007
@Positron:
Looking forward to see the intro. When do you think you gonna release it ?
I haven't got a fixed plan for release date. Possibly this weekend.
Title: Re: Twister problem
Post by: Shockwave on September 13, 2007
Looking forward to it :)

Make sure that you adda little test to see how long the line you are drawing is, the longer the line, the lighter the colour and it will have a nice fake light sourcing effect.

And then if you really want to hide the fake light sourcing, you can draw some metaballs over the top to give a specular highlight sort of effect :)
Title: Re: Twister problem
Post by: Positron on September 13, 2007
Make sure that you adda little test to see how long the line you are drawing is, the longer the line, the lighter the colour and it will have a nice fake light sourcing effect.
I have already done the fake light sourcing effect. Looks nice. ;D

Your intro looks very nice. Already planned a release date?
Title: Re: Twister problem
Post by: Shockwave on September 13, 2007
Ehm, well I am waiting for a song from Ampli and the scroll texts from some other S!P members before I can release :) I think yours will be released first, Ampli is quite busy with work at the moment.
Title: Re: Twister problem
Post by: Positron on September 13, 2007
Song!  :inspired:
Dawned I forgot we have no song. I think no release this weekend!
Title: Re: Twister problem
Post by: Positron on September 13, 2007
Hey,

I can give karma now.

Shockwave k++ for always being helpful!

Title: Re: Twister problem
Post by: Shockwave on September 13, 2007
Thanks :)

Btw, sorry I should have added a description about Karma somewhere on the forum, people with more than 25 posts can give good or bad karma every 3 hours :)
Title: Re: Twister problem
Post by: va!n on September 19, 2007
Sorry for the big delay due fact of some internet provider problems... here you will find a PB source version...

http://dbfinteractive.com/index.php?topic=2430.0 (http://dbfinteractive.com/index.php?topic=2430.0)
Title: Re: Twister problem
Post by: Positron on September 20, 2007
@Va!n
Thanks.
The twister already works. Now I'm waiting for the gfx and maybe I change the design a little bit.
Than I will release it.