GMS 2 Camera and Physics Conflict?

EricPB

Member
Here is a problem, I am trying to make a physics based game for GMS2. As such I enabled physics for the one test room I have and the appropriate objects, and try to set up the camera.

The problem comes that when I have the rooms boxed ticked off as using physics, the camera doesnt display the room properly- i only see the background color. Either because it is so far off the rooms center or the room doesnt display properly. However, when I untick the using physics box for the room, the camera works fine but none of my physics objects work, obviously.

What the heckie is going on?

I only have two objects in my game, the player and the walls - its top down. The use viewports is ticked, both objects are ticked off as physics objects.

Create code for camera
Code:
/// @description Set Up Camera
cam = view_camera[0];
follow = obj_player;
view_w_half = camera_get_view_width(cam) * 0.5;
view_h_half = camera_get_view_height(cam) * 0.5;
xTo = xstart;
yTo = ystart;
Step Code for camera
Code:
//Update Destionation

if (instance_exists(follow))
{
    xTo = follow.x;
    yTo = follow.y;
}

//Update Object Position
x += (xTo - x) / 25;
y += (yTo - y) / 25;

//Update Camera View
camera_set_view_pos(cam,x-view_w_half,y-view_h_half);
UPDATE

The instance does not have an associated physics representation
at gml_Object_obj_player_Step_0 (line 16) - physics_apply_local_force (0,32,-10,0);
#######################################################################

if i have the physics turned off to make the camera work, this is the error i get for the physics object
 
Last edited:

EricPB

Member
is the camera object a physics object?
if it is you then need to use phy_position_x and y to move it.
the camera I specifically have set up is not technically a physics object, there is an updated issue presented now but maybe your suggestion can help improve my system. Where would i substitute a piece of my code above for phy_position_x/y ?
 

Jakylgamer

Member
Code:
//Update Object Position
x += (xTo - x) / 25;
y += (yTo - y) / 25;

//Update Camera View
camera_set_view_pos(cam,x-view_w_half,y-view_h_half);
all of the x and y values in that snip of code

also might not hurt to change the follow.x to follow.phy_position_x and y since the player is a physics object
Code:
if (instance_exists(follow))
{
   xTo = follow.x;
   yTo = follow.y;
}
 

EricPB

Member
I changed what you said, but it returned an error message. The line it appears on is line 16 which is this : phy_position_x += (xTo - phy_position_x) / 25;


Variable camera.phy_position_x(55, -2147483648) not set before reading it.
at gml_Object_camera_Step_0 (line 16) - phy_position_x += (xTo - phy_position_x) / 25;

Code:
if (instance_exists(follow))
{
    xTo = follow.phy_position_x;
    yTo = follow.phy_position_y;
}

//Update Object Position
phy_position_x += (xTo - phy_position_x) / 25;
phy_position_y += (yTo - phy_position_y) / 25;

//Update Camera View
camera_set_view_pos(cam,phy_position_x-view_w_half,phy_position_y-view_h_half);
 

Jakylgamer

Member
you get that error because physics are not running on that object.
i have found that if you use physics_world_create() you have to set up the objects physics properties in the create event rather than setting the "uses physics" tickbox
example:
set up physics like this for each object that needs physics enabled (im using a 16x16 sprite as an example)
Code:
var fix = physics_fixture_create();
//get offsets if sprites are not centered (need to be negative values)
var xo = -8;
var yo = -8;
//get width / height of current sprite
var sw = sprite_get_width(sprite_index) / 2;
var sh = sprite_get_height(sprite_index) / 2;
//setup physics properties
physics_fixture_set_box_shape(fix, sw,sh);
physics_fixture_set_density(fix, 1.0);
my_fix = physics_fixture_bind_ext(fix, id , xo,yo);
physics_fixture_delete(fix);
and this way the camera doesnt need to be a physics object to work
 
Top