# Moving a sphere in a basic ray tracer

I have written a really basic ray tracer. It generates primary rays and sets the colour of the pixel to the colour of the object that I have set myself. The code for which can be seen below.

from PIL import Image, ImageDraw
from Sphere import Sphere
import numpy as np

imgx = 300
imgy = 300

img = Image.new('RGB', (imgx, imgy), color = 'black')

#Place a Sphere in centre, distance 10 away.
#Sphere has a constructor (centre, colour, radius)
sphere1 = Sphere(np.array([0,0,-1]), (255, 255, 255), 100)
#sphere2 = Sphere(np.array([300,0,-1]), (255, 0, 0), 100)

objects = [sphere1]

for i in range(img.size):
for j in range(img.size):

u = -imgx/2 + (imgx)*(i+0.5)/imgx
v = -imgy/2 + (imgy)*(j+0.5)/imgy

rayDirection = -1*np.array([0,0,1]) + u*np.array([1,0,0]) + v*np.array([0,1,0])
rayOrigin = np.array([0,0,0])
#p(t) = rayorigin + t*raydirect

#Sphere intersection
for obj in objects:
a = np.dot(rayDirection, rayDirection)
b = np.dot(2*rayDirection, (rayOrigin - obj.centre))
c = np.dot((rayOrigin - obj.centre), (rayOrigin - obj.centre)) - obj.radius**2

eminc = rayOrigin - obj.centre

discrim = (np.dot(rayDirection, eminc))**2 - ((np.dot(rayDirection, rayDirection))*(np.dot(eminc, eminc))-obj.radius**2)
if(discrim >= 0):
print("intersect")
pixelMap[i,j] = obj.colour
else:
pixelMap[i,j] = (0, 0,0)

img.save('output.png') It works in the sense that when I run the code, I get an image of a white circle. My issue arises when I change the centre of the sphere. Instead of shifting to the left, right, up or down. I get a skewed image. Seen below when I set the Sphere to have centre np.array([3,0,-1]). I believe the problem may be to do with the translation between world co-ordinates and camera co-ordinates? However, I am not sure what transformation matrix to use or how to use it. For what it's worth, my code takes ideas from Fundamentals of Computer Graphics by Pete Shirley and Steve Marschner.

• discrim = (np.dot(rayDirection, eminc))**2 - ((np.dot(rayDirection, rayDirection))*(np.dot(eminc, eminc))-obj.radius**2), use discrim = b * b-4*a*c – lightxbulb Jul 15 '19 at 13:34
• @lightxbulb discrim is equivalent to b^2-4ac with the values for a,b and c substituted in. – Tim Jul 15 '19 at 16:26
• It is not. It would be if you actually wrote (np.dot(rayDirection, eminc))**2 - 4*((np.dot(rayDirection, rayDirection))*(np.dot(eminc, eminc)-obj.radius**2)). So just put in b * b - 4 * a * c, and check whether it works. – lightxbulb Jul 15 '19 at 18:37
{"ft_lang_label":"__label__en","ft_lang_prob":0.75969696,"math_prob":0.98211676,"size":2344,"snap":"2020-10-2020-16","text_gpt3_token_len":731,"char_repetition_ratio":0.14871795,"word_repetition_ratio":0.040816326,"special_character_ratio":0.3370307,"punctuation_ratio":0.20604914,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9915403,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-24T16:46:37Z\",\"WARC-Record-ID\":\"<urn:uuid:6cde0ac0-2746-4a00-bd3e-9bbb5dfabe65>\",\"Content-Length\":\"134627\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f94f85e6-89ae-4752-972f-d7c57c49fe6a>\",\"WARC-Concurrent-To\":\"<urn:uuid:14de92d5-8abd-41c5-b318-39781e24ebdb>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://computergraphics.stackexchange.com/questions/9002/moving-a-sphere-in-a-basic-ray-tracer\",\"WARC-Payload-Digest\":\"sha1:HCMPYIN57JO4TJE6MD3PKB2ATALHXBQS\",\"WARC-Block-Digest\":\"sha1:FGVGD35GJN6Y7J4OHAXDKSWB7LA5NJZV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875145966.48_warc_CC-MAIN-20200224163216-20200224193216-00019.warc.gz\"}"}