Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

How to modify this 3D pyramid

 
Ranch Hand
Posts: 41
Oracle Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hii,
I hope everyone's doing well
I have a Java project. First I had to make a tetrahedron ( see the code below). Now I have to modify the code a little so that it shows a square pyramid but I can't seem to be able to solve it.
Any kind of help would be reallyyy appreciated:))))






Thank you a lot in advance  
 
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, a tetrahedron is composed of four triangles. Whereas a square pyramid is composed of a square plus four triangles.

So presumably you need to write a SquarePyramid class which is like Tetrahedron except taking into account that difference. I'm unfamiliar with most of the classes you're using there so I don't know if it would be suitable for SquarePyramid to extend IndexedTriangleArray or not, since one of its components is a square. Maybe there's some other more suitable class.
 
Saloon Keeper
Posts: 3893
154
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A separate class was my first thought as well. But since a square is made up of two triangles, it is maybe easier to split the square into two triangles. The rest can then be the same, including the indices array. Just have a look whether the normals need to be modified in some way, since in the ouput those two bottom triangles must look like a square.
 
Anduena Smith
Ranch Hand
Posts: 41
Oracle Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your help. I understand what should be done and I've been trying for 2 hours but somehow I can't get it to work... :/
 
Paul Clapham
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're not averse to looking at people's non-working code here. Descriptions of why it's not working are welcomed as well!
 
Anduena Smith
Ranch Hand
Posts: 41
Oracle Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.. I'm sorry for taking your time
 
Piet Souris
Saloon Keeper
Posts: 3893
154
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul (and me too) was just curious to know what exactly did not work.

My idea was to turn the square pyramid into 5 points with 6 triangles. For instance: if the base is A, B, C, D and the top T, then we have the triangles ABC, BCD, ABT, BCT, et cetera, with indices {0, 1, 2, 1, 2, 3, 0, 1, 4, 1, 2, 4, ...}. Now, the two base triangles must appear as just one square, so must have the same material, and the same normals. I don't have the classes you are using, so can't test.
 
Anduena Smith
Ranch Hand
Posts: 41
Oracle Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Piet Souris Thank you very much for the idea and again for your time
 
Piet Souris
Saloon Keeper
Posts: 3893
154
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
Anduena Smith
Ranch Hand
Posts: 41
Oracle Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing,
So for the tetrahedron we have:
Vertices:  (1,1,1),(1,-1,-1),(-1,1,-1),(-1,-1,1)
Indices: 0,1,2,   0,3,1,   1,3,2,  2,3,0
Normals: (1,1,-1),(1,-1,1),(-1,-1,-1),(-1,1,1).
Now I have to find them for the square pyramid. I've tried a lot of combinations but can't find the right one.. Maybe someone here can help me with it please?
 
Marshal
Posts: 68904
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Anduena Smith
Ranch Hand
Posts: 41
Oracle Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I hope everyone's doing well. I have to draw a square pyramid using Java3D and in order to do it I need to know the vertices, indices and normals that draw a square pyramid( or four-sided pyramid) but I'm having trouble figuring them out. Here's an example of them but for a tetrahedron:
Vertices:  (1,1,1),(1,-1,-1),(-1,1,-1),(-1,-1,1)
Indices: 0,1,2,   0,3,1,   1,3,2,  2,3,0
Normals: (1,1,-1),(1,-1,1),(-1,-1,-1),(-1,1,1).

I don't know if I can ask such questions in here since it's not about code but I think it's related to Java and I really need help . Thank you in advance if someone helps me
 
Campbell Ritchie
Marshal
Posts: 68904
275
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to be asking the same question twice. I shall try adding you to a very general forum, but I think you are all right where you are.
 
Piet Souris
Saloon Keeper
Posts: 3893
154
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Anduena,

how did you get the normals in the code in your opening post?

An advice for you: with these four - or five point figures, keep at least one triangle in the xy-plane, that is much much more clear than working with all these minusses in the coordinates..

In general: if you have the vectors (a1, a2, a3) and (b1, b2, b3) and you want to calculate a vector that is perpendicular to both (assuming these vectors are independant), then you can calculate the so called outproduct (or crossproduct as it is called in English). That vector is the vector(a2 * b3 - a3 * b2, a3 * b1 - a1 * b3, a1 * b2 - a2 * b1). The direction of this vector is according to a so called right-handed system: if you move the fingers of you right hand from vector first to vector second, then the direction of that normal is the direction your thumb is pointing to. Sounds complicated, but that direction is important to get the lighting correct. More about that later.

