Moving object inside another Moving Object

H

Huy

Guest
2D game. I have a player moving inside a moving ship.

How do i get the player to stay on the ship when the ship is moving around, and be able to move around while the ship is moving. Also turn the player relative to how the ship is turning?

Thanks!
 
The best solution would depend on the complexity of your game.

It might be that the easiest way to draw the ship would be to pretend it doesn't move, and instead move the camera instead. But that would depend on whether things inside of the ship can interact with things outside of the ship. If not, then it would be an idea solution.

So the idea is to position the camera according to the location & orientation of the ship. And then to draw everything on the ship as if the ship was at the origin (0,0) and at zero rotation.

Code:
    var _x = cam_x - x;
    var _y = cam_y - y;
    var _c = dcos(angle_of_ship);
    var _s = dsin(angle_of_ship);
    d3d_set_projection_ortho(
        _x * _c - _y * _s - width_of_view/2 + offset_x,
        _x * _s + _y * _c - height_of_view/2 + offset_y,
        width_of_view,
        height_of_view,
        cam_angle + angle_of_ship
    );
    //draw everything on ship here.
    //then reset to normal view, if necessary.
cam_x,cam_y is where the camera is located (i.e. center of view).
cam_angle is equivalent to view_angle.
offset_x,offset_y should be zero unless you are drawing something like a background or surface, and you want it to rotate around something other than its upper-left corner. Example: drawing a surface 128x64 pixels in size, and want it to rotate around its center, offset_x/y would then be 64 & 32.
 
Last edited:
  • Like
Reactions: Huy
H

Huy

Guest
The best solution would depend on the complexity of your game.

It might be that the easiest way to draw the ship would be to pretend it doesn't move, and instead move the camera instead. But that would depend on whether things inside of the ship can interact with things outside of the ship. If not, then it would be an idea solution.

So the idea is to position the camera according to the location & orientation of the ship. And then to draw everything on the ship as if the ship was at the origin (0,0) and at zero rotation.

Code:
    var _x = cam_x - x;
    var _y = cam_y - y;
    var _c = dcos(angle_of_ship);
    var _s = dsin(angle_of_ship);
    d3d_set_projection_ortho(
        _x * _c - _y * _s - width_of_view/2 + offset_x,
        _x * _s + _y * _c - height_of_view/2 + offset_y,
        width_of_view,
        height_of_view,
        cam_angle + angle_of_ship
    );
    //draw everything on ship here.
    //then reset to normal view, if necessary.
cam_x,cam_y is where the camera is located (i.e. center of view).
cam_angle is equivalent to view_angle.
offset_x,offset_y should be zero unless you are drawing something like a background or surface, and you want it to rotate around something other than its upper-left corner. Example: drawing a surface 128x64 pixels in size, and want it to rotate around its center, offset_x/y would then be 64 & 32.

Sounds like a great plan. But doesn't seem like d3d_set_projection_ortho is in the code?
 
D

DarthTenebris

Guest
Sorry if this isn't helpful, but from the OP it looks like you're trying to make a 2D game.

If so, an idea I have is, involve a bit of the physics I learn in school :p
Here's my idea:
Any object that is on a moving object is also moving. For example, an apple on a truck moving at 10m/s is also moving at 10m/s.
So when you move on a moving object you're adding your speed to the object you're on's speed. For an example, a person moving at 5m/s observed by another person on a plane travelling at say 100m/s is actually moving at 105m/s. This assumes the plane and person move in the same direction. If the objects are moving in opposite directions it'll be 95m/s. For the sake of simplicity I won't discuss movements at an angle here. Also due to me not quite fully understanding it :p

To GML:
Player is on a moving platform. Platform moves at 8px/frame. Player has a speed of 5px/frame.
If the player moves in the same direction as the platform, the speed of the player will be 13px/frame. Otherwise, it will ve 3px/frame.

Same thing applies to vertical movement (y axis).

So basically check for the direction the player is moving in, compare it with the direction the platform is moving in, then apply the appropriate calculations.

Unfortunately I have no idea about rotation... :oops:

Hope I helped :)
 

The-any-Key

Member
Use a sprite to draw the ship.
Add invisible collision boxes over the ship where collision should be.
Link each collision box with the origin of the ship with vector rotation.
Rotate the ship origin and calc each collision box new angle and position.
 
  • Like
Reactions: Huy
H

Huy

Guest
Use a sprite to draw the ship.
Add invisible collision boxes over the ship where collision should be.
Link each collision box with the origin of the ship with vector rotation.
Rotate the ship origin and calc each collision box new angle and position.
Right now, i'm checking for collision where the player is moving. If the player will not collide with the ship sprite when it moves, then it will not move there. This way, i can just use one ship sprite to keep the player inside. This works fairly well but the collision box for the ship doesn't rotate and change when the ship rotates.
 
If the character only interacts with the ship, it would be far better to have the inside of the ship stationary and at zero rotation.

And I think the corresponding function in gamemaker for positioning the "view" would be:

camera_set_view_pos()

and

camera_set_view_angle()
 
Top