Collision Detection Problems with rotation and movement
Hi - (i'm a newbie)
I've come to a halt in working something out. After a long drawn out search for collision detection script i found one on this forum that is similar to what i want.
I have a boat which travels forward only (altered script for a moving car), you are able to rotate it left and right to chage direction you are travelling in. You aren't able to go backward.
There is land that you cannot travel (instance 'hit') over (as you're in the sea obviously ) and i have collision detection to make that possible. So it basically hits it and bounces util you change direction.
The problem i have - is that when you rotate to change your direction (with the reg point at the front of the boat) the rear of the boat goes ontop of the land.
I have tried changing my collision detection so that its bounding boxes rather than points that it checks, however - as soon as you rotate it no longer checks the collision of the bounding box.
I'm a bit stumped - i understand i would have to put a check where it's telling to rotate - but still havent come up with anything working
hope someone can point me in the right direction - thanks for any help in advance!
onClipEvent (load) {
speed = 0;
}
onClipEvent (enterFrame) {
// make the car go forward
speed += 0.1;
// tells the car to slow down after the speed of 20
if (Math.abs(speed)>20) {
speed *= .7;
}
// you can change the rotation of the car to your desire
if (Key.isDown(Key.LEFT)) {
_rotation -= 15;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 15;
}
// here is where the hittest is for the boundary
speed *= .98;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.hit.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.6;
}
}
You could try making it so when you hit the land you bounce back a little so that even if you rotate, your far away enough to not have the Hit box go over the land. Or make the hit box a circle, cause the distance of the outside of the circle to the middle is always the same so when you rotate, the box couldn't go over the land.
i neglected to mention i'd already had a think about making the bounding box a circle. and bouncing back a little to cover the rotation - however the problem i have with that is as follows....
the boat has a long net on the back - the game is about catching fish (its for a museum before you mention it sounds rather dull!)
so when you turn the whole back of the net goes over the land. as the registration point obviously has to be at the front of the boat for more realistic navigation.
now you can see more of my problem.
i just need it so when you rotate the net hits the land in the same way the boat did and stops before going over it
thanks for the reply so far - any more ideas would be really helpful
hmm.. tough situation. Not sure how good this would workk but perhaps you could have 2 seperate movieclips for the Hit test of the boat (one on the front and one for the back) and check if the land is hitting the back side and then make it bounce back a little. Im not sure how well that would work, it would work better then having it bounce back even if it hits the front.. but I dont know. Just tossing out some ideas.
Maybe someone else on the board can give you some ideas.
The problem i have - is that when you rotate to change your direction (with the reg point at the front of the boat) the rear of the boat goes ontop of the land.
So the basic idea is, that when the boat would hit someting moving at the current vector, apply some major friction? Well the obvious flaw in this is that it doesn't check for rotation (as you found out). So you should also have something reduce the rotation, or disallow rotation.
However, given this way to resolving a hit, your problem would probably be the same. It doesn't feel unnatural to have a car maneuver out of a sand or gravel patch, with some trouble, but a ship slowly working it's way of land? Hmmm I dunno. I think it would be best to make the possible rotation for the player on land Very tiny. Just thinking ahead though, could be wrong and it could feel natural.
Thanks for the ideas....here's a screenshot to help explain the problem. The graphics can't be changed as they've ben signed off now. The problem is i need rotation to navigate the boat around.
but when i turn after hitting land it's still able to rotate all the way over it - so as the boat is heading away - the net is on top. - if the net were smaller i could hide it under a land layer above everything else.
my problem is - i do not know what code to implement to check whether it hits during rotation as well as just going forward.
onClipEvent (load) {
speed = 0;
}
onClipEvent (enterFrame) {
// make the car go forward
speed += 0.1;
var Key
// tells the car to slow down after the speed of 20
if (Math.abs(speed)>20) {
speed *= .7;
}
// you can change the rotation of the car to your desire
if (Key.isDown(Key.LEFT)) {
_rotation -= 15;
Key = "Left"
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 15;
Key = "Right"
}
// here is where the hittest is for the boundary
speed *= .98;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.hit.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.6;
if(Key == "Left"){
_rotation += 15*.9;
} else if (Key == "Right"){
_rotation -= 15*.9;
}
}
}
I'm late, so this is some quick coding. Don't know if it got any bugs because of it.
A) you cant navigate on your own from the start. only travels forward until hitting the land (a bug which i sorted out as the var name needed changing from 'key')
B) you are forced to wait for the boat to turn before being able to navigate yourself again - losing some interactivity and getting frustrating... and also the graphic still goes over the edge of the land....i could possibly extend the hit area so it wasnt as obvious. You can also still turn the boat yourself and thus going over the land again.
I can't have it wait to turn as its a game against the clock
hhhmmm - still not quite what i'm after....i'll keep trying!
just exactly what i need - i didnt think of seperating the two - and it would work more naturally as in real life, and would solve my land problems.
you blinkok are bloody wonderful!!!!!! if this were real life i'd buy you a bottle of wine and or a pint.
I hope its ok if i get back to you if i'm too much of an idiot to implement this....but i'll give it a good old go first.
I'm not majorly amazing at flash - i'm a director programmer really, trying to make the move over to actionscript and its all fairly new.
thanks again for everyone's help - i'll message back when i've sorted so people can get feedback - as i hate it when you never know if its worked or not
B) you are forced to wait for the boat to turn before being able to navigate yourself again - losing some interactivity and getting frustrating... and also the graphic still goes over the edge of the land....i could possibly extend the hit area so it wasnt as obvious. You can also still turn the boat yourself and thus going over the land again.
Aah, I assumed you could reverse Anyways, Blinks solution is far superior. But as for as never getting the net over land... I don't think that's possible, without the net pulling on the ship as it get's bounced back of land. That would seem ackward to me.