Hello I am trying to create a 2D platformer, and I want to check if there is a collision about 0.1 BU beneath me.
so what I originally had set up was a ray sensor pointing downwards , it went flrom the centre of my object to 0.1 BU beneath it.
then if there was a collision I snapped the object to the ray hit position and set the downwards velocity to 0
but here’s the problem with that:
in state1 it works right…
but in state 2, the ray sensor doesnt collide and the object falls down, even though it’s meant to stay up
So I’m wondering if there’s a way that I could check for collision underneath me, but not just in a ray but in like an area or somethiung, it also needs to be able to get hit collision though so I can snap my object to that position, so if you could help me with that, that would be wonderful, thankyou.
A) sensors not connected to any controller of an current state will not be evaluated = deactivated
B) for “Area” check you can use a sensor object + collision sensor
@sdfgeoff: okay, so the reason I dont use the physics engine is because I couldn’t get it to work properly, my player kept on getting stuck in walls and the ground, so I decided to try and implement it manually, which I didnt think would be hard because I already had experience with that.
and what I’m trying to do is just check whether or not there is a collision below me, so then I can account for it before I actually collide meaning it doesnt look like I fall into the ground then get pushed back up.
@youle: that would only allow me to apply actions for collisions once I’ve already collided with it
So if object1.worldPosition.x-1 < object2.worldPosition.x < object1.worldPosition.x+1 and object1.worldPosition.y-1 < object2.worldPosition.y < object1.worldPosition.y+1 and object2.worldPosition.z+1.1 < object1.worldPosition.z: toDo
oh wait I figured out monster’s so I created an object, 0.1 BU thuck put it beneath my player object, made it a sensor type then gave it a collision sensor.
and use that collision sensor in my players script as a means to check for collision
downSensor = scene.objects["downSensor"].sensors["Collision"]
if downSensor.positive:
print ("hello")
I would probably rather use a sensor as well, but you can’t get the collided point with just a sensor, which is necessary if you want to snap the character to the ground. So you might end up using a ray cast anyway.
You could use a sensor to detect a collision, and then a ray in the same direction to get the hit position and snap the character to the ground, but obviously the ray won’t work if you’re only partially on the ledge. Inconsistent behavior probably isn’t the best solution.
You could use just one ray and give your character a diamond, sphere, or capsule-like shape, which means that the character would automatically slide off of any corners. This works okay, but you can get stuck if you’re moving in the air in the opposite direction that you’re sliding in (so the physics engine is pushing the character and your controls are also pushing you).
Finally, you could also cast multiple rays, which works pretty well, though obviously it’s 3x the calculations for a single raycast.
I used getHitObject function and then got the position of that, which will work if all my objects are square, they arent though, so I reckon I will be using multiple rays, that would be better.
“or I could learn to use the phys. engine better” I think it is the best solution (for sensors near, ray, mouse, collision, you can use hitObject function to get the object “touched” and hitPosition to get the hit position…)… You can also get informations about rayCastTo, rayCast, getScreenRay etc…
As I mentioned, you can’t get the hit position from a collision sensor, hence the need to use a rayCast or Ray sensor to get the hit position. You don’t need to snap the player to the ground, but he’ll hover at least a little bit above it (worst case, he’ll hover a considerable distance).