#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# I may have found a bug

edited May 2017 in Bugs Posts: 69

First of all. I am using the latest Beta. I used this code here to generate a sphere(for a planet), and always scale the surface 100 meters away. With my distance and radius parameter, i control the distance and parameter. This all works when I am decreasing the distance or increasing the radius, but as if i come closer and go further again it stays the same. I am sure I did everything correctly. Here is the code:

function setup()
s = sphere(600000)
parameter.number("d",601000,800000,800000)
parameter.number("r",500000,600000,500000)

end

function draw()
scl = (d-r)/100
perspective(90)
camera(0,d/scl,0,0,0,0,0,0,1)
scale(r/scl)
s:draw()

end

s = mesh()
vertices = {}
for x = -180, 175, 5 do
for y = -90, 85, 5 do
end
end
s.vertices = vertices
s:setColors(color(0,0,255,255))
return s
end

function rotate3D(vec,angle,x,y,z)
x = x or 0
y = y or 0
z = z or 1

if x == 1 then
return vec3(vec.x,vec.y*c(a)-vec.z*s(a),vec.y*s(a)+vec.z*c(a))
elseif y == 1 then
return vec3(vec.z*c(a)-vec.x*s(a),vec.y,vec.z*c(a)-vec.x*s(a))
elseif z == 1 then
return vec3(vec.x*c(a)-vec.y*s(a),vec.x*s(a)+vec.y*c(a),vec.z)
end
return vec
end

function sphereRotate(x,y)
return rotate3D(rotate3D(vec3(0,1,0),y,1,0,0),x,0,0,1)
end
Tagged:

• edited May 2017 Posts: 71

• edited May 2017 Posts: 69
@Attila717 Since I didnt use the Craft extension for this example i didnt post it there, also I did put 3 "~" at the beginning and at the end. I dont know what Iam doing wrong
Also I cant even change it for some reason. Wether or not I have the 3 ~ there it always highlights that part.
• Posts: 71

@GR00G0 I don't understand why you are trying to use meshes to create your planets, as Codea Craft would be much simpler. Is there a limitation of Codea Craft that makes it not worth using in this instance?

• Mod
edited May 2017 Posts: 8,740

@GR00G0 The 3~'s have to be on a line by themselves. You had them as ~~~function setup and end~~~ .

• Posts: 69
@Attila717 I want to use a similar approach like KSP. There the ground gets generated up until about 40 km. And after that a 1:6000 scale module of Kerbin gets used. Meaning that the planet is about 6 km away. If I have a rocket wich is only 10 meters lomg it will point inside the sphere. Also I feel really unconfortable of how codea craft works.
• Posts: 71

@GR00G0 Example of how easy this is to do with Codea Craft:

-- Use this function to perform your initial setup
function setup()
sphere(5)
parameter.number("distance",10,20,10)
end

function draw()
end

function update()
craft.scene.camera.position = vec3(0,0,distance)
craft.scene.camera.rotation = quat.eulerAngles(0,0,180)

s.rotation = quat.eulerAngles(45 * ElapsedTime, 45 * ElapsedTime, 45 * ElapsedTime)
end

if s then
s:destroy()
end
s = craft.entity()
--sphereMesh can be this or an obj file like this --> craft.mesh(PUT FILE HERE)
sphereRender.material = craft.material("Materials:Standard")
return s
end

• Posts: 69
@dave1707 Ah ok. Thank you. I am sorry again everyone I am new here.
• Posts: 69

@Attila717 Its just with the lack of documentation, things are way simpler for me using normal meshes.

• Mod
Posts: 8,740

@GR00G0 You forgot background(0) at the start of the draw() function. That's the reason your circle gets bigger but not smaller. You're not clearing the previous drawings.

Here's a sphere program I have.

supportedOrientations(LANDSCAPE_ANY)

function setup()
parameter.number("view",1,10,3)
parameter.integer("level",6,200,12)
parameter.action("new sphere",setup1)
parameter.number("AngleX",0,360,90)
parameter.number("AngleY",0,360,140)
parameter.number("AngleZ",0,360,90)
setup1()
end

function setup1()
tab={}  -- table of points
M=level
N=level
for n=0,N do
tab[n]={}
for m=0,M do
-- calculate the x,y,z point position
x=r * math.sin(math.pi * m/M) * math.cos(2*math.pi * n/N)
y=r * math.sin(math.pi * m/M) * math.sin(2*math.pi * n/N)
z=r * math.cos(math.pi * m/M)
-- 2 dimension table of sphere points
tab[n][m]=vec3(x,y,z)
end
end
sph={}
for n=0,N-1 do
for m=0,M-1 do
-- loop thru sphere table to create a rectangle
-- create 2 triangles from 4 points of a rectangle
-- create 1st triangle of the rectangle
table.insert(sph,tab[n][m])
table.insert(sph,tab[n][m+1])
table.insert(sph,tab[n+1][m+1])
-- create 2nd triangle of a rectangle
table.insert(sph,tab[n][m])
table.insert(sph,tab[n+1][m])
table.insert(sph,tab[n+1][m+1])
end
end
-- create table of random colors for each triangle
cols={}
for z=1,#sph,6 do
col=vec4(math.random(),math.random(),math.random(),1)
for q=1,6 do
table.insert(cols,col)
end
end
-- create mesh
sphere=mesh()
sphere.vertices=sph
sphere.colors=cols
end

function draw()
background(40, 40, 50)
perspective(view,WIDTH/HEIGHT)
camera(2000,0,0,0,0,0,0,1,0)
rotate(AngleX,1,0,0)
rotate(AngleY,0,1,0)
rotate(AngleZ,0,0,1)
sphere:draw()
end

• Posts: 69
Thank you @dave1707 ! Also do you know how I can draw a 3d mesh over the sphere?
• Mod
edited May 2017 Posts: 8,740

@GR00G0 How much do you know about putting an image onto a mesh. I don't want to be to basic if you know some stuff already, or I don't want to get to involved if you don't know too much.

EDIT: Instead of my explaining it, here's a link that will give you a better explanation.

https://codea.io/talk/discussion/1244/mesh-tutorial

• edited May 2017 Posts: 69

@dave1707 i know how to map textures onto a sphere, but what I want to know is if I have a sphere, with the the surface about 1 meter away, and another mesh wich is bigger than 1 m, how I can draw the bigger mesh in front of the other mesh. i need this so I can scale down the planet by alot.

• Posts: 71

@GR00G0 So what I'm hearing is that you want the sphere that makes up the solid body of the planet behind some other mesh (with an image texture) that encases the aforementioned sphere?

• edited May 2017 Posts: 69

@Attila717 This is just something thats gonna happen when I scale the planet down.
Scalimg down the radius also means scaling down the distance. the actual mesh could just be a few meters away. and having a rocket in front of you would collide it with the planet(or it would just be inside it).