**0**

# [XNA] Spotlight cone model rotation. ViewMatrix Decompose Problem.

Started by Aug 01 2012 05:27 AM

,
2 replies to this topic

###
#1
Members - Reputation: **188**

Posted 01 August 2012 - 05:27 AM

Hi everyone!

I'm having a little issue when trying to decompose a view matrix:

I basically have a spot light which works fine.

I added an alpha blended cone model to create a light volume effect. It looks allright & everything is fine except its rotation.

I thought I could probably create a view matrix for the light (with position & target) & decompose it to get an automatically made rotation matrix (to apply to the cone & get the same rotation as the light).

But when I decompose it, the values I get don't seem to be correct. The translation matrix is not working for me & the rotation doesn't seem to be right.

I wonder why is this happening &, in case I cannot achieve it like this, how may I get a rotation from a source & a target point in 3D (this http://www.chriskugl...en-two-vectors/ didn't work for me, maybe I'm doing smth wrong, but I don't know...)

Here's how I create the view matrix & then I decompose it:

[source lang="csharp"]light.view = Matrix.CreateLookAt(light.position, myPlayer.animatedModel.position, Vector3.Up);bool deco=light.view.Decompose(out scaleView, out rotationView, out translationView);[/source]

I also attach a pic showing the light (following the player) & the cone (moving in a way that sometimes matches the light movement but it's not right). The light diameter is exagerated to illustrate the case, the idea is that the cone is scaled depending on the light diameter, I just have to tweak it a bit somehow to match). The cone doesn't seem to move in X, it moves in Z following the light movement but not matching it. It moves in a very restricted range & X movement seems to be rendered in Z.

Thanks in advance,

Synth

P.S.: I'm not sure I'm posting this in the right forum because it mixes math, graphics & xna elements, so forgive me if I'm wrong. Thanks.

I'm having a little issue when trying to decompose a view matrix:

I basically have a spot light which works fine.

I added an alpha blended cone model to create a light volume effect. It looks allright & everything is fine except its rotation.

I thought I could probably create a view matrix for the light (with position & target) & decompose it to get an automatically made rotation matrix (to apply to the cone & get the same rotation as the light).

But when I decompose it, the values I get don't seem to be correct. The translation matrix is not working for me & the rotation doesn't seem to be right.

I wonder why is this happening &, in case I cannot achieve it like this, how may I get a rotation from a source & a target point in 3D (this http://www.chriskugl...en-two-vectors/ didn't work for me, maybe I'm doing smth wrong, but I don't know...)

Here's how I create the view matrix & then I decompose it:

[source lang="csharp"]light.view = Matrix.CreateLookAt(light.position, myPlayer.animatedModel.position, Vector3.Up);bool deco=light.view.Decompose(out scaleView, out rotationView, out translationView);[/source]

I also attach a pic showing the light (following the player) & the cone (moving in a way that sometimes matches the light movement but it's not right). The light diameter is exagerated to illustrate the case, the idea is that the cone is scaled depending on the light diameter, I just have to tweak it a bit somehow to match). The cone doesn't seem to move in X, it moves in Z following the light movement but not matching it. It moves in a very restricted range & X movement seems to be rendered in Z.

Thanks in advance,

Synth

P.S.: I'm not sure I'm posting this in the right forum because it mixes math, graphics & xna elements, so forgive me if I'm wrong. Thanks.

###
#2
Moderators - Reputation: **17717**

Posted 01 August 2012 - 11:54 AM

The Matrix class already has the CreateWorld method, which does what you want. No need to make a view matrix.

FYI, what that function does is generate a "right" vector by crossing the "forward" and "up" vectors and normalizing the result. Then it generates a new "up" vector by crossing "right", and "forward". These three vectors are the basis vectors for your orientation, so you just stick them in the first three rows of a matrix (right in the first row, up in the second, forward in the third). The position then goes in the translation portion of the matrix, which is the last row.

FYI, what that function does is generate a "right" vector by crossing the "forward" and "up" vectors and normalizing the result. Then it generates a new "up" vector by crossing "right", and "forward". These three vectors are the basis vectors for your orientation, so you just stick them in the first three rows of a matrix (right in the first row, up in the second, forward in the third). The position then goes in the translation portion of the matrix, which is the last row.

###
#3
Members - Reputation: **188**

Posted 03 August 2012 - 12:28 PM

Hi Matt,

thanks for your answer, the info you gave me tempted me to dig more on matrices & now I understand them much better

After I read more, played with matrices & applied the Matrix.CreateWorld, the problem persisted anyway. I spent all the afternoon double & triple checking everything & I wasn't able to find anything wrong in the code! So I created the model over & over again... I recorded a video showing the problem & I was already writing here a new message when I had an insight...: "am I exporting the model left handed...?" Daaaaaaamn that was the problem! Half of me felt as a dumb @ss, but the other half shouted: "YEEEEEEAAAH!!"

hehe, so it works like a charm!

I actually want to thank you for pointing me out this info (I didn't know how matrices were storing this data) as now I understand it & it helps me for future usages!

Cheers,

Synth

thanks for your answer, the info you gave me tempted me to dig more on matrices & now I understand them much better

After I read more, played with matrices & applied the Matrix.CreateWorld, the problem persisted anyway. I spent all the afternoon double & triple checking everything & I wasn't able to find anything wrong in the code! So I created the model over & over again... I recorded a video showing the problem & I was already writing here a new message when I had an insight...: "am I exporting the model left handed...?" Daaaaaaamn that was the problem! Half of me felt as a dumb @ss, but the other half shouted: "YEEEEEEAAAH!!"

hehe, so it works like a charm!

I actually want to thank you for pointing me out this info (I didn't know how matrices were storing this data) as now I understand it & it helps me for future usages!

Cheers,

Synth