Archive: Sphere Intersection prob


4th July 2003 19:26 UTC

Sphere Intersection prob
Sphere Intersection Problem:

Ok, I have been playing a lot with dm's these days and I have treied to make multiple cylinders and spheres in one dm. Found that it was much easier to play around with cylinders and sphere's than planes. But, there is a problem here about the sphere intersection.

The ugly edges are too much, is there any way we can use alpha blending to blacken out the edges?
And then is there any way to enter the cylinders?

Here's what is required:

(ox,oy,oz) origin
(rx,ry,rz) rotation angles
sx,sy,sz,cx,cy,cz sines and cosines of rx,ry and rz

a,b,c - co-efficient of the three terms of the quadratic equation of the cylinder/sphere

k1,k2 - for cylinder
k3 - inner sphere
k4 - outer sphere

to solve a quadratic equaltion ax^2+bx+c=0
x = (-b +/- sqrt( sqr ( b )- 4 ac ) / 2a is used.

there are a lot of temperory vars used.


Init:
pi=acos(-1)

Frame:
t=t+.001+ti*.1;ti=ti*.9;
ox=sin(t)*.1;
oy=sin(t)*cos(t)*.15;
oz=cos(t)*.1;
rx=atan2(ox,sqr(oy)+sqr(oz));
ry=-atan2(ox,oz);
rz=rx*ry*ox*oy*oz+sin(t*.3)*pi;
sx=sin(rx);sy=sin(ry);sz=sin(rz);
cx=cos(rx);cy=cos(ry);cz=cos(rz);
af=max(h,w)/min(h,w);

Beat:
ti=(abs(getosc(0,0,0)*400)%10)*.2

Point:
y=if(above(h,w),y*af,y);
x=if(above(w,h),x*af,x);

¤ Rotate ray;
dx1=x*cz-y*sz;
dy1=x*sz+y*cz;
dy2=dy1*cx-sx;
dz2=dy1*sx+cx;
dx3=dx1*cy-dz2*sy;
dz2=dx1*sy+dz2*cy;

¤temp variables, save time;
dx1=sqr(dx3);dy1=sqr(dy2);dz1=sqr(dz2);

¤calculate cylinfer 1, equation sqr(x)+sqr(y)=0.7*0.7;
a=dx1+dy1;
b=2*(dx3*ox+dy2*oy);
c=sqr(ox)+sqr(oy)+.49;
D=sqr(b)-4*a*c;
k1=(-b+sqrt(d))/(2*a);


¤calculate cylinfer 2, equation sqr(x)+sqr(z)=0.7*0.7;
a=dx1+dz1;
b=2*(dx3*ox+dz2*oy);
c=sqr(ox)+sqr(oz)+.49;
D=sqr(b)-4*a*c;
k2=(-b+sqrt(d))/(2*a);

¤calculate inner sphere, equation: sqr(x)+sqr(y)+sqr(z)=sqr(0.9);

a=dx1+dy1+dz1;
b=2*(dx3*ox+dy2*oy+dz2*oz);
c=sqr(oz)+sqr(oy)+sqr(oz)-.9;
D=sqr(b)-4*a*c;
k3=(-b+sqrt(d))/(2*a);

¤calculate outer sphere, equation: sqr(x)+sqr(y)+sqr(z)=sqr(2);
a=dx1+dy1+dz1;
b=2*(dx3*ox+dy2*oy+dz2*oz);
c=sqr(oz)+sqr(oy)+sqr(oz)-4;
D=sqr(b)-4*a*c;
k4=(-b+sqrt(d))/(2*a);

k1=if(below(k1,0),1000,k1);
k2=if(below(k2,0),1000,k2);
k3=if(below(k3,0),1000,k3);
k4=if(below(k4,0),1000,k4);

¤ order - inner sphere - cylinders - outer sphere;
¤ k1, k2 - cylinders, k3 - inner sphere, k4 - outer sphere;
k=min(max(max(k1,k2),k3),k4);

ix=k*dx3+ox;
iy=if(equal(k,k1)+equal(k,k3)+equal(k,k4),dy2*k+oy,dz2*k+oz);
iz=if(equal(k,k1)+equal(k,k3)+equal(k,k4),dz2*k+oz,dy2*k+oy);

rad=if(equal(k,k3),.9,2);
r=if(equal(k,k3),1,.5);

¤problem zone;
x=if(equal(k,k3)+equal(k,k4),atan2(iz,ix)/pi*1.4,asin(sin(atan2(ix,iy)))*.954);
y=if(equal(k,k3)+equal(k,k4),asin(iy/rad)*4/pi,iz);
¤end of problem zone;

¤ cool wrap by UnConeD;
x=asin(sin(x*pi*.5))*2/pi;
y=asin(sin(y*pi*.5))*2/pi;

---------------------------------
rectangular coords on, blend off, wrap OFF


Ohh...and use any kind of a picture as a texture, as I havent made any.

5th July 2003 02:25 UTC

for alpha blending it's fairly simple... I'm not going to actually do this for you, but say you want a sphere to black out just before it hits the edge of a cylinder that's on the z axis... sqrt(ix*ix+iy*iy) = distance from the z axis, so if k=ksphere then alpha = dist_z_axis * 5 - cylinderradius. This should create a nice fade to darkness around the cylinder, if it doesn't fade out all the way when it gets to the ugly edge then use slightly more than the cylinder radius...

Then after you do that, to fade the cylinder, use the distance from the origin instead of the distance from the z axis and use sphereradius instead of cylinderradius, and check that k=kcylinder instead of ksphere...

I hope I've been clear enough and that I haven't said anything stupid...

And then is there any way to enter the cylinders?
Try changing ox, oy, and oz :)
That maze that UnConeD did might be helpful to look at also...

edit: blargh, I think I did do something stupid... it should be (dist_z_axis - cylinderradius)*5

5th July 2003 05:56 UTC

but, I think there is some problem with the raytracing, as when I bring the camera ver close to the walls, the walls seem to be squashed
:igor:


5th July 2003 15:12 UTC

sheryas: could also be a consequence of not having perspective correction. Don't forget the whole raytracing thing is just a big ugly hack in AVS...


7th July 2003 12:10 UTC

well, I didnt get you...


8th July 2003 23:01 UTC

Okay try this. Imagine a wall with alternating white/black panels of same size. If you look at it from the front, all the panels have equal width.
Now if you stand up to the wall and look along its length, the panels in the distance will of course look less wide than the ones up front. This is regular 3D.
Now suppose that the panels were drawn using a texture instead. The projection of the wall is a trapezoid shape (if you're looking straight ahead), but you can't simply take the alternating white/black bands and squish them into a trapezoid-shape: you have to distort it so the panels in the distance look less wide than the ones in the front (check attachment... on the left is stretched, on the right is perspective corrected).

This is what perspective correction does. Because DM's are simple 2D movements, there is no depth information and no perspective correction.

Older 3D cards (and for example playstation 1 consoles) didn't have perspective correction either as a speedup. It's not noticable except for stuff that's up close and/or almost tangent to the viewer (floors and ceilings). And most software engines simply perform perspective correction every 8 pixels or so, and do linear interpolation in between (e.g. Quake1 did this).