Difference between revisions of "BezierCurve:getDerivative"

(Added example; feel free to refine/simplify, but i'd keep the sprite in, due to the fact that probably a lot of use cases would want to rotate an image like that... like how the article itself says so as well.)
Line 13: Line 13:
 
=== Returns ===
 
=== Returns ===
 
{{param|BezierCurve|derivative|The derivative curve.}}
 
{{param|BezierCurve|derivative|The derivative curve.}}
 +
== Example ==
 +
=== Draw a bezier 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]]

Revision as of 17:33, 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

Draw a bezier 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