• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How to Rotate View Around Non Center (Math Problem)

R

Robert

Guest
Hello, I am having a hard time figuring out how to do this, and I feel stupid because I think its probably a super easy thing and I just can't wrap my head around it. Let's say I have a view around the size of a portrait phone and I want the view to be centered around the player but also slightly vertically offset, see the image below for an example of what I mean. Now, I want the player to always be facing up so when the player turns the view needs to turn with them.

If the player was perfectly centered it wouldn't be a problem, but the only way to rotate the view is with view_angle and it's origin is fixed at the center, so what is the formula/function I would use to calculate the view angle and the correct x and y? I thought the lengthdir functions would be what I need but I couldn't get them to work either. Any ideas?

 

TehCupcakes

Member
Can you just have an invisible camera object that is always in front of the player and the view centers on it? You would move the camera object based on the player's angle using lengthdir functions, as you guessed:

Code:
x = obj_player.x + lengthdir_x(300, obj_player.image_angle);
y = obj_player.y + lengthdir_y(300, obj_player.image_angle);
In essence, the camera "orbits" around the player as he turns. Adjust the value so that it is perfectly centered within the dimensions of the view, and then set up the view to follow the camera object.
 

Yal

🐧 *penguin noises*
GMC Elder
AFAIK the view is actually rotated around the top left corner, so you'd figure out where to position that post-rotation. You ideally want the view's top-left corner to be at something like
Code:
view_xview = obj_player.x + lengthdir_x(view_wview*0.50,ang + view_angle)
view_yview = obj_player.y + lengthdir_y(view_hview*0.75,ang + view_angle)
Where ang is a constant value that depends on your view aspect ratio - it's between 90 and 180 degrees (straight up and straight left, respectively), and since your view is about 3 times as tall as wide, I'd go for a value along the lines of 115 degrees (and then tweak it a bit if it feels off-center).
 

jo-thijs

Member
Got eaten to it by 2 people...
Anyway, @Yal, that's not true, it actually IS rotated around the center of the view.

Here's the message I wrote:

If I remember it correctly, view_angle is the minus the amount of degrees the view should be rotated around the point (view_xview + view_wview * 0.5, view_yview + view_hview * 0.5).
With that, the code should be:
Code:
var voffset = 90;
var facingDir = image_angle;
var mx = x + lengthdir_x(voffset, facingDir);
var my = y + lengthdir_y(voffset, facingDir);

view_xview = mx - view_wview * 0.5;
view_yview = my - view_hview * 0.5;
view_angle = 90 - facingDir;
This code assumes your character's sprite is facing left in the editor.
Otherwise, add 90 to facingDir.
 
To position a rotated view, where the view's center should mainain a position relative to some object:

Code:
  //Im rotating the view along with the image angle of the thing the view is following
  //I'm subtracting 90 so that object is pointing toward the top of the screen
  var _an = image_angle - 90;
  var _c = dcos(_an);
  var _s = dsin(_an);
  view_angle[0] = -_an;
  view_xview[0] = x + _c * other.view_x_offset * zoom + _s * other.view_y_offset * zoom - view_wview[0] / 2;
  view_yview[0] = y + _c * other.view_y_offset * zoom - _s * other.view_x_offset * zoom - view_hview[0] / 2;
"view_x_offset" and "view_y_offset" are the position of the center of the view relative to the thing that the view is following in room pixels but using the orientation of the view. In other words, if you want the object that the view is following to appear 300 room pixels beneath the center of the view (as it apperas on the screen), then your offset values would be this:

view_x_offset = 0;
view_y_offset = -300;

"zoom" is the scale of the view (height of view / height of port) (if you are using a scaled view) If the scale of your view is 1, then you don't need to include zoom in this calculation.
 
R

Robert

Guest
Wow thanks for much for the great replies. Im not able to get into gamemaker at the moment but I will try you guys code here in a couple hours... yay thanks so much, I was getting rather annoyed last night.
 

Yal

🐧 *penguin noises*
GMC Elder
Anyway, @Yal, that's not true, it actually IS rotated around the center of the view.
I should read the update notes more often, I guess. Well, that means it's suddenly a lot easier to use, I guess.

In that case, let me revise my statement: you'd use an angle of 90 all the time to get the effect you want.
 
Top