Problems with Rotation using Float2x2, converting from mat2

Hello,

This is been really bugging me, the resource I’ve been using to learn from, the book of shaders (written in glsl) uses a technique for rotation that I haven’t been able to translate with success.

It’s using a rotation matrix, cos,sin,-sin,cos something like that, the return value is a mat2 in glsl, but the function is set up with a vec2, which doesn’t make sense to me when turning into hlsl, I don’t know how it’s returning a different type.

vec2 rotate(vec2 p, float angle)
{
    float c = cos(angle);
    float s = sin(angle);
   
    return mat2(c,s,-s,c) * p;
}

my ghetto way was just to chop off the end like this below, doesn’t really work if you want to make something specific like a rotating tunnel.

float2 rotate(float2 p, float angle)
{
    float c = cos(angle);
    float s = sin(angle);
   
    return float2(c,s) * p;
}

Does anyone have nay ideas of what to change it to? I know it should be float2x2, but haven’t managed to get it to work, says type mismatch. I’m not quite sure what to do, any thoughts?

It’s one of the subtle differences between GLSL and HLSL.

GLSL:
mat2(1,1,1,1) * vec2(1,1);

HLSL:
mul(float2(1,1), float2x2(1,1,1,1));

3 Likes

Ahh thank you so much!! That one was killing me!