Okay, here's my code with the normals.
1 2 3 4 5 6 7 8 9 10 11
| ArbiterList arbiters = world.getArbiters(); for (int i = 0; i < arbiters.size(); i++) { Arbiter a = arbiters.get(i); Contact[] contacts = a.getContacts(); for (int j = 0; j < contacts.length; j++) { if (a.getBody1() == player.getBody() || a.getBody2() == player.getBody()) System.out.println(contacts[j].getNormal()); } } |
Don't worry about the player's Body, that's just because I only want to report the normals for the player while I am testing this.
The results are:
1 2 3 4 5 6 7 8 9 10
| [Vec 4.3435415E-7,1.0 (1.0)] [Vec 4.3435415E-7,1.0 (1.0)] [Vec 0.0,0.0 (0.0)] [Vec 0.0,0.0 (0.0)] [Vec 0.0,0.0 (0.0)] [Vec 0.0,0.0 (0.0)] [Vec 0.0,0.0 (0.0)] [Vec 0.0,0.0 (0.0)] [Vec 0.0,0.0 (0.0)] [Vec 0.0,0.0 (0.0)] |
Repeatedly. The value of that normal (the ones that actually have a value) is correct of course (if very small values are rounded off), because it is pushing straight up in the Y direction only, but I'm curious about those zero's. Should I just have my code ignore any vectors with a length of zero? I'm more curious exactly what collision they are supposed to be dealing with... where are they coming from?
Also, those reported values are when the player is at rest on top of a StaticBody with a Box shape.
[EDIT]
I got it to work, using normals (as long as I ignored the zero ones, things seemed to work fine). Here is the resulting source code. It basically says that a hill with a 45 degree angle or less should count as the ground. I will probably change my jump application of force to reflect the angle of the ground slope, but for now this works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| ArbiterList arbiters = world.getArbiters(); for (int i = 0; i < arbiters.size(); i++) { Arbiter a = arbiters.get(i); Contact[] contacts = a.getContacts(); for (int j = 0; j < contacts.length; j++) { if (a.getBody1() == player.getBody() || a.getBody2() == player.getBody()) { if (contacts[j].getNormal().getY() != 0 || contacts[j].getNormal().getX() != 0) { if (contacts[j].getNormal().getX() == 0 || Math.abs(Math.atan(contacts[j].getNormal().getY()/contacts[j].getNormal().getX())) >= Math.PI/4) { if (a.getBody1().getPosition().getY() < a.getBody2().getPosition().getY()) doGroundHit(a.getBody1()); else doGroundHit(a.getBody2()); } } } } } |
And back to my other question...
I'm going to eventually need to send out a "ray" to where the Body will be. If there is a StaticBody that is, say, 50 pixels in the direction of velocity of the Body, then a certain button should do a jump-type-thing. I'm not sure the best way to navigate around the space like that would be, aside from potentially looping through every Body and doing it on my own. Is there a smarter way to do this?
Any insight on this?
Thanks.