I'm trying to write a simplest raytracer that will just output a circle to a PPM file yet i'm getting nothing.Problem is not with the PPM code and is with the ray code.I'm bad at math so i really don't know where the problem is and would appreciate if somebody could help me as i really have no idea what i'm doing wrong.
Here is the code:
public class Main {
static int WITDH = 200;
static int HEIGHT = 200;
public static void main(String[] args) {
Sphere Ball = new Sphere(0, 0, -1 ,1);
Vector Camera = new Vector(0, 0, -1);
int [][] Pic = new int[200][200];
int number=0;
for (int y = 0;y<HEIGHT;y++) {
for (int x = 0; x < WITDH; x++) {
number++;
Ray test = new Ray(Camera);
double first=0.5*(WITDH-x)/(double)WITDH;
double second=0.5*(HEIGHT-y)/(double)HEIGHT;
Vector direc=new Vector(first,second,-1.0);
test.at(number,direc);
if (Sphere.intersects(test, Ball) == true) {
Pic[y][x]=0;
} else if(Sphere.intersects(test,Ball) == false) {
Pic[y][x]=1;
}
}
}
}
}
static Boolean intersects(Ray ray, Sphere Shape) {
Vector Origin = ray.origin;
Vector center = Shape.Pozicija;
double discrimant;
Vector L = Vector.SubtractVector(Origin, center);
double a = Vector.ReturnDotProduct(ray.Direction, ray.Direction);
double b = 2.0 * Vector.ReturnDotProduct(L, ray.Direction);
double c = Vector.ReturnDotProduct(L, L) - (Shape.radius * Shape.radius);
double t0=0.0;
double t1=0.0;
discrimant = (b * b) - (4.0 * a * c);
double q;
if(discrimant<0.0){
return false;
}else if(discrimant==0.0){
t0=t1=-0.5*b/a;
}else {
if(b>0) {
q = -0.5*(b+Math.sqrt(discrimant));
}else{
q = -0.5*(b-Math.sqrt(discrimant));
}
t0=q/a;
t1=c/q;
}
if(t0>t1){
double t3;
t3=t1;
t0=t1;
t1=t3;
}
if (t0 < 0) {
t0 = t1;
if (t0 < 0) {
return false;
}
}
System.out.println(t0+t1);
return true;
}
Vector at(double t,Vector d2){
Vector d1=new Vector(0,0,0);
if(t==0){
d1=Direction;
}else if(t>0){
d1=Vector.GetDirection(d2);
}
return Vector.AddVector(origin,Vector.MultiplyScalar(d1,t));
}
Your code appears to be an attempt at implementing a simple ray tracer, but there are some issues that need to be addressed for it to work correctly. I'll help you identify and fix these issues:
Ray Class: In the Ray class, you have a method at(double t, Vector d2) that attempts to compute a new vector at a given t value. However, it appears that you are trying to mutate the Direction field of the Ray class, which is incorrect. Instead, you should create a new Ray instance with the updated origin and direction. Here's a corrected at method:
Ray at(double t, Vector d2) { Vector newDirection = Vector.GetDirection(d2); Vector newOrigin = Vector.AddVector(origin, Vector.MultiplyScalar(newDirection, t)); return new Ray(newOrigin, newDirection); }
Intersect Method: The intersects method seems to be a static method in your code, but it should be defined within the Sphere class, as it operates on Sphere objects. Move the intersects method into the Sphere class.
if (discriminant < 0.0) { return false; } else if (discriminant == 0.0) { t0 = t1 = -0.5 * b / a; } else { if (b > 0) { q = -0.5 * (b + Math.sqrt(discriminant)); } else { q = -0.5 * (b - Math.sqrt(discriminant)); } t0 = q / a; t1 = c / q; }
if (t0 < 0 && t1 < 0) { return false; }
return true;
Looping Issue: In your main method, the variable number is used as an incrementing factor, but it is not required. You should increment number within the loop, not beforehand.
PPM File Generation: You mentioned that the PPM code is working, but it's not included in the provided code. Ensure that the code to write the PPM file is functioning correctly, and make sure you save the file with the correct extension (.ppm).
With these corrections and after making sure the rest of your code (including the Vector and Sphere classes) is implemented correctly, your ray tracer should be able to render a circle to a PPM file. Additionally, you may want to consider using a proper development environment or IDE, as it can assist you in debugging and writing cleaner code.