Just thought I'd post this, as it's a really simple little algorithm I put together for drawing a sphere. More coherent and easy to follow than sk89's code, I hope yall find it helpful.
public void sphere(int i, int j, int k, int r){ //i, j, and k are the coordinates of the center of the sphere, r is the radius
Vec3D center = Vec3D.createVector(i, j, k); //this just creates a set of where the points are
Vec3D point = null; //declares the point variable we will use later
for(int x = -r; x < r; x++){
for(int y = -r; y < r; y++){
for(int z = -r; z < r; z++){
point = Vec3D.createVector(i+x, j+y, k+z); //defines the variable we declared earlier
if(center.distanceTo(point) > r) //if the point is further away from the center than the radius, ignore it
continue;
int a = MathHelper.floor_double(point.xCoord);
int b = MathHelper.floor_double(point.yCoord);
int c = MathHelper.floor_double(point.zCoord);
if(worldObj == null)worldObj = ModLoader.getMinecraftInstance().theWorld;
worldObj.setBlockWithNotify(a, b, c, 0); //creates the blocks
}
}
}
}
So I was working earlier this evening, and I found an even easier way, that doesn't even require a call to Vector3D. Here's the code
int i = MathHelper.floor_double(player.posX);
int j = MathHelper.floor_double(player.posY);
int k = MathHelper.floor_double(player.posZ);
int r = 100;
for(int x = -r; x < r; x++){
for(int y = -r; y < r; y++){
for(int z = -r; z < r; z++){
double dist = MathHelper.sqrt_double((x*x + y*y + z*z)); //Calculates the distance
if(dist > r)
continue;
if(worldObj == null)worldObj = ModLoader.getMinecraftInstance().theWorld;
worldObj.setBlockWithNotify(i+x, j+y, k+z, 0);
}
}
}
I was going to be faced with the problem of generating random circles, and I would have gotten it working relatively easily with understandable code, but it would have taken at least three times as much as this.
Okay note to self: LEARN TO USE VEC3D I was going to be faced with the problem of generating random circles, and I would have gotten it working relatively easily with understandable code, but it would have taken at least three times as much as this.
Glad I could you guys! Here's an even simpler method I discovered earlier this evening.
int i = MathHelper.floor_double(player.posX);
int j = MathHelper.floor_double(player.posY);
int k = MathHelper.floor_double(player.posZ);
int r = 100;
for(int x = -r; x < r; x++){
for(int y = -r; y < r; y++){
for(int z = -r; z < r; z++){
double dist = MathHelper.sqrt_double((x*x + y*y + z*z)); //Calculates the distance
if(dist > r)
continue;
if(worldObj == null)worldObj = ModLoader.getMinecraftInstance().theWorld;
worldObj.setBlockWithNotify(i+x, j+y, k+z, 0);
}
}
}
Glad I could you guys! Here's an even simpler method I discovered earlier this evening.
int i = MathHelper.floor_double(player.posX);
int j = MathHelper.floor_double(player.posY);
int k = MathHelper.floor_double(player.posZ);
int r = 100;
for(int x = -r; x < r; x++){
for(int y = -r; y < r; y++){
for(int z = -r; z < r; z++){
double dist = MathHelper.sqrt_double((x*x + y*y + z*z)); //Calculates the distance
if(dist > r)
continue;
if(worldObj == null)worldObj = ModLoader.getMinecraftInstance().theWorld;
worldObj.setBlockWithNotify(i+x, j+y, k+z, 0);
}
}
}
Im trying to find a way in which you find the coordinates of everything in the sphere from the center out to the furthest point. Your code won't be working for my case. Do you know anyway in wich you can create an array containing all blocks within a sphere in order from the center to the outside? I'm using this to calculate explosions.
Im trying to find a way in which you find the coordinates of everything in the sphere from the center out to the furthest point. Your code won't be working for my case. Do you know anyway in wich you can create an array containing all blocks within a sphere in order from the center to the outside? I'm using this to calculate explosions.
Actually, yah, you'd just vary the radius, here's the simplest terms:
for(int r = 0; r < 100; r++){
for(int x = -r; x < r; x++){
for(int y = -r; y < r; y++){
for(int z = -r; z < r; z++){
double dist = MathHelper.sqrt_double((x*x + y*y + z*z)); //Calculates the distance
if(dist > r)
continue;
if(worldObj == null)worldObj = ModLoader.getMinecraftInstance().theWorld;
worldObj.setBlockWithNotify(i+x, j+y, k+z, 0);
}
}
}
}
}
r increases per iteration, thereby getting all block starting in the center. All you have to do is add them into a list.
So I was working earlier this evening, and I found an even easier way, that doesn't even require a call to Vector3D. Here's the code
LEARN TO USE VEC3D
I was going to be faced with the problem of generating random circles, and I would have gotten it working relatively easily with understandable code, but it would have taken at least three times as much as this.
Yes. Vec3D is a very useful tool.
Im trying to find a way in which you find the coordinates of everything in the sphere from the center out to the furthest point. Your code won't be working for my case. Do you know anyway in wich you can create an array containing all blocks within a sphere in order from the center to the outside? I'm using this to calculate explosions.
Actually, yah, you'd just vary the radius, here's the simplest terms:
r increases per iteration, thereby getting all block starting in the center. All you have to do is add them into a list.
It can be used for pretty much anything that needs circles or spheres. That includes WorldGen