0 Favourites

# How do I set position of sprite based on other sprites?

• 7 posts
• Hey everybody,

I’m planning to create couch co-op multiplayer game. Game won’t be split-screen. instead, all characters will be on screen at same time, like in New Super Mario Bros. Most important aspect (for now) is the camera. I want that camera follows all the characters on screen. So camera would always stay in the middle of characters, regardless of how many characters are on screen. With 2 characters it’s easy. Camera will be in between of 2 characters. With 3 characters, it will become more complicated. I made a visualisation of what I mean by that.

pic1

h*t*t*p*s://www.dropbox.com/s/pokxit8uixytdpo/camera_visualization1.tiff?dl=0

pic2

h*t*t*p*s://www.dropbox.com/s/oav8tsc0jo4s5eq/camera_visualization2.tiff?dl=0

(remove those *** from h*t*t*p*s and you are good to go)

So I want that camera will be at the intersection of those orange lines all the time and that’s the problem I’m facing right now. I think I need to use cos/sin or acos/asin (or some other math calculation) to calculate the correct position but math isn’t my piece of cake.

I don’t want that somebody will do all the work for me. I don’t learn anything if answers are handed to me. I’m asking that if someone can point me to the right direction so I can continue from there. I will appreciate all kind of help.

-M-

• Ok the idea here is to apply a formula to calculate the center of gravity. Or the centroid.

Between two points, as you said it's quite easy. Between 3 and 4 points it starts to get weird, and if you add 5 or more players... oh boy.

Here, take a look: https://en.wikipedia.org/wiki/Centroid

• My brain exploded O_o

Calculations are way more complicated than I though and are beyond my skill level. If I limit number of players to 3 and calculate the centroid between those 3, THAT could be manageable to me. OR I just figure out something else. A bit more simpler, maybe? :)

Maybe if I pick 2 characters which are most apart from each other and place camera at middle point between those 2. Or something.

Thank you RayKi for your help :)

-M-

• So I went for a walk and came back with a better solution, way less complicated than calculating the cg.

You see, if you calculate the centroid every time a character moves the screen will adjust to the position in between them and that will just screw up the whole game because the screen will be shaking like crazy all the time.

What i think you can do is :

1 - set your game to be played like Kirby, the camera follows a main player and the other players just have to deal with it.

2 - Set your camera to move depending on the furthest points. Compare the Xs and Ys of all characters take the smallest and biggest values and set the camera in the middle of those 4 points.

3 - Set the players to follow the camera, just like those beat them up games where the camera moves independently. The camera shows a challenge, when the players are done with the challenge the camera moves to another area and the players have to keep up with the camera until it gets into a new area where it freezes again.

4 - Set a camera fixed and work with boundaries. If ALL the players are to the right of the screen the screen will move to the right, if all of them are to the left, the screen follows. If the players are scattered all over the place, fix the camera and let them come to an agreement before proceeding.

Hope that helps to give you some ideas o/

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• You can give more sprites (bundle them in a family if needed) the 'Scroll to' behaviour. The camera will centre on them.

• Just average the positions.

With two players:

camera.x = (player1.x + player2.x)/2

camera.y = (player1.y + player2.y)/2

With three players:

camera.x = (player1.x + player2.x + player3.x)/3

camera.y = (player1.y + player2.y + player3.y)/3

With four players:

camera.x = (player1.x + player2.x + player3.x + player4.x)/4

camera.y = (player1.y + player2.y + player3.y + player4.y)/4

...and so one. hopefully you see the pattern

• Thank you everybody.

RayKi Yes, really good options for different camera behaviours.

99Instances2Go this was a new thing to me! I have always thought that layout should contain only one camera object and if there is more than one, they will interrupt each others. I was so wrong.

R0J0hound I can see the pattern ^^

I made some testings. There isn’t that much of a difference between methods, so I think it’s more like what I want to use

If anyone is interested to see my results, here is the link: h*t*t*p*s://www.dropbox.com/s/i6oqciyjoufart5/hitbox_scroll-to_%20distance_layout_scale.capx?dl=0

I can’t take full credits. I used 2 examples by Yann and combined them together to gain desired result.

example 1) autoFraming: h*t*t*p*s://www.scirra.com/forum/between-sprites-distance-zoom_t69772?&hilit=autoframing

example 2) check distance between 2 instances:

Key binding credits goes to Kyatric. I modified his code slightly.