• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code smooth camera like in gms

C

c3pu

Guest
hey guys ive been trying to use the new cameras to make a smooth camera like i had in gms in gms2 but i cant get anywhere

this is what it was in gms 1
view_xview += (obj_hero.x + (image_xscale *100)-(view_xview+(view_wview/2)))/16;
view_yview += (obj_hero.y-(view_yview+(view_hview/2)))/16;
ive tried multiple way to convert this to the new camera system but nothing seems to work
 
C

c3pu

Guest
i made some progress i finally understood the tut thats up on here for now the camera drags behind the player but i want it to move ahead. i just right now got this to work so ill start messing with it


//Get target view position and size. size is halved so the view will focus around its center
var vpos_x = camera_get_view_x(view_camera[0]);
var vpos_y = camera_get_view_y(view_camera[0]);
var vpos_w = camera_get_view_width(view_camera[0]) * 0.5;
var vpos_h = camera_get_view_height(view_camera[0]) * 0.5;

//The interpolation rate
var rate = 0.2;

//Interpolate the view position to the new, relative position.
var new_x = lerp(vpos_x, obj_hero.x - vpos_w, rate);
var new_y = lerp(vpos_y, obj_hero.y - vpos_h, rate);

//Update the view position
camera_set_view_pos(view_camera[0], new_x, new_y);
 
C

c3pu

Guest
Ah I see. I just use lengthdir_x and y to set the camera location ahead of the player,
where would i add that?

right now i added this and it works but when i change direction its really abrupt not smooth
+ (image_xscale *100)

here is what it looks like

var new_x = lerp(vpos_x, obj_hero.x + (image_xscale *100) - vpos_w, rate);
 

MilesThatch

Member
Oh well that's actually different. It's abrupt because you set the value abrupt. Lerp function just returns a position between two values It doesn't do it gradually.

What I like to do is to make a camera object that follows whatever target_x target_y. Be it a player or equal distance between two platers. And to make the movement smooth, I use:

speed = point_distance(x,y, target_x, target_y)* 0.1;
direction = point_direction(x,y, target_x, target_y);

Yes those a built in variables but give it a try.

The 0.1 value essentially says that the speed of the camera object is 0.1 of it's distance so the target in question. It will also ensure a silky smooth animation curve for the camera movement.

So what's where I'd use lengthdir_x and y to set the camera to be ahead of the player. Really handy and quick solution
 
C

c3pu

Guest
Oh well that's actually different. It's abrupt because you set the value abrupt. Lerp function just returns a position between two values It doesn't do it gradually.

What I like to do is to make a camera object that follows whatever target_x target_y. Be it a player or equal distance between two platers. And to make the movement smooth, I use:

speed = point_distance(x,y, target_x, target_y)* 0.1;
direction = point_direction(x,y, target_x, target_y);

Yes those a built in variables but give it a try.

The 0.1 value essentially says that the speed of the camera object is 0.1 of it's distance so the target in question. It will also ensure a silky smooth animation curve for the camera movement.

So what's where I'd use lengthdir_x and y to set the camera to be ahead of the player. Really handy and quick solution
hey thank ill check this tomorrow probably
 
C

c3pu

Guest
Oh well that's actually different. It's abrupt because you set the value abrupt. Lerp function just returns a position between two values It doesn't do it gradually.

What I like to do is to make a camera object that follows whatever target_x target_y. Be it a player or equal distance between two platers. And to make the movement smooth, I use:

speed = point_distance(x,y, target_x, target_y)* 0.1;
direction = point_direction(x,y, target_x, target_y);

Yes those a built in variables but give it a try.

The 0.1 value essentially says that the speed of the camera object is 0.1 of it's distance so the target in question. It will also ensure a silky smooth animation curve for the camera movement.

So what's where I'd use lengthdir_x and y to set the camera to be ahead of the player. Really handy and quick solution
sry to bother you again but i cant seem to figure this out lengthdir_x wants a direction in documentation they use image_angle but my image angle is fixed since my game is a platformer(sry forgot to mention that)
 
C

c3pu

Guest
Oh well that's actually different. It's abrupt because you set the value abrupt. Lerp function just returns a position between two values It doesn't do it gradually.

What I like to do is to make a camera object that follows whatever target_x target_y. Be it a player or equal distance between two platers. And to make the movement smooth, I use:

speed = point_distance(x,y, target_x, target_y)* 0.1;
direction = point_direction(x,y, target_x, target_y);

Yes those a built in variables but give it a try.

The 0.1 value essentially says that the speed of the camera object is 0.1 of it's distance so the target in question. It will also ensure a silky smooth animation curve for the camera movement.

So what's where I'd use lengthdir_x and y to set the camera to be ahead of the player. Really handy and quick solution

made it work like this
direction = point_direction(x-(100 * obj_hero.image_xscale),y, obj_hero.x, obj_hero.y);
speed = point_distance(x-(100 * obj_hero.image_xscale),y, obj_hero.x + (obj_hero.hsp * 9), obj_hero.y + (obj_hero.vsp * 9))* 0.1;
 

MilesThatch

Member
made it work like this
direction = point_direction(x-(100 * obj_hero.image_xscale),y, obj_hero.x, obj_hero.y);
speed = point_distance(x-(100 * obj_hero.image_xscale),y, obj_hero.x + (obj_hero.hsp * 9), obj_hero.y + (obj_hero.vsp * 9))* 0.1;
If you got it to work your own way then yeah congrats.

You don't HAVE to supply an image_angle, that's just an example code. You can use your own value or a variable...

lengthdir_x and lengthdir_y return a new x and y positions based on the information you supply. It needs and angle for the direction in which to calculate the second coordinate and the length from the original point.

Do not forget to add the returned value to the players original position because it will return relative position not absolute. And it's supposed to be relative from the players x and y so
new_point_x = player.x + lengthdir_x("length from the players x", "direction in which the length will be protruded")

If you got it too work you way then it's fine, but there is too much code to essentially achieve the same thing. lengthdir is what you should use, simply because that's what it's designed to do.
 
P

PhoenixBolt Studios

Guest
I use this code for a smooth camera in GMS 2.0
this works perfect for me.

in 'player' object

///CREATE EVENT

camx = camera_get_view_x(view_camera[0]);
camy = camera_get_view_y(view_camera[0]);​

///STEP EVENT

camh = camera_get_view_height(view_camera[0]);
camw = camera_get_view_width(view_camera[0]);

camx += ((x - (camw/2) - camx) * 0.08)
camy += ((y - (camh/2) - camy) * 0.08)

camera_set_view_pos(view_camera[0], camx, camy);
 
Top