I received an email asking for ways to make solid objects (meaning no other objects should be able to go through the solid), so I figured I’d survey the crowd to see if anyone has more efficient ways to make this happen.
The solution I came up with is to add a block of code to all the objects in the game saying that when they are in touch with the solid, they will bump back, but this would require the person to add this block of code to all the objects. Are there any easier ways?
I’m not sure - I’d be interested to learn myself how to do this easily! - but I’ll tag @Spy_Guy_96 since he’s made a bunch of platformers with a really good collision engine.
The way I do it is pretty complex. But to make it easier to understand, I basically use 2 variables that track the players next position. Then, if the object with collision realises that the player next position is inside the object, it will change the 2 variables to be right next to the tile.
Generally, for good collision engines - even with game engines outside of Hopscotch - it’s pretty crucial to have the next position of the moving object to compare it to the solid, so that next position can be adjusted for, so it collides and stops on a solid instead of bouncing on it. It’s the difference between good collision and weird floaty collision, ie. something like this.
And then, yeah, like everyone else is saying, you could just add that collision code into an ability, or use clones, to use that code everywhere else!
If you make it a custom rule it’s easy to add to all objects and would be pretty good. The main problems are figuring out how best to detect the two objects touching and how to push them away from the object.
For a lot of uses, the touching block would work if you time it right. Importantly I think (I’m not sure) that you’d need to move the object first (put the moving code above the touching rules) then the touching, then some sort of code to push it out.
That last part is the hardest part, I’m not sure how best to do it and end up having to fiigure it out every time I want to use it so any help would be helpful.
So like collision codes?
I mean you could do when bumps object check once if X pos = X pos +100 yeah but that would take a while. I’ve been wondering how to do that.
FoxProductions has some really good games that use that
Purple Custom Block:
When game starts{
Set [(self) type] to [whatever you want... solid=1 non-solid=2
}
When [anything] is touching [self] {
Check once if [(self) type] = [1] {Broadcast message named [Solid hit]}
}
When [anything] is not touching [self] {
Check once if [(self) type] = [1] {set var [var1] to [self x position]; set var [var2] to [self y position]
}
When I get a message [Solid hit]{
Set position x[var1] y[var2]
}
Is this clear? This purple block would need to go into every object… I haven’t tested this code but it should work…
I have two objects. The first object is a can… you can’t slide your hand through a can. But my second object is just a picture of a slice of bread made by a projector . The second object is NOT a solid, so you can slide your hand through it. It is just a projection… however, the first object is a solid, you can NOT slide your hand through it…
Now back to hs…
In Hs currently objects can pass over/through other objects. Making an object a solid would mean that the object trying to pass over/through the object would stop once it hits the (a) solid object.
E.g. I have two objects: Bear and Robo.
Robo is set as a solid and positioned at x:300; y:500
Bear is positioned at x:0; y:500
Bear’s code is as follows:
When Game Starts
Move forward by 1000
Bear’s y position is the same as Robo’s, meaning that they are on a collision course. Bear’s code makes bear move forward by 1000, bumping into robo after the first 300. If robo isn’t a solid, bear would just continue past robo by either moving in front of robo or behind robo (depending on their z index values)… Note: bear’s y position would not change.
However, if Robo is a solid, when bear bumps into robo, bear would stop moving.
Phew that was a lot! … I hope I helped you understand.