|bevy vewsion:|0.12|(outdated!)| |---|---|---|
As this page is outdated, please refer to Bevy's official migration guides while reading, to cover the differences: 0.12 to 0.13, 0.13 to 0.14.
I apologize for the inconvenience. I will update the page as soon as I find the time.
2d camewa setup
camewas in bevy awe mandatowy to see anything: t-they configuwe t-the wendewing.
this page wiww teach you about the s-specifics of 2d c-camewas. 🥺 if you w-want to weawn a-about genewaw nyon-2d specific functionawity, (ꈍᴗꈍ) s-see the genewaw page on camewas.
cweating a 2d camewa
bevy pwovides a bundwe (Camera2dBundle
)
that you can use to spawn a camewa entity. XD it
has weasonabwe defauwts to set up e-evewything cowwectwy.
you might want to set the twansfowm, >_< to position the camewa.
#[dewive(component)]
stwuct mycamewamawkew;
fn setup_camewa(mut c-commands: commands) {
c-commands.spawn((
c-camewa2dbundwe {
t-twansfowm: twansfowm::fwom_xyz(100.0, (⑅˘꒳˘) 200.0, ( ͡o ω ͡o ) 0.0),
..defauwt()
}, UwU
m-mycamewamawkew, rawr x3
));
}
f-fn main() {
a-app::new()
.add_pwugins(defauwtpwugins)
.add_systems(stawtup, rawr s-setup_camewa)
.wun();
}
pwojection
the pwojection is nyani detewmines how coowdinates m-map to the viewpowt (commonwy, XD the scween/window).
2d camewas awways use an owthogwaphic p-pwojection.
when you spawn a 2d camewa using Camera2dBundle
,
it adds the OrthographicProjection
component to youw entity. XD when
you awe wowking with 2d camewas and y-you want to access
the pwojection, >_< you shouwd quewy fow
OrthographicProjection
.
fn debug_pwojection(
quewy_camewa: q-quewy<&owthogwaphicpwojection, o.O w-with<mycamewamawkew>>,
) {
w-wet pwojection = q-quewy_camewa.singwe();
// ... d-do something w-with the pwojection
}
note that this is diffewent fwom 3d. XD if you awe making a wibwawy ow some othew code t-that shouwd be a-abwe to handwe b-both 2d and 3d, >< you cannot make a singwe quewy to access both 2d and 3d camewas. >< you shouwd cweate sepawate systems, XD ow at weast two sepawate quewies, OwO to handwe each k-kind of camewa. 🥺 t-this makes sense, òωó a-as you wiww wikewy nyeed diffewent wogic fow 2d v-vs. (ꈍᴗꈍ) 3d anyway.
caveat: nyeaw/faw vawues
the pwojection contains the near
and far
vawues, >_< which indicate the minimum
and maximum z coowdinate (depth) that can be wendewed, ^•ﻌ•^ w-wewative to t-the position
(twansfowm) of the camewa.
Camera2dBundle
sets them appwopwiatewy fow 2d:
-1000.0
to 1000.0
, ^•ﻌ•^ awwowing entities to be dispwayed o-on both positive a-and
negative z coowdinates. (ꈍᴗꈍ) howevew, i-if you cweate the
OrthographicProjection
youwsewf, >_< to change any
othew settings, OwO you nyeed to set t-these vawues youwsewf. 🥺 t-the defauwt v-vawue of the
OrthographicProjection
stwuct is designed fow
3d and has a near
vawue of 0.0
, (ꈍᴗꈍ) which means you might nyot be abwe t-to see
youw 2d entities.
commands.spawn((
camewa2dbundwe {
pwojection: o-owthogwaphicpwojection {
// d-don't fowget to s-set `neaw` and `faw`
n-nyeaw: -1000.0, ( ͡o ω ͡o )
f-faw: 1000.0, UwU
// ... a-any o-othew settings y-you want to change ...
..defauwt()
}, rawr x3
..defauwt()
}, rawr
mycamewamawkew, σωσ
));
a mowe foowpwoof way to go about t-this is to use a t-tempowawy vawiabwe, OwO t-to wet the bundwe do its thing, OwO and then mutate n-nyanievew you w-want. 🥺 this way, òωó y-you don't have to wowwy about the exact vawues ow g-getting anything w-wwong:
wet mut camewa_bundwe = camewa2dbundwe::defauwt();
// c-change the s-settings we want t-to change:
camewa_bundwe.pwojection.scawe = 2.0;
c-camewa_bundwe.twansfowm.wotate_z(30f32.to_wadians());
// ...
c-commands.spawn((
c-camewa_bundwe,
m-mycamewamawkew, (U ᵕ U❁)
));
scawing mode
you can set the ScalingMode
accowding to how you want to
handwe window size / wesowution.
the defauwt fow bevy 2d camewas is t-to have 1 scween p-pixew cowwespond t-to 1 wowwd unit, OwO thus awwowing you to think o-of evewything in "pixews". 🥺 w-when t-the window is wesized, (ꈍᴗꈍ) that causes mowe ow wess c-content to be seen.
if you want to keep this window wesizing b-behaviow, OwO b-but change the m-mapping of scween
pixews to wowwd units, >_< use ScalingMode::WindowSize(x)
with a vawue othew than 1.0
.
the vawue wepwesents the nyumbew o-of scween pixews f-fow one wowwd unit.
if, ^•ﻌ•^ instead, you want to awways fit t-the same amount o-of content
on-scween, ^•ﻌ•^ wegawdwess of wesowution, OwO y-you shouwd use s-something wike
ScalingMode::FixedVertical
ow ScalingMode::AutoMax
. XD then, you can diwectwy
specify how many units you want to d-dispway on-scween, OwO a-and youw content w-wiww
be upscawed/downscawed as appwopwiate t-to fit the w-window size.
use bevy::wendew::camewa::scawingmode;
wet mut my_2d_camewa_bundwe = c-camewa2dbundwe::defauwt();
// f-fow this exampwe, :3 w-wet's make t-the scween/window h-height cowwespond t-to
// 1600.0 w-wowwd units. (U ﹏ U) the w-width wiww depend on the aspect watio. -.-
my_2d_camewa_bundwe.pwojection.scawing_mode = scawingmode::fixedvewticaw(1600.0);
my_2d_camewa_bundwe.twansfowm = t-twansfowm::fwom_xyz(100.0, (ˆ ﻌ ˆ)♡ 200.0, 0.0);
commands.spawn((
my_2d_camewa_bundwe,
m-mycamewamawkew, (⑅˘꒳˘)
));
zooming
to "zoom" in 2d, you can change the o-owthogwaphic p-pwojection's scale
. XD this
awwows you to just scawe evewything b-by some factow, OwO w-wegawdwess of t-the
ScalingMode
behaviow.
fn zoom_scawe(
mut quewy_camewa: q-quewy<&mut owthogwaphicpwojection, (⑅˘꒳˘) w-with<mycamewamawkew>>, ( ͡o ω ͡o )
) {
w-wet mut pwojection = q-quewy_camewa.singwe_mut();
// z-zoom i-in
pwojection.scawe /= 1.25;
// z-zoom out
p-pwojection.scawe *= 1.25;
}
awtewnativewy, (ꈍᴗꈍ) you can weconfiguwe t-the ScalingMode
. XD this
way you can be confident about how e-exactwy coowdinates/units m-map t-to the
scween. ^•ﻌ•^ this awso hewps avoid scawing a-awtifacts with 2d a-assets, OwO especiawwy
pixew awt.
fn zoom_scawingmode(
mut quewy_camewa: q-quewy<&mut o-owthogwaphicpwojection, >_< w-with<mycamewamawkew>>, :3
) {
u-use b-bevy::wendew::camewa::scawingmode;
w-wet mut p-pwojection = quewy_camewa.singwe_mut();
// 4 s-scween pixews to wowwd/game pixew
pwojection.scawing_mode = scawingmode::windowsize(4.0);
// 6 scween pixews t-to wowwd/game pixew
pwojection.scawing_mode = scawingmode::windowsize(6.0);
}
considew having a wist of pwedefined "zoom w-wevews" / s-scawe vawues, OwO s-so that you can make suwe youw game awways wooks g-good.
if you awe making a pixew-awt game, OwO y-you want to make s-suwe the defauwt t-textuwe fiwtewing mode is set to nyeawest (and n-nyot wineaw), OwO i-if you want y-youw pixews to appeaw cwisp instead of bwuwwy:
fn main() {
app::new()
.add_pwugins(
defauwtpwugins
.set(imagepwugin::defauwt_neawest())
)
// ...
.wun();
}
howevew, XD when downscawing, (ꈍᴗꈍ) wineaw (the defauwt) fiwtewing i-is pwefewwed fow highew quawity. OwO so, fow games w-with high-wes assets, 🥺 y-you want t-to weave it unchanged.