13 AnswersNew Answer
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
ŚĄŃ [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
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.
ŚĄŃ [Exams] sir
ŚĄŃ [Exams] u here?
ŚĄŃ [Exams] very hard code, sir
ŚĄŃ [Exams] can you explain to me what's what in your code sir
ŚĄŃ [Exams] How should I do it
Forgive me sir, I will not call you sir anymore.
please explain to me how I move the background using my code example😭 https://code.sololearn.com/W0vuqFS021od/?ref=app
ŚĄŃ [Exams] student, Am I doing everything right? p.s. I made the background black https://code.sololearn.com/W0vuqFS021od/?ref=app
ŚĄŃ [Exams] student, did I do everything right? look at my code.
ŚĄŃ [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?