Difference between revisions of "BezierCurve:getDerivative"

(Created page with "{{newin|0.9.0|090|type=function}} Get the derivative of the Bézier curve. This function can be used to rotate sprites moving along a curve in the direction of the movement ...")
 
m (fixed example subtitle)
 
(2 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
Get the derivative of the Bézier curve.
 
Get the derivative of the Bézier curve.
  
This function can be used to rotate sprites moving along a curve in the direction of the movement and compute the direction perpendiculat to the curve at some parameter t.
+
This function can be used to rotate sprites moving along a curve in the direction of the movement and compute the direction perpendicular to the curve at some parameter t.
  
 
== Function ==
 
== Function ==
Line 13: Line 13:
 
=== Returns ===
 
=== Returns ===
 
{{param|BezierCurve|derivative|The derivative curve.}}
 
{{param|BezierCurve|derivative|The derivative curve.}}
 +
== Example ==
 +
=== Position a sprite along a Bézier-curve. ===
 +
<source lang="lua">
 +
curve      = love.math.newBezierCurve({25,25,75,50,125,25})
 +
derivative = curve:getDerivative()
 +
sprite    = love.graphics.newImage('sprite.png') -- to demonstrate orientation; assuming the sprite's front is upwards.
 +
 +
local t = 0.0 -- range: [0,1]
 +
function love.update(dt)
 +
t = (t + dt / 10) % 1.0
 +
end
 +
 +
function love.draw()
 +
local ex,ey = curve:evaluate(t)
 +
local dx,dy = derivative:evaluate(t)
 +
-- If the sprite were facing to the right, the +math.pi/2 part would not be needed.
 +
love.graphics.draw(sprite, ex, ey, math.atan2(dy,dx)+math.pi/2, 1, 1, sprite:getWidth()/2, sprite:getHeight()/2)
 +
end
 +
</source>
 
== See Also ==
 
== See Also ==
 
* [[parent::BezierCurve]]
 
* [[parent::BezierCurve]]

Latest revision as of 18:08, 6 October 2020

Available since LÖVE 0.9.0
This function is not supported in earlier versions.

Get the derivative of the Bézier curve.

This function can be used to rotate sprites moving along a curve in the direction of the movement and compute the direction perpendicular to the curve at some parameter t.

Function

Synopsis

derivative = BezierCurve:getDerivative()

Arguments

None.

Returns

BezierCurve derivative
The derivative curve.

Example

Position a sprite along a Bézier-curve.

curve      = love.math.newBezierCurve({25,25,75,50,125,25})
derivative = curve:getDerivative()
sprite     = love.graphics.newImage('sprite.png') -- to demonstrate orientation; assuming the sprite's front is upwards.

local t = 0.0 -- range: [0,1]
function love.update(dt)
	t = (t + dt / 10) % 1.0
end

function love.draw()
	local ex,ey = curve:evaluate(t)
	local dx,dy = derivative:evaluate(t)
	-- If the sprite were facing to the right, the +math.pi/2 part would not be needed.
	love.graphics.draw(sprite, ex, ey, math.atan2(dy,dx)+math.pi/2, 1, 1, sprite:getWidth()/2, sprite:getHeight()/2)
end

See Also

Other Languages