How to lock an main actor to the most nearest actor
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.
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
-
tatiang Posts: 11,949
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
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.
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
Thank you so much @tatiang, I'll try not to bother you again :P