View Full Version : Script question

 Mic_Ma09 September 2006, 09:11 AMI' pretty new to scripting. I was trying to write a script that gives me the angle between the camera's line of sight and each vertex normal. I get the point-normals through geometry.points(n).normal. but how do I figure out the line of sight of the camera? I tried calculate the angle between camera position and vertex, but, uh, the results are ok-ish but kind of weird.
ThE_JacO
09 September 2006, 10:26 AM
get the direction of the camera's Z axis from the rotation into a vector, normalize it, and calculate the angle or use a dotproduct (the dotproduct is more convenient if you're trying to figure out wether a normal is facing the camera or not)

Mic_Ma
09 September 2006, 01:51 AM
get the direction of the camera's Z axis from the rotation into a vector,

Yes, dot-product sounds better than just the angle.

But, hm. I am being a bit thick here. I get the rotation of the camera like this:

camrotz = oCam.kinematics.local.Parameters("rotz").value

But it is scalar, how can I turn a scalar into a vector?

ThE_JacO
09 September 2006, 03:10 AM
I didn't say get the rotation, I said get the -direction- of the Zaxis from the rotation, which is part of the transformation matrix :)

you can get the transformation matrix and isolate the Z axis if you want, or do it a number of other ways.

what you're getting right now is an angle used in a euler system to produce part of a transformation matrix, that's got little to do with the direction you need (although you could derivate from those it's a loopsided way to do it and fairly computationally expensive, whereas isolating elements from a matrix that's already there is pretty much a freebie).

Mic_Ma
09 September 2006, 03:52 AM
Huhh....thanks...that sounds...more difficult than I thought...

Hmm.

I am almost totally guessing, clueless about programming. I did the following (should run in New Scene). Does that give me the direction vector the camera looks at??? :shrug:

'-----GET CAMERA FROM SCENE--------

set oCamerasCollection = ActiveSceneRoot.FindChildren(Camera)
set oCam = oCamerasCollection(1)

'------GET CAMERA LOCAL ROTATION VALUES----------------

camrotz = oCam.kinematics.local.Parameters("rotz").value
camroty = oCam.kinematics.local.Parameters("roty").value
camrotx = oCam.kinematics.local.Parameters("rotx").value

'------FIND ROTATION QUATERNION-------------------------

set oRotation = XSIMath.CreateRotation( camrotx , camroty , camrotz)
set oQuaternion = XSIMath.CreateQuaternion()

oRotation.GetQuaternion oQuaternion

set vCamNormal = XSIMath.CreateVector3
vCamNormal.set oQuaternion.x, oQuaternion.y, oQuaternion.z
vCamNormal.NormalizeInPlace

'-------LOG MESSAGE-----------------------------------

LogMessage "vCamNormal.x:.." & vCamNormal.x
LogMessage "vCamNormal.y:.." & vCamNormal.y
LogMessage "vCamNormal.z:.." & vCamNormal.z

ThE_JacO
09 September 2006, 06:17 AM
this has got nothing to do with programming.
vectors are first explained to you when you're 14old, matrices might or might not come, depending from your studies, a couple years later. Regardless, linear algebra basics are trivially simple and tremendously useful to learn, check out the books reccomendations thread to get some pointers on what to buy to self-educate.

as for being complicated...
a matrix containing a rotation (and scaling) is simply an ordered bidimensional array of 9 numbers.

An ID matrix(default rotation and scaling) looks like:
1 0 0
0 1 0
0 0 1

taking three values out of that (either a row or a column, depending from what standards the API you're using goes by) will give you the vector representing the Z axis, as those 9 numbers are nothing else then the 9 scalars describing the orientation and magnitude of your transform's vectors.
not that hard to take 3 numbers out of a table is it? :)

Mic_Ma
09 September 2006, 07:03 AM
not that hard to take 3 numbers out of a table is it?

Actually THAT is the hardest part. I can't figure out how to access that vector out of the transformation matrix in xsi. Very frustrating. The helpfile is more confusing than helpful.

Well, I guess programming remains a dark art for me.

