Jump to content

GetMousePosition().z ( Zoom ) Move Camera.


Yue
 Share

Recommended Posts

I have a problem, I wish that when moving the wheel of the mouse the camera gets closer or further away from the character. 

This I want to do with the mouse wheel, and is a kind of zoom, but I can not set things to work. 

I welcome any possible suggestions, and if possible a simple script code. 

	zoomCamara = ventana:GetMousePosition().z /500.0

	--zoomCamara ~= zoomCamara then
	
	
	-- Zoom / Se mueve el pivote de la cámara.
	if self.pivoteCamara:GetPosition(false).z < -2 then
		
		self.pivoteCamara:Move(0,0,zoomCamara)
	end

 

Mars.jpg.89ab63a64eebc1f5ada0ab82b66a1f8c.jpg

 

 

Link to comment
Share on other sites

This is my RTS project's mouse zoom code. I hope this c++ code can give you idea.

float cameraDistance = 15.0f;
float smoothness = 15.0f;
float zoomRangeMin = 8.0f;
float zoomRangeMax = 20.0f;;
Vec3 playerCameraPos;
Vec3 playerCameraNewPos;
float scrollSpeedFPS;
float mouseWheel;

int getMouseWheelDirection() {
	int wheelReturn = 0;
	float mouseWheelPos = Window::GetCurrent()->GetMousePosition().z;

	if (mouseWheel != mouseWheelPos) {
		if (mouseWheelPos < mouseWheel) {
			wheelReturn = 2; //Wheel Down
		}
		else if (mouseWheelPos > mouseWheel) {
			wheelReturn = 1; //Wheel Up
		}
		mouseWheel = mouseWheelPos;
	}
	return wheelReturn;
}

void mouseMovement()
{
    playerCameraPos = playerCamera->GetPosition();
    playerCameraNewPos = playerCameraPos;
    scrollSpeedFPS = scrollSpeed * Time::GetSpeed();
    
    if (playerCameraNewPos.y != cameraDistance) {
		playerCameraNewPos.y = Math::Curve(cameraDistance, playerCameraNewPos.y, smoothness / Time::GetSpeed());
	}

	int mouseWheelDirection = getMouseWheelDirection();
	if (mouseWheelDirection) {
		if (mouseWheelDirection == 1) {
			if (cameraDistance < zoomRangeMax) {
				cameraDistance = cameraDistance + 2;
			}		
		}
		else if(mouseWheelDirection == 2){
			if (cameraDistance > zoomRangeMin) {
				cameraDistance = cameraDistance - 2;		
			}	
		}
	}

	playerCamera->SetPosition(playerCameraNewPos);
}

 

  • Confused 1
Link to comment
Share on other sites

	
	if ventana:KeyHit(Key.F1) then
		zoom = 4
	elseif ventana:KeyHit(Key.F2) then
		zoom = 2 
	elseif ventana:KeyHit(Key.F3) then
		zoom = 0
	end


	

	self.PivoteCamara:SetPosition(0,0,zoom)
	self.PivoteCamara:Move(0,0,-5)

It's the easiest thing I can do. I guess it's not that bad.

Mars.jpg.89ab63a64eebc1f5ada0ab82b66a1f8c.jpg

 

 

Link to comment
Share on other sites

  • 3 weeks later...

I have more simple one, only for detecting mouse scroll up/down, not for calculating HOW much scroll

in Main.lua, add this at the bottom of main while loop (right above "end")

image.png.493a001fabaa6eac5116159ed245541b.png

lastMouseZ = window:GetMousePosition().z

and add this as a Global function

MouseScrollDirection = function()
  if window:GetMousePosition().z < lastMouseZ then return 1 --scolled up
  elseif window:GetMousePosition().z > lastMouseZ then return 2 --scrolled down
  elseif window:GetMousePosition().z == lastMouseZ then return 0 end --no scroll
end

Use this function in any Script:UpdateWorld() function to detect mouse scroll

function Script:UpdateWorld()
  if MouseScrollDirection == 1 then
    --do something with scroll up
    --example: camera:Move(0,0,1)
  end
  if MouseScrollDirection == 2 then
    --do something with scroll down
    --example: camera:Move(0,0,-1)
  end
end

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...