- Title: Rotating a view around an off-center position
- Description: How to make the rotated view follow an object that is not at the center.
- GM Version: GM 6/7
- Registered: yes
- File Type: .gm6
- File Size: 19KB
- File Link: view_rotate.gm6
Rotating a view isn’t hard; just set view_angle. But how can we keep the position of our object as the screen rotates?
To keep it at the center of the screen, we can simply set the view border to the half view size and make the view follow the object, then Game Maker takes the rest of care. However, if we need it to be off-center (say, at the bottom of the screen), we have to adjust the view position on our own.

Theory
First, we have to know how a view is displayed on the screen when it is rotated. Firstly, if the view is set to follow an object, Game Maker updates view_xview and view_yview as usual (without taking account of rotation.) Then, the view is rotated and rendered on the screen so that its center comes to the center of the view port.

If our object is not at the center of the view, then it is displayed at the position rotated by view_angle around the center of the view port. We can reverse-calculate this process to get the desired effect.
- Calculate the coordinates of the center of view port relative to the object.
- Map it onto the room’s coordinate system.
- Determine view_xview and view_yview so that the position comes at the view’s center.

Example
Suppose that we want to keep the object near the bottom of screen. First, we rotate the view in the opposite way of the object so that it looks as if the world rotates instead of the object. Then, we fix the view position to display it at the desired position.
view_angle[0] = -image_angle; adjust_view(0, x, y, view_wport[0]/2, view_hport[0]-sprite_height);Here, adjust_view is a script that takes care of the view position:
// This script adjusts the view position so that it rotates around the specified position. // argument0 = view number (0-7) // (argument1, argument2) = coordinates of the origin of rotation in the room // (argument3, argument4) = coordinates of the origion within viewport var vx, vy, xx, yy, t; // First, calculate the relative coordinates of the center of view port // (taking acccount of scaling.) vx = (view_wport[argument0]/2 - argument3) * view_wview[argument0]/view_wport[argument0]; vy = (view_hport[argument0]/2 - argument4) * view_hview[argument0]/view_hport[argument0]; // Rotate it around the origin in the opposite way to view. t = -view_angle[argument0]; xx = argument1 + lengthdir_x(vx, t) - lengthdir_y(vy, t); yy = argument2 + lengthdir_y(vx, t) + lengthdir_x(vy, t); // Then move the view so that the point comes at the center. view_xview[argument0] = xx - view_wview[argument0]/2; view_yview[argument0] = yy - view_hview[argument0]/2;
Search Tags
- GMCTUTORIAL
- GMCVIEWS
Edited by icuurd12b42, 14 December 2011 - 12:01 AM.











