Compare commits
2 commits
master
...
kapri-ball
Author | SHA1 | Date | |
---|---|---|---|
![]() |
099f3e672c | ||
![]() |
82c1339388 |
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -1,11 +1,9 @@
|
||||||
package xyz.marsavic.gfxlab.graphics3d.solids;
|
package xyz.marsavic.gfxlab.graphics3d.solids;
|
||||||
|
|
||||||
import xyz.marsavic.geometry.Vector;
|
|
||||||
import xyz.marsavic.gfxlab.Vec3;
|
import xyz.marsavic.gfxlab.Vec3;
|
||||||
import xyz.marsavic.gfxlab.graphics3d.Hit;
|
import xyz.marsavic.gfxlab.graphics3d.Hit;
|
||||||
import xyz.marsavic.gfxlab.graphics3d.Ray;
|
import xyz.marsavic.gfxlab.graphics3d.Ray;
|
||||||
import xyz.marsavic.gfxlab.graphics3d.Solid;
|
import xyz.marsavic.gfxlab.graphics3d.Solid;
|
||||||
import xyz.marsavic.utils.Numeric;
|
|
||||||
|
|
||||||
|
|
||||||
public class Ball implements Solid {
|
public class Ball implements Solid {
|
||||||
|
@ -17,7 +15,6 @@ public class Ball implements Solid {
|
||||||
private final double rSqr;
|
private final double rSqr;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private Ball(Vec3 c, double r) {
|
private Ball(Vec3 c, double r) {
|
||||||
this.c = c;
|
this.c = c;
|
||||||
this.r = r;
|
this.r = r;
|
||||||
|
@ -43,16 +40,23 @@ public class Ball implements Solid {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HitBall firstHit(Ray ray, double afterTime) {
|
public HitBall firstHit(Ray ray, double afterTime) {
|
||||||
Vec3 e = c().sub(ray.p()); // Vector from the ray origin to the ball center
|
// U nasoj jednacini At^2 + Bt + C = 0, gde je t vreme kada zrak pogadja dve tacke sfere
|
||||||
|
// diskriminanta ce nam dati da li trazeno t postoji i da li postoje 1 ili 2, ako postoje 2,
|
||||||
double dSqr = ray.d().lengthSquared();
|
// iz diskriminante mozemo dobiti i njihove udaljenosti.
|
||||||
double l = e.dot(ray.d()) / dSqr;
|
// Za sferu (x-xc)^2 + (y-yc)^2 + (z-zc)^2 = r^2 i pravu (x,y,z) = (x0,y0,z0) + t(a,b,c)
|
||||||
double mSqr = l * l - (e.lengthSquared() - rSqr) / dSqr;
|
// (a,b,c) je ray.d(), a x0,y0,z0 je ray.p(), dok je (xc,yc,zc) centar sfere i r poluprecnik.
|
||||||
|
double A = ray.d().lengthSquared(), B = 2*ray.d().dot(ray.p().sub(c)), C = ray.p().sub(c).lengthSquared() - rSqr;
|
||||||
if (mSqr > 0) {
|
// A = a^2+b^2+c^2, B = 2(a(x0-xc)+b(y0-yc)+c(z0-zc)), C = (x0-xc)^2+(y0-yc)^2+(z0-zc)^2-r^2
|
||||||
double m = Math.sqrt(mSqr);
|
double D = B*B - 4*A*C;
|
||||||
if (l - m > afterTime) return new HitBall(ray, l - m);
|
if (D > 0) {
|
||||||
if (l + m > afterTime) return new HitBall(ray, l + m);
|
double rootD = Math.sqrt(D);
|
||||||
|
// t = (-B+-sqrt(D))/2A
|
||||||
|
double tSmall = (-B-rootD)/(2*A);
|
||||||
|
if (afterTime < tSmall)
|
||||||
|
return new HitBall(ray, tSmall);
|
||||||
|
double tBig = (-B+rootD)/(2*A);
|
||||||
|
if (afterTime < tBig)
|
||||||
|
return new HitBall(ray, tBig);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,7 +149,7 @@ public class UtilsGL {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
parallelism = p;
|
parallelism = 3;
|
||||||
pool = new ForkJoinPool(parallelism);
|
pool = new ForkJoinPool(parallelism);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue