![]() ![]() Offset += ((viewport_size * 0.5) - mouse_position) * (zoom-previous_zoom) Here's the same code which was posted, except readable by humans: extends Camera2Dįunc zoom_camera(zoom_factor, mouse_position): Var z1 = z0 * zoom_change # next zoom valueīest practice is to write new code by yourself - you will remember it better that way - so try avoid copy-pasting answered by Bartosz ( 1,022 points) Var v0 = get_viewport().size # vieport size Var c0 = global_position # camera position Zoom_at_point(1/zoom_step,mouse_position) If event.button_index = BUTTON_WHEEL_UP:Įlse : if event.button_index = BUTTON_WHEEL_DOWN: If event.is_pressed() and not event.is_echo(): That is new camera position that we were searching for.Īnd here some code to attach to camera script: var zoom_step = 1.1 Replacing M1 and M2 with equations (m - 0.5 * s)*z0 + C0 = (m - 0.5 * s)*z1 + C1Īfter transforming(skipping some steps) we will get this: C1 = C0 + (-0.5*s + m)*(z0 - z1) We know that global mouse position and mouse position on screen before and after zooming will be same M0 = M1 Global mouse positions before and after zooming will be M0 = (m - 0.5 * s)*z0 + C0 That is everything need to compute global mouse position M = (m - 0.5 * s)*z + C Screen can be zoomed so we need to apply that to screen size and mouse position on screen M ~ (m - 0.5 * s)*zĮverything is relative to camera position M ~ (m - 0.5 * s)*z + C We set camera anchor to drag center so its reference point is in center of the screen and mouse position on screen is relative to upper left corner which is half screen size to upper left direction M ~ (m - 0.5 * s) ![]() Mouse global position depends on position on screen M ~ m Mouse positions looks like most appropriate points of building an equation to compute new position of camera. * Invariant: screen size, mouse global position, mouse position on screen To tackle this problem first we must get our facts strait. applying reversed translation - returning camera to its appropriate positionīecause Zooming part is doing for us by Godot as a first transformation we need to combine 1st and 3rd step together and do camera positioning ourselves and it will involves math.zooming by desire amount - actual zooming.translating to zooming point - to set zoom center.If we would do manually zoom-at-point transformation we could split it into 3 parts: Try creating simple scene and make zooming at center work, then after you resolve that issue read next part how zoom at specific point. Second it looks like either some script modifies cameras anchor_mode or your camera isn't active-one after all. First using only zoom property to zoom at point will not be sufficient(read below).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |