Compare commits
	
		
			2 commits
		
	
	
		
			master
			...
			kapri-ball
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
							 | 
						099f3e672c | ||
| 
							 | 
						82c1339388 | 
					 3 changed files with 43 additions and 33 deletions
				
			
		
							
								
								
									
										6
									
								
								.idea/vcs.xml
									
										
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								.idea/vcs.xml
									
										
									
										generated
									
									
									
										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;
 | 
			
		||||
 | 
			
		||||
import xyz.marsavic.geometry.Vector;
 | 
			
		||||
import xyz.marsavic.gfxlab.Vec3;
 | 
			
		||||
import xyz.marsavic.gfxlab.graphics3d.Hit;
 | 
			
		||||
import xyz.marsavic.gfxlab.graphics3d.Ray;
 | 
			
		||||
import xyz.marsavic.gfxlab.graphics3d.Solid;
 | 
			
		||||
import xyz.marsavic.utils.Numeric;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class Ball implements Solid {
 | 
			
		||||
| 
						 | 
				
			
			@ -17,7 +15,6 @@ public class Ball implements Solid {
 | 
			
		|||
	private final double rSqr;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
	private Ball(Vec3 c, double r) {
 | 
			
		||||
		this.c = c;
 | 
			
		||||
		this.r = r;
 | 
			
		||||
| 
						 | 
				
			
			@ -43,16 +40,23 @@ public class Ball implements Solid {
 | 
			
		|||
 | 
			
		||||
	@Override
 | 
			
		||||
	public HitBall firstHit(Ray ray, double afterTime) {
 | 
			
		||||
		Vec3 e = c().sub(ray.p());                                // Vector from the ray origin to the ball center
 | 
			
		||||
		
 | 
			
		||||
		double dSqr = ray.d().lengthSquared();
 | 
			
		||||
		double l = e.dot(ray.d()) / dSqr;
 | 
			
		||||
		double mSqr = l * l - (e.lengthSquared() - rSqr) / dSqr;
 | 
			
		||||
		
 | 
			
		||||
		if (mSqr > 0) {
 | 
			
		||||
			double m = Math.sqrt(mSqr);
 | 
			
		||||
			if (l - m > afterTime) return new HitBall(ray, l - m);
 | 
			
		||||
			if (l + m > afterTime) return new HitBall(ray, l + m);
 | 
			
		||||
		// 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,
 | 
			
		||||
		// iz diskriminante mozemo dobiti i njihove udaljenosti.
 | 
			
		||||
		// 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)
 | 
			
		||||
		// (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;
 | 
			
		||||
		//     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 D = B*B - 4*A*C;
 | 
			
		||||
		if (D > 0) {
 | 
			
		||||
			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;
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -149,7 +149,7 @@ public class UtilsGL {
 | 
			
		|||
		} catch (Exception e) {
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
		}
 | 
			
		||||
		parallelism = p;
 | 
			
		||||
		parallelism = 3;
 | 
			
		||||
		pool = new ForkJoinPool(parallelism);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue