Camera | SoloLearn: Learn to code for FREE!

0

Camera

How to make the camera follow a character in a game in pure JavaScript. In general, how to write a camera in js, you can throw examples

4/10/2021 11:03:35 AM

Alexander Sokolov

13 Answers

New Answer

+3

I think you just have to translate the square positional vector from the world space to camera space by subtracting their positional vector from the camera's positional vector. Then to make the camera follow the square, you have to translate the camera's positional vector to the middle of it's position. This is just an example for a single positive X-axis. You'll have to use the same idea for the Y-axis https://code.sololearn.com/WaIP6fnhWNP2/?ref=app Also, i provided a camera class in my game engine which does more than this including clipping, clamping and gourad lightening. you just need to call the camera.setView(world, pos, ...arg) method you may need to checkout an example https://code.sololearn.com/Wam3R3Hk3s6c/?ref=app

+1

ŚĄŃ [Exams] I have my own code in which I tried to make a camera on the line 21 to 60. Can you help me make the camera follow the movement of the red cube https://code.sololearn.com/W0vuqFS021od/?ref=app

0

Inside ur main loop: ctx.resetTransform(); ctx.translate(cvs.width/2-red.x, cvs.height/2-red.y); Translate function translates the origin of the canvas. U can think of translating as adding the specified values to every coordinates u pass to any ctx function. Keep in mind the transformations stack so u need to reset the transform as above. Here the required translation vector is the vector that points from red's position to the center of the screen.

-1

ŚĄŃ [Exams] sir

-1

ŚĄŃ [Exams] u here?

-1

ŚĄŃ [Exams] very hard code, sir

-1

ŚĄŃ [Exams] can you explain to me what's what in your code sir

-1

ŚĄŃ [Exams] How should I do it

-1

Forgive me sir, I will not call you sir anymore.

-1

please explain to me how I move the background using my code example😭 https://code.sololearn.com/W0vuqFS021od/?ref=app

-1

ŚĄŃ [Exams] student, Am I doing everything right? p.s. I made the background black https://code.sololearn.com/W0vuqFS021od/?ref=app

-1

ŚĄŃ [Exams] student, did I do everything right? look at my code.

-1

ŚĄŃ [Exams] school student, Why do I need your example? you have already sent enough examples. I just asked if everything worked out correctly in my code, and if not, why?