How to lock an main actor to the most nearest actor

VyseVyse Member Posts: 4
edited December 2012 in Working with GS (Mac)
Sorry to interrupt but I need some serious help. I'm creating a game, and I need to know how to force an actor to change position with another actor when colliding with it. Also, how to make the main actor to choose the nearest actor to change position to.

Like for example: If A-Actor is colliding with B-Actor more than C-Actor, then how do I make A-Actor change its position to lay on top of B-Actor? The A-Actor is colliding with both B and C Actor, but it is colliding more with B-Actor, so I want the A-Actor to change its position to where the B-Actor is.

Best Answer

  • tatiangtatiang Posts: 11,949
    edited December 2012 Accepted Answer
    Here's a demo I made that allows actors to swap positions when clicked. You'd have to change the rule conditions to match your example above.

    As far as collisions go, you'll need to measure the distance between actors using the Magnitude function. Then you can work out some rules for when an actor's distance is greater/than less than another actor's distance.

    The key with anything complex like this is to write out an algorithm in English before you start to code it. If you can do that, post your algorithm if you're still having trouble coding it and I'll help. If you can't write an algorithm, it means you're not ready to start coding.

    And by algorithm, I mean a more specific example. There's no such thing in GameSalad as "colliding more." Things either collide or they don't. But using magnitude, you can work with distance. So you might write "If A's distance to B is less than A's distance to C, then change A's position to B's position."

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

Answers

  • VyseVyse Member Posts: 4
    edited December 2012
    Thank you so much for helping me @tatiang. I figured out how to do the magnitude for the distance fine :) This works fine if it's choosing between two actors.

    I'm creating a game that is similar to Tetris, and it's about 40 actors that needs to connect with the main actors (main actors are shapes). I just want to make it where that the shapes will fall down in one out of the 40 actors in perfect position when someone drag and drop the shapes.

    Like for example: There are 3 actors going across (one actor is in position 10, second actor in position 20, third actor in position 30) and when someone drags a shape down and lands it inside the first actor, then the shape should set its position to 10. Only problem is that sometimes the shape will collide with the first and second actor. If someone drops the shape in the first actor more than the second actor, then the shape should go to position 10 to be with the first actor.

    All I want to figure is how to make the shape to lock/line-up into a perfect position, in one of the 40 actors, when someone drag&drop the shape.

    I'm sorry to keep asking questions, and once again, thank you so much for helping me @tatiang.
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    Okay, I think I get it. If not, you may have to post a screenshot of your scene so I can see the actor placements. It sounds like when a piece falls and lands on another piece (I'm thinking classic Tetris here and ignoring your 40 pieces for the moment), it should snap into place. You can do this by creating a rule for when a piece collides with another piece (AND has not yet landed) and then changing/constraining the falling piece's position to a grid:

    image

    That assumes that your grid size is 100 pixels. Basically, you divide the actor's X position by the grid to find out which grid square it is closest to -- take the ceiling to round it to a whole number -- and then multiply by the grid size to move it to the nearest grid center.

    So my idea is to not pay attention to the piece it collides with but rather to its position in the game and to snap it to a grid accordingly.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • VyseVyse Member Posts: 4
    Thanks so much! That is exactly what I'm looking for. Sorry that I didn't explain it well enough, but yeah, I just want the pieces to snap on to a grid accordingly. I'm going to definitely use what the picture you posted as soon as I figure out how to make a grid. Once I learn how and create a grid pattern, my problems will be solved.

    Thank you so much @tatiang, I'll try not to bother you again :P
Sign In or Register to comment.