ThE_JacO
09 September 2006, 03:10 PM
I'm afraid you're still confusing programming and maths, and the documentation about the XSIMath object is decent enough (although a few details are a bit remote from where I'd like them, I doubt you encountered that yet).

You don't magically suqeeze a vector out of a transformation matrix through some arcane black programming art.
The actual situation is a lot simplier then you seem to think it, you just lack some fundamentals of linear algebra probably, but nothing a 20\$ book and the intarweb can't fix in a couple weeks.

a vector is a 3x1 matrix itself, which means all you need to create one is 3 scalars.
a transformation matrix without translation (which you don't need) is 9 values, the "last" three representing the Z axis vector.
create an empty vector with XSIMath.CreateVector3()
and then populate its x, y and z values with the x, y and z values from the last row/column (depending on notaiton) of your matrix.

programming wise this requires nothing more then instancing a vector object and reading 3 straight values from another given object to put them in there.

don't get scared by words like matrix, vectors, cross product, dot product etc. it's really quite simple stuff.
the truth is they are glaringly simple concepts, easily visualized by anybody of average or better intelligence, but hugely mystified by their scary names.

mocaw
09 September 2006, 07:10 PM
please delete this was a double post

mocaw
09 September 2006, 07:13 PM
I just think that many of us have to have a look at several sources on this use of math to speak the same language as you do JacO. In the US, many of us were taught math in a very lame and dumbed down practical way that made many of our eyes glaze over.

IE algebraic examples that involve people buying apples at a store, and trying to figure out who spent X amount of money etc. out of three people buying the apples with Y amount of money. I think this kind of teaching leads to railroading many of us to look at math less as a language, and more as a tool. We then only see the tool in the way the example is given- IE it's a hammer- so you hammer with it. So we have lost the idea of true context etc. that a language would give us- and the variable meaning and ability to make tools with it, and describe.

I have yet to study up on the maths described by you, but even several years ago when I started to use expressions I had the thought that this SHOULD be the way children are taught maths- I think teaching children through 3D examples, on simple projects, would allow them to express ideas through math better and break down this old "tool" way of looking at it. Hell it would prevent that kind of dead end logic in the first place and be far more rewarding.

I can see many more children getting excited by taking a rocket ship they've made, and trying to get it to hit XYZ point in 3D space etc. than two trains speeding at each other.

I'm not saying we need edu-tainment here, just more hands on and "real world" examples here in the US. People don't use algebra in daily life to buy apples, or check on trains speeding into each other, but they do use it for 3D, rocket science, biology, etc. etc. and those should be the basis of the examples IMHO.

solarflare
09 September 2006, 08:16 PM
Math uses an interesting portion of your brain, and I don't believe everyone understands it the same. For me, calculus was the easiest way to see that. I knew so many smart people who just could NOT understand calculus no matter how hard they tried.

I felt the way I was taught math was fine (in the US). For me, they just taught the concepts in the form of expressions (5 + 4 = ?), and then the word problems (like the example of the train) simply forced me to translate words into equations. The boringness of the actual problem was never really relevant. In fact, that's basically all that physics (at least at intro levels) is about. You just need to figure out what equations to use, and what values from the word problem go where.

Anyways, I don't really know too much about XSI programming at this point, but I believe the idea of what Jaco is saying is that you can make some function call to get the 3x3 rotation matrix strait from XSI. From there, you just get the last row/column, or whatever corresponds to the z-axis and that is the z-direction. Here is an example:

Z direction in global coordinates = (0,0,1).
The camera has been rotated some amount.
This can be represented by matrix R.
To get the new Z-direction, you simply rotate the (0,0,1) by R:
R = [x1, y1, z1
x2, y2, z2
x3, y3, z3]
(0,0,1) = [0
0
1]

R * (0,0,1) = (x1*0 + y1*0 + z1, x2*0...) = (z1, z2, z3).

So in this case, the z-direction is just the third column of the R matrix. However, from what Jaco said I am guessing you can do this rotation differently, such that you need the last row (instead of (0,0,1) making a column vector, have it make a row vector and put it in front of the matrix).

ThE_JacO
09 September 2006, 02:27 AM
I just think that many of us have to have a look at several sources on this use of math to speak the same language as you do JacO. In the US, many of us were taught math in a very lame and dumbed down practical way that made many of our eyes glaze over.
Sure, it happens to be the case in other countries as well, however some people manage to pick it up even then, which is proof that it doesn't require a broad understanding of many other subjects to grasp.
This would be about mis-education and schooling systems though, and that's a minefield to venture in.

IE algebraic examples
... snip ....
I'm not saying we need edu-tainment here, just more hands on and "real world" examples here in the US. People don't use algebra in daily life to buy apples, or check on trains speeding into each other, but they do use it for 3D, rocket science, biology, etc. etc. and those should be the basis of the examples IMHO.

well, in my opinion we DO need edutainment in schools and in life.
I try to force myself to read at least a book a week and at least one technical and/or historical biography every month or two, often practicing speed reading every other book.
It's hard to get an interest in something, anything at all infact, if it's poorly written, completely devoid of anedoctal cases and generally not fun to read, and I fail to see how this shouldn't apply to all fields of study, no matter how situational the study is.

When you're 15 it's hard to understand why those two ropes and gravity need to be represented with a vector, so it's perfectly forgivable that kids are uninterested and bored when the teacher isn't good, but if you work (or want to work) in CG then you need a language platform to discuss it.

99% of the people reading this thread know words like raytracing, samples, SSS etc, and are not intimidated by any of these words, because they have been presented to us in a context where we already had a growing interest in the subject.
Other words however have been introduced to us much earlier, and now for many carry a stigmata of being technical and hazy, and not everybody wants to make an effort to re-learn them.
That is a crying shame, you defined math well when you said it's a language.
All maths will do for you is giving you the ability to describe concept that would otherwise be too abstract or complex for normal english to deal with.

When you use the word apple you know pretty well what you're talking about, yet you probably don't know how it was grown, what pesticides or agricultural techniques were used, nor are concerned with the market dynamics of selling or buying apples, yet you accept that the word apple and the actual object are part of your language and you need to know what one is to have certain conversations.
Maths works exactly the same way.
You don't need to go all the way back to knowing all the axioms, or knowing off the top of your head all the properties of algebric operations, before you understand what a vector is, but to have a conversation about 3D where both sides can share and understand, knowledge of what sampling, interpolation, linear algebra etc. are is fairly necessary, and not so hard to deal with that it should excuse not putting some effort into it.

I'm more then happy to crusade for the learning of the basics, and to keep explaining the same concepts over and over. I think it's acceptable that people need a lot of pushing to get past their scare of "technical" concepts.
I still stand by the saying "give a man a fish and he won't be hungry for the day, teach a man to fish, and he'll never be hungry again" though; which is why I try to avoid spoonfeeding explicit examples and code bits unless it's instrumental to learning the backend concepts.

I'm done babbling now.

Mic_Ma
09 September 2006, 03:10 AM
Not everybody who has learned maths remembers it well enough years and years after. Yes, I did vectors, and quite a bit more after that, I vaguely remember Fourier Transforms (unrelated to this) and that I was about get to tensors. But that was 10 years ago, and I haven't touched maths since.

There is often confusion with terms (vectors after all are something completely different in, say, Biology, and in the maths I learned they were not merely a set of 3 numbers) and programming seems to be full of terms that sound familiar, yet have different meanings from the ones I -hazily- remember.

Call me stupid, but I find it quite hard to start scripting anything useful. You don't know a programming term and look it up, only to find that it is explained with 10 other terms you don't know. It's quite a jungle to get through, and frustrating it is. I'm sure it's not that hard and can be done. I'm also sure it could be made a lot easier and more accessible to a wider public.

So, I find it a bit offensive to assume that everyone who starts scripting -and gets it a bit wrong- to be ignorant of the underlying principles.

Sbowling
09 September 2006, 05:45 AM
I completely agree that math (and most other things) are poorly taught in the US. Don't get me started on the P.C. B.S. that they are teaching is schools today. I never learned a lot of these things in school because the subject didn't interest me in the least and I couldn't understand why I would ever need to use any of this. My Jr. High school math teacher didn't seem like he was the least bit interested in really teaching. He used to give out the assignment at the beginning of class and then would go sit in the back of the room reading Issac Asimov books for he rest of the period.

I feel very strongly that getting kids interested in what they are learning is vital to really teaching them. Unfortunately the P.C. Public school system in the U.S. just isn't that interested in teaching kids any more. In fact, from watching what my niece has been going through it really looks like they are trying to make kids not want to learn anything. Hell, it's not even P.C. to have games where people lose. Yeah, that will really prepare a kid for life in the real world. The more I think back on this stuff, the more I feel like I was robbed of something very valuable.:sad:

It wasn't until I got into computers and 3d graphics that I started to really understand where the higher level math becomes very valuable. I've recently become very interested in math and have been trying to learn more of the advanced stuff that I never learned in school. One of the funny things I discovered is that I had actually taught myself a lot of more advanced math skills just by dealing with 3d graphics. I just didn't know I had learned them at the time.

Anyway, getting back to math, there are some math videos on Amazon.com by Jason Gibson that walk you through calculus and algebra and other mathematics and seem to do a pretty good job. I haven't watched any of them all the way through yet, but what I have seen has been very well done and I don't find Boring to watch. IOW, I don't feel like I'm sitting in a class room. :eek:

Sbowling
09 September 2006, 05:56 AM
Call me stupid, but I find it quite hard to start scripting anything useful. You don't know a programming term and look it up, only to find that it is explained with 10 other terms you don't know. It's quite a jungle to get through, and frustrating it is. I'm sure it's not that hard and can be done. I'm also sure it could be made a lot easier and more accessible to a wider public.
[QUOTE]

I actually find scripting in XSI to be incredibly easy. The SDK has some great examples and I've been able to do things I NEVER would have thought I could do with scripting. If you think scripting is bad in XSI, you should try scripting in something like Lightwave! :eek:

[Quote] So, I find it a bit offensive to assume that everyone who starts scripting -and gets it a bit wrong- to be ignorant of the underlying principles.

IGNORANT
1.lacking in knowledge or training; unlearned: an ignorant man.
2.lacking knowledge or information as to a particular subject or fact: ignorant of quantum physics.
3.uninformed; unaware.
4.due to or showing lack of knowledge or training: an ignorant statement.

According to #2 above, it seems like ignorance is exactly why people get things a bit wrong. It's not a bad thing, it just means you don't fully understand it. If you did fully understand it, you probably wouldn't get it a bit wrong. Being called ignorant is not the same thing as being called stupid, it just means you dodn't know. I'm ignorant of many, many things and not afraid to admit it.:)

ThE_JacO
09 September 2006, 07:43 AM
So, I find it a bit offensive to assume that everyone who starts scripting -and gets it a bit wrong- to be ignorant of the underlying principles.

It sounds like you got offended somewhat by my posts, which might, or might not, be a consequence of the fact I don't abuse smileys or shoulderpatting every other paragraph.
My posts however were not meant to be offensive, if I think somebody "too stupid to get it" I normally don't even bother posting to start with.

Anyway...
you didn't start script it and get it a bit wrong.
You are facing a fairly extensive ignorance of linear algebra if you confuse roation with direction, or if you think that anything more complex then getvalue and setvalue is needed to get a vector out of a matrix (partializing a matrix into another), and it might also be why the docs are unclear to you.

so yes, I am assuming that you are ignorant of the underlying principles, and it still seems markedly so to me. As a consequence I tryied to point you in the direction of reading up on what I think would benefit you the most.

If you find it offensive think again; does it take me longer to write all these posts, or would it take me longer to write a scripted example (roughly a 3mins job) to "shut you up", and to contribute to your current state of confusion?

I loath the modern derogatory slant that people give to the word ignorance, or to somebody else considering you so, it makes sharing, teaching and learning so much harder it's not funny anymore.
Of course I'm assuming you ignorant of the underlying principles involving linear algebra if you don't know how to get a vector out of a transformation matrix.
I would be equally ignorant if a friend of mine who's been a researcher for Bayer for the last 11 years (roughly how long I've been employed in this industry) was to discuss chemistry with me when I only have a passing interest in it.
How this form of unconditional honesty is offensive or counterproductive in terms of sharing knowledge is still slipping past me.

CGTalk Moderation
09 September 2006, 07:43 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.

1