Difference between revisions of "Tutorial:Graphic Transformations"

m
m (@ to at)
 
(2 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
<source lang="lua">
 
<source lang="lua">
 
function love.draw()
 
function love.draw()
love.graphics.push() -- store the previous transformation state
+
love.graphics.push() -- Store the previous transformation state
-- shift the coordinate system down 10, right 10
+
-- Shift the coordinate system down 10, right 10
 
love.graphics.translate(10,10)
 
love.graphics.translate(10,10)
 
love.graphics.point(0,0)
 
love.graphics.point(0,0)
love.graphics.pop() -- return to the previous transformation state
+
love.graphics.pop() -- Return to the previous transformation state
love.graphics.point(0,0) -- the origin is back @ (0,0)
+
love.graphics.point(0,0) -- The origin is back at (0,0)
 
end
 
end
 
</source>
 
</source>
Line 31: Line 31:
 
love.graphics.translate(10,10)
 
love.graphics.translate(10,10)
 
love.graphics.scale(-1,-1)
 
love.graphics.scale(-1,-1)
love.graphics.point(10,10) -- the point is located @ global (0,0)
+
love.graphics.point(10,10) -- The point is located at global (0,0)
 
love.graphics.pop()
 
love.graphics.pop()
 
 
 
love.graphics.scale(-1,-1)
 
love.graphics.scale(-1,-1)
 
love.graphics.translate(10,10)
 
love.graphics.translate(10,10)
love.graphics.point(10,10) -- the point is located @ global (-20,-20)
+
love.graphics.point(10,10) -- The point is located at global (-20,-20)
 
end
 
end
 
</source>
 
</source>
Line 46: Line 46:
 
The second way (which is more intuitive in my humble opinion) is to visualize each transformation in  
 
The second way (which is more intuitive in my humble opinion) is to visualize each transformation in  
 
REVERSE ORDER relative to the global coordinate system. Let's take the first point as an example.  
 
REVERSE ORDER relative to the global coordinate system. Let's take the first point as an example.  
Scaling is applied first and places the new point @ (-10,-10). Lastly, the translation moves the new  
+
Scaling is applied first and places the new point at (-10,-10). Lastly, the translation moves the new  
 
point to (0,0).
 
point to (0,0).
 +
 +
== See Also ==
 +
* [[love.graphics.scale]]
 +
* [[love.graphics.shear]]
 +
* [[love.graphics.rotate]]
 +
* [[love.graphics.translate]]
  
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
{{#set:LOVE Version=0.7.0}}
+
{{#set:LOVE Version=0.8.0}}
 
{{#set:Description=Covers the basic of using graphic transformations}}
 
{{#set:Description=Covers the basic of using graphic transformations}}
  
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|Tutorial:Graphic Transformations}}
 
{{i18n|Tutorial:Graphic Transformations}}

Latest revision as of 22:44, 26 September 2013

This tutorial covers the basics of using graphic transformations.

love.graphics.push/love.graphics.pop

These two functions allow you to save and return to the previous transformation state. Every push must be paired up with a pop, and each pair can also be called within another push/pop.

Here's an example:

function love.draw()
	love.graphics.push() -- Store the previous transformation state
		-- Shift the coordinate system down 10, right 10
		love.graphics.translate(10,10)
		love.graphics.point(0,0)
	love.graphics.pop() -- Return to the previous transformation state
	love.graphics.point(0,0) -- The origin is back at (0,0)
end

Order of Transformations

Graphic transformations are not commutative. This means the call order affects the final result.

Here's an example:

function love.draw()
	love.graphics.push()
		love.graphics.translate(10,10)
		love.graphics.scale(-1,-1)
		love.graphics.point(10,10) -- The point is located at global (0,0)
	love.graphics.pop()
	
	love.graphics.scale(-1,-1)
	love.graphics.translate(10,10)
	love.graphics.point(10,10) -- The point is located at global (-20,-20)
end

There are two ways to visualize the above code.The first way is to think of each transformation as relative to the new coordinate system from previous transformations. For example, the scaling to the first point would be applied relative to the new origin at (10,10).

The second way (which is more intuitive in my humble opinion) is to visualize each transformation in REVERSE ORDER relative to the global coordinate system. Let's take the first point as an example. Scaling is applied first and places the new point at (-10,-10). Lastly, the translation moves the new point to (0,0).

See Also


Other Languages