Now: look at the code in your opening post. We had the point A = (1, 1, 1), B = (1, -1, -1) and C = (-1, 1, -1). The triangle ABC has two directional vectors AB and BC. We calculate these:

AB = A - B = (1, 1, 1) - (1, -1, -1) = (0, 2, 2)
BC = B - C = (1, -1, -1) - (-1, 1, -1) = (2, -2, 0).

Takiing the crossproduct (that vector with a2b3 - a3b2 et cetera) we get the vector (4, 4, -4), and if we normalize it, we get the vector 1 / sqr(3) * (1, 1, -1).

This is the normal in point B, for the triangle ABC. And orf course A and C have the same normal.

And we do the same for the vertices of the other triangles. So, for ABC we have the normal indices 0, 0, 0. Et cetera.

As I said, the direction in which the normal points is important, since it should point to the outward of the triangle. The lighting formulas can then determine which sides are enlightend.

Now, with all those minusses and plusses in your coordinates it is hard to say which direction is the correct one: the one calculated from the crossproduct, or the one that points in the other direction.
So, check your theory about normals, it is a very important aspect.

Lets consider the square pyramid. Let the base be A = (0, 0, 0), B = (2, 0, 0), C = (2, 2, 0) and D = (0, 2, 0). The top T = (1, 1, 1). So we know that the normal in B must point downwards, in the negative Z-direction.

Lets do the calculation for point B:
AB = A - B = (-2, 0, 0)
BC = B - C = (0, -2, 0)

and so the normal = (0, 0, 4) or normalized (0, 0, 1). But this vector poins in the positive direction of the Z-axis, while we should have it pointing in the negative Z-diection. So we take (0, 0, -1)
and the points A and C have that same normal.

In short: I don't know what theory you have gotten sofar, but it is important to get a good grip on what is says. I hope this summation gives a little insight in the world of 3D modelling and lighting.
 
Paul Clapham
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Lets consider the square pyramid. Let the base be A = (0, 0, 0), B = (2, 0, 0), C = (2, 2, 0) and D = (0, 2, 0). The top T = (1, 1, 1).



That's certainly a square pyramid, but it doesn't have equilateral triangles for its three non-square sides. Although maybe that isn't part of the requirements... maybe Anduena can clarify that.
 
Anduena Smith
Ranch Hand
Posts: 41
Oracle Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you veryyyy much again @Piet Souris, you just reminded  me that I really need to learn some more theory and that's what I'll do
By the way I modified the class Tetrahedron  by  taking into consideration your lesson and here's what I did:


a m
It doesn't show a pyramid at the moment but I'll try some more after I learn more theory because I might've made mistakes.. Anyways, thank you a million, I really appreciate your help.

 
Piet Souris
Saloon Keeper
Posts: 3893
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you specify 8 points when you only need 5 of them?

And I must repeat my advice: put the base points in the xy-plane, and the top some point with a positive z-coordinate, it is then very easy to visualize what figure you are building.

For instance: if you look at your 5 unique points, with all the minus and plus coordinates, it is very hard to see if these form indeed a square pyramid. I tried to skech these points to the best of my abilities, but I could not detect any square pyramid (though my sketch abilities are very limitied, to say the least).

So, instead, let
A = (1, 1, 0)
B = (-1, 1, 0)
C = (-1, -1, 0)
D = (1, -1, 0)
T = (0, 0, 1)

then it is immediately clear what figure you are defining.

The triangles will be: ABC, BCD, ABT, BCT, CDT, DAT, giving the indices array
[0, 1, 2, 1, 2, 3, 0, 1, 4, 1, 2, 4, 2, 3, 4, 3, 0, 4]

And then for the normals: it is clear that for the triangles in the base (ABC and BCD) the normal would be (0, 0, -1). For ABT we have that the normal of ABT is (0, 1, 1) (why?). For triangle BCT we have the normal (-1, 0, 1), CDT gives (0, -1, 1) and DAT the normal (1, 0, 1) (not normalized). If we number these vectors 0 to 4, we get the indices array
[0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]

And finally: it might be that specifying a triangle as 0, 1, 2 will give the triangle some other properties that when specified (0, 2, 1). I don't know the software you are using, check it to be sure.

Succes!
 
This tiny ad is suggesting that maybe she should go play in traffic.
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic