Jump to content


Photo
- - - - -

Rotating A View Around An Off-center Position


  • Please log in to reply
7 replies to this topic

#1 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 27 February 2010 - 05:33 PM

  • 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
Additional Info
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.
Posted Image

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.
Posted Image

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.
Posted Image

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.

  • 1

#2 Dylan93

Dylan93

    GMC Member

  • GMC Member
  • 866 posts
  • Version:GM8

Posted 28 February 2010 - 09:23 PM

Its a very good example for people that want to make a game based on rotations, but as the title says its not a Rotating view around a center off position its a gravity rotation :)
  • 0

#3 Robert3DG+

Robert3DG+

    VR Games

  • GMC Member
  • 1845 posts
  • Version:GM:Studio

Posted 28 February 2010 - 10:15 PM

Really nice example and explanation thread torigara!

The effect was really cool and worked perfectly.
  • 0

#4 marumariball

marumariball

    Studio Fusion

  • GMC Member
  • 121 posts

Posted 01 March 2010 - 12:16 AM

Nice! This looks like just what i needed.
  • 0

#5 DJking

DJking

    GMC Member

  • New Member
  • 28 posts

Posted 27 May 2010 - 01:31 PM

Nice example! its helping me loads!

Edited by DJking, 27 May 2010 - 01:32 PM.

  • 0

#6 S.Flo

S.Flo

    GMC Member

  • New Member
  • 30 posts

Posted 05 July 2010 - 03:39 AM

Thank you so much, this is an amazing thread, with nice visual aids to boot.
  • 0

#7 apocalypse_knight

apocalypse_knight

    GMC Member

  • New Member
  • 3 posts

Posted 11 December 2011 - 06:32 AM

Thanks a lot for the great tutorial. Found it really helpful.
  • 0

#8 Maple

Maple

    GMC Member

  • GMC Member
  • 3 posts

Posted 13 July 2012 - 06:10 PM

Hmm. This is really interesting / almost what I need.

What would you do, if say, you didn't want the character stuck in one spot on the screen, but wanted the camera to trail smoothly behind the character too?

To be more specific, in the game I am developing, gravity isn't constantly changing; it rotates 90 degrees back or forth when you hit a switch, and stays at that angle until you hit another switch. I'm looking for a solution where I can move the camera around however I like, free from the character, but when a switch is flipped, as the rotation is happening, the character will stay in the same spot on the screen (or at a spot that flows with the camera movement, and won't be shifted to one of the other 3 sides of the camera (when I say 'camera', I mean the center of the view)), afterwhich the camera will continue to flow smoothly. Or... perhaps easier to understand, I just want to be able to move the camera however I like, without the character's place on the screen getting altered by the view angle.

Any help is much appreciated.
  • 0




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users