Difference between revisions of "Source:setRelative"
(Made description more clear and added an example.) |
m (Corrected type of default value.) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{newin|[[0.9.0]]|090|type=function}} | {{newin|[[0.9.0]]|090|type=function}} | ||
− | Sets whether the [[ | + | Sets whether the [[Source]]'s position, velocity, direction, and cone angles are relative to the listener, or absolute. |
− | By default, all sources are absolute and therefore relative to the origin of love's coordinate system [0, 0, 0]. Only absolute sources are affected by the position of the listener. | + | By default, all sources are absolute and therefore relative to the origin of love's coordinate system [0, 0, 0]. Only absolute sources are affected by the position of the listener. Please note that positional audio only works for mono (i.e. non-stereo) sources. |
== Function == | == Function == | ||
=== Synopsis === | === Synopsis === | ||
Line 9: | Line 9: | ||
</source> | </source> | ||
=== Arguments === | === Arguments === | ||
− | {{param|boolean|enable ( | + | {{param|boolean|enable (false)|True to make the position, velocity, direction and cone angles relative to the listener, false to make them absolute.}} |
+ | |||
=== Returns === | === Returns === | ||
Nothing. | Nothing. | ||
Line 15: | Line 16: | ||
This example demonstrates how an absolute source is affected by the position of the listener. | This example demonstrates how an absolute source is affected by the position of the listener. | ||
<source lang="lua"> | <source lang="lua"> | ||
− | + | local x, y, z = 0, 0, 0; | |
− | local x, y, z; | ||
− | |||
− | |||
local snd; | local snd; | ||
+ | local relative; | ||
function love.load() | function love.load() | ||
− | snd = love.audio.newSource(' | + | snd = love.audio.newSource('beep.wav', 'static') |
− | |||
snd:setLooping(true); | snd:setLooping(true); | ||
snd:play(); | snd:play(); | ||
− | -- | + | -- By default the sound is not relative. |
− | + | relative = snd:isRelative(); | |
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | |||
function love.keypressed(key) | function love.keypressed(key) | ||
+ | -- Move the listener via WASD. | ||
if key == 'a' then | if key == 'a' then | ||
x = x - 1; | x = x - 1; | ||
− | |||
elseif key == 'd' then | elseif key == 'd' then | ||
x = x + 1; | x = x + 1; | ||
− | love.audio.setPosition(x, y, z); | + | elseif key == 'w' then |
+ | y = y - 1; | ||
+ | elseif key == 's' then | ||
+ | y = y + 1; | ||
+ | end | ||
+ | love.audio.setPosition(x, y, z); | ||
+ | |||
+ | -- Toggle between a relative and absolute source. | ||
+ | if key == 'r' then | ||
+ | relative = snd:isRelative(); | ||
+ | snd:setRelative(not relative); | ||
end | end | ||
+ | end | ||
+ | |||
+ | function love.draw() | ||
+ | love.graphics.print('Relative: ' .. tostring(relative), 20, 20); | ||
+ | love.graphics.print('Listener: (x = ' .. x .. ', y = ' .. y .. ')', 20, 40); | ||
end | end | ||
</source> | </source> |
Latest revision as of 22:45, 17 April 2020
Available since LÖVE 0.9.0 |
This function is not supported in earlier versions. |
Sets whether the Source's position, velocity, direction, and cone angles are relative to the listener, or absolute.
By default, all sources are absolute and therefore relative to the origin of love's coordinate system [0, 0, 0]. Only absolute sources are affected by the position of the listener. Please note that positional audio only works for mono (i.e. non-stereo) sources.
Contents
Function
Synopsis
Source:setRelative( enable )
Arguments
boolean enable (false)
- True to make the position, velocity, direction and cone angles relative to the listener, false to make them absolute.
Returns
Nothing.
Absolute Source Demonstration
This example demonstrates how an absolute source is affected by the position of the listener.
local x, y, z = 0, 0, 0;
local snd;
local relative;
function love.load()
snd = love.audio.newSource('beep.wav', 'static')
snd:setLooping(true);
snd:play();
-- By default the sound is not relative.
relative = snd:isRelative();
end
function love.keypressed(key)
-- Move the listener via WASD.
if key == 'a' then
x = x - 1;
elseif key == 'd' then
x = x + 1;
elseif key == 'w' then
y = y - 1;
elseif key == 's' then
y = y + 1;
end
love.audio.setPosition(x, y, z);
-- Toggle between a relative and absolute source.
if key == 'r' then
relative = snd:isRelative();
snd:setRelative(not relative);
end
end
function love.draw()
love.graphics.print('Relative: ' .. tostring(relative), 20, 20);
love.graphics.print('Listener: (x = ' .. x .. ', y = ' .. y .. ')', 20, 40);
end
See Also
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info