xxxxxxxxxx
148
let rotateangle = 0
let Sunx, Suny, Sunz
let Earthx, Earthy, Earthz, earthSize, earthDist
let Moonx, Moony, Moonz, moonSize, moonDist
let Marsx, Marsy, Marsz, marsSize, marsDist
let Jupiterx, Jupitery, Jupiterz, jupiterSize, jupiterDist
let MarsPos = []
let sunSizeOriginal
let scroll = 15
let xrotation = 0
let yrotation = 0
let xtranslation = 0
let ytranslation = 0
function setup() {
createCanvas(windowWidth,windowHeight, WEBGL);
frameRate(60)
sunSizeOriginal = width/10
resetrotationbtn = createButton('Reset Rotation')
resetrotationbtn.mousePressed(rotationReset)
backgroundimage = loadImage("Image.jpg")
}
function rotationReset() {
xrotation = 0
yrotation = 0
}
function drawPlanet(Px, Py, Pz, r, g, b, size) {
push()
noStroke()
strokeWeight(1)
fill(r,g,b)
translate(Px, Py, Pz)
sphere(size)
pop()
}
function mouseWheel(event) {
if (event.delta > 0) {
scroll += 1
} else if (scroll - 1 !== 0) {
scroll -= 1
}
return false
}
function draw() {
background(0)
tranRotate()
translate(xtranslation, ytranslation)
rotateY(yrotation)
rotateX(xrotation)
drawSun()
drawEarth()
drawMoon()
drawMars()
rotateangle += 0.01
if (rotateangle == 360) {
rotateangle = 0
}
}
function tranRotate() {
if (mouseIsPressed && mouseButton === LEFT && (0 < mouseX) && (mouseX < width) && (0 < mouseY) && (mouseY < height)) {
xtranslation = mouseX - width / 2
ytranslation = mouseY - height / 2
} else if (mouseIsPressed && mouseButton === CENTER && (0 < mouseX) && (mouseX < width) && (0 < mouseY) && (mouseY < height)) {
yrotation = (mouseX - width / 2) / 100
xrotation = (mouseY - height / 2) / 100
}
}
function drawSun() {
sunSize = sunSizeOriginal / scroll
Sunx = 0
Suny = 0
Sunz = 0
drawPlanet(Sunx, Suny, Sunz, 255, 255, 0, sunSize)
}
function drawEarth() {
earthSize = sunSize * 0.25
earthDist = sunSize * 21
Earthx = Sunx + map(sin(rotateangle), -1, 1, -earthDist, earthDist)
Earthy = Suny + map(cos(rotateangle), -1, 1, -earthDist, earthDist)
Earthz = Sunz
drawPlanet(Earthx, Earthy, Earthz, 0, 200, 0, earthSize)
stroke(51)
strokeWeight(1)
line(Earthx, Earthy, Earthz,Sunx,Suny,Sunz)
}
function drawMoon() {
moonSize = earthSize * 0.273
moonDist = earthDist * 0.1
Moonx = Earthx + map(cos(rotateangle * 13.5), -1, 1, -moonDist, moonDist)
Moony = Earthy + map(sin(rotateangle * 13.5), -1, 1, -moonDist, moonDist)
Moonz = Earthz + map(sin(rotateangle * 13.5), -1, 1, -moonDist / 2, moonDist / 2)
drawPlanet(Moonx, Moony, Moonz, 200, 200, 200, moonSize)
stroke(51)
strokeWeight(1)
line(Moonx,Moony,Moonz,Earthx,Earthy,Earthz)
}
function drawMars() {
marsSize = earthSize
marsDist = 1.75 * earthDist
Marsx = Sunx + map(cos(rotateangle / 2), -1, 1, -marsDist, marsDist)
Marsy = Suny + map(sin(rotateangle / 2), -1, 1, -marsDist, marsDist)
Marsz = Sunz
drawPlanet(Marsx, Marsy, Marsz, 200, 0, 0, marsSize)
stroke(51)
strokeWeight(1)
line(Marsx,Marsy,Marsz,Sunx,Suny,Sunz)
//noFill()
//beginShape()
//for (x = -marsDist; x < marsDist; x++) {
// for (y = -marsDist; y < marsDist; y++) {
// vertex(x,y,Marsz)
// }
//}
//endShape()
}