Draw a texture rotated, BUT not around the center?

Okay, so I’m recreating the CSS radar. Picture… It’s really simple, but right now it doesn’t rotate depending on your aim like in css. Does anyone know a way to draw a rotated texture on a defined axis and not the center? I’m guessing I could pull it off with Surface.DrawTexturedRectRotated, but I would have to set its position based on where I want to rotate it and how much it’s rotated. I bet that would be a pain in the ass… I figured I’d ask if anyone knows a way to do this before I attempt at it.

[lua]
function DrawRotatedTexture( x, y, w, h, angle, cx, cy )

if( !cx and !cy ) then
	
	surface.DrawTexturedRectRotated( x, y, w, h, angle );
	
else
	
	local sin, cos = math.sin( math.rad( angle ) ), math.cos( math.rad( angle ) );
	
	x = x - sin * cx;
	y = y - cos * cy;
	
	surface.DrawTexturedRectRotated( x, y, w, h, angle );
	
end

end
[/lua]

I just woke up so I’m not sure if it will work, but you’ll probably get the idea.

Makes sense. I’ll try it when I get home. :smiley: Thanks.

Well it works to an extent… as long as cx and cy are equal. I get weird results when they aren’t.

It’s not working as expected though, like if I make cx and cy 0, it should rotate at the top left corner of the texture, not the center.

I’ll mess around with this though.

I think it only works when its a square because sin/cos will just be an oval, its orientation might not be right to rotate with.

Might be able to use a vector and rotatearoundaxis? Maybe? lol

Holy shit I love you. I would’ve never thought of that! It works perfectly.

[lua]function DrawRotatedTexture( x, y, w, h, angle, cx, cy )

if( !cx and !cy ) then
	
	surface.DrawTexturedRectRotated( x, y, w, h, angle )
	
else
	
	local vec = Vector( w/2-cx, cy-h/2, 0 )
	vec:Rotate( Angle(180, ( angle + 90 ) * -1, -180) )
	surface.DrawTexturedRectRotated( x+vec.x, y+vec.y, w, h, angle )
	
end

end[/lua]

nitpicky, but allows you to specify only one of the c values.
[lua]function DrawRotatedTexture( x, y, w, h, angle, cx, cy )
cx,cy = cx or w/2,cy or w/2
if( cx == w/2 and cy == w/2 ) then

	surface.DrawTexturedRectRotated( x, y, w, h, angle )
	
else
	
	local vec = Vector( w/2-cx, cy-h/2, 0 )
	vec:Rotate( Angle(180, ( angle + 90 ) * -1, -180) )
	surface.DrawTexturedRectRotated( x+vec.x, y+vec.y, w, h, angle )
	
end

end[/lua]

There was a problem with the angles… All fixed.

[lua]function DrawRotatedTexture( x, y, w, h, angle, cx, cy )
cx,cy = cx or w/2,cy or w/2
if( cx == w/2 and cy == w/2 ) then

	surface.DrawTexturedRectRotated( x, y, w, h, angle )
	
else
	
	local vec = Vector( w/2-cx, cy-h/2, 0 )
	vec:Rotate( Angle(180, angle, -180) )
	surface.DrawTexturedRectRotated( x-vec.x, y+vec.y, w, h, angle )
	
end

end[/lua]

Hehehe, now my radar is a lot better because of this. http://i30.tinypic.com/opdbgj.jpg

CS:S Gamemode?