03-19-2012, 09:20 AM
|
#1
|
|
Member
Join Date: Feb 2012
Posts: 52
|
[AS3] help with multiple enemy collision with wall
hello,
In my game i have a player, and multiple zombie enemies, which move torwards the player .
The problem is that i dont know what to do to make the zombies stop when they hit the wall, or other zombies. to avoid lagging the game by giving each zombie its own internal movement code which i did before, i am moving each zombie in a loop. the trouble is now, how do i stop them hitting the wall.
One problem i faced before though was that i didnt know how to make sure each zombie when they hit the wall, didnt simply get stuck there.
Does anyone know how i can make sure that they can still move sideways when hitting a block infront of them?
In my game i use tiles for all the collision blocks, so all surfaces are pretty much straight.
here is some of my AS code which i am using for collisions and moving the zombies, the moving code comes courtesy of another actionscript.org member, and attached is the whole project
ActionScript Code:
private function checkForCollisions() {
for (var m:int=dirt.length-1; m>=0; m--) {
if (dirt[m].hitTestObject(_player)) {
collide();
hit2=true;
} else {
hit2=false;
}
for (var Z:int=zArray.length-1; Z>=0; Z--) {
if (zArray[Z].hitTestObject(dirt[m])) {
//dont know what to put here
}
}
}
if (hit2==false) {
lastx=_player.x;
lasty=_player.y;
}
}
function collide() {
_player.x=lastx;
_player.y=lasty;
}
private function moveZombie() {
for (var Z in zArray) {
var enemy=zArray[Z];
var distX:Number=_player.x-enemy.x;
var distY:Number=_player.y-enemy.y;
var distance:Number=Math.sqrt(distX*distX+distY*distY);
if (distance<=aggroRange) {
var moveDistX:Number=turnSpeed*distX/distance;
var moveDistY:Number=turnSpeed*distY/distance;
enemy.movX+=moveDistX;
enemy.movY+=moveDistY;
var totalMove=Math.sqrt(enemy.movX*enemy.movX+enemy.movY*enemy.movY);
enemy.movX=followSpeed*enemy.movX/totalMove;
enemy.movY=followSpeed*enemy.movY/totalMove;
enemy.x+=enemy.movX;
enemy.y+=enemy.movY;
}
}
}
Anyway, can someone please help me with my game?
thanks!
|
|
|
03-19-2012, 09:34 AM
|
#2
|
|
Member
Join Date: Mar 2012
Location: Newtown, PA
Posts: 86
|
Quote:
|
Does anyone know how i can make sure that they can still move sideways when hitting a block infront of them?
|
This is actually simple. Make separate hittests for x and y, so when you get a hittest in x only the sprite will continue moving y.
I'm actually making a game with multiple sprites wandering around, when they hit a wall they just make a turn, let me know if you'd like to see the code.
|
|
|
03-19-2012, 09:51 AM
|
#3
|
|
Senior Member
Join Date: May 2011
Posts: 283
|
This problem ended up taking me three weeks to properly solve. The concept is pretty simple but the implementation is going to be pretty long if you intend it to be foolproof.
Bascally how it works is this:
When a dynamic object is found to be interpenetrating with a static object, you find the shortest path that will take it to the edge of the static, and move it there, so that they are touching but no longer inside each other.
Actually calculating this is kind of tedious. you need to create a list of all statics it interpenetrates with, calculate the X and Y directions for each one to move, and use the one with most "votes" as the direction. then you have to figure out whether to move X or Y first - you find which of the two offsets is smaller, and move it there. Then you test it again for collisions against all the statics in the list you made, and move it on the other axis if it is still colliding.
Once i eventually got this working, it's given accurate collision detection in 99% of cases so far. the only place i can really see it failing is if an object manages to somehow get trapped between two statics in a space that's too small for it, then it might not be able to move. But at least that won't cause any infinite loops or crashes since you're moving each object to stop interpenetration, at most, twice per frame.
|
|
|
03-20-2012, 01:08 AM
|
#4
|
|
Member
Join Date: Feb 2012
Posts: 52
|
How would i do a hit test for x and y? just make two hit tests , with the result of one being that x cannot change, and the result of the other being that y cannot change?
I know it seems simple, but i dont know how i implement it into this game
|
|
|
03-20-2012, 11:22 AM
|
#5
|
|
Member
Join Date: Mar 2012
Location: Newtown, PA
Posts: 86
|
Quote:
|
How would i do a hit test for x and y?
|
Here's my example from the class for enemy sprites:
ActionScript Code:
grubosc=20
if (dx>0) {
if (_parent.krzywa.hitTest(_x+dx+grubosc,_y,true)) {
dx *= -1;
}
} else {
if (_parent.krzywa.hitTest(_x+dx-grubosc,_y,true)) {
dx *= -1;
}
}
if (dy>0) {
if (_parent.krzywa.hitTest(_x,_y+dy+grubosc,true)) {
dy *= -1;
}
} else {
if (_parent.krzywa.hitTest(_x,_y+dy-grubosc,true)) {
dy *= -1;
}
}
'grubosc' is the size of the sprite, 'krzywa' is the walls of the place where the sprites are walking. when a sprite hits the wall it 'bounces'; changes direction and keeps walking.
|
|
|
03-22-2012, 02:38 AM
|
#6
|
|
Member
Join Date: Feb 2012
Posts: 52
|
i know this sounds nooby and unprofessional, but do you or anyone else know how i could implement that into my zombie class?
Or change my game code so that it would?
Im only just starting to learn enemy classes and some of the things around that.
Otherwise some tuts that could help with specifically what im after would help also.
|
|
|
03-22-2012, 10:38 AM
|
#8
|
|
Member
Join Date: Feb 2012
Posts: 52
|
please , im not very experienced at these things, could someone give me an example i could easy incorperate into my game, without the need to rewrite everything?
i have already tried and failed at about 3 different engined for collision detection and moving the zombie, this is the first that allws for both to happen without lagging the hell out of flash.
Also, i do not have XNA for flash, nor does my school were it would be graded
|
|
|
04-05-2012, 10:29 AM
|
#9
|
|
Member
Join Date: Feb 2012
Posts: 52
|
Quote:
Originally Posted by predi22
Here's my example from the class for enemy sprites:
ActionScript Code:
grubosc=20
if (dx>0) {
if (_parent.krzywa.hitTest(_x+dx+grubosc,_y,true)) {
dx *= -1;
}
} else {
if (_parent.krzywa.hitTest(_x+dx-grubosc,_y,true)) {
dx *= -1;
}
}
if (dy>0) {
if (_parent.krzywa.hitTest(_x,_y+dy+grubosc,true)) {
dy *= -1;
}
} else {
if (_parent.krzywa.hitTest(_x,_y+dy-grubosc,true)) {
dy *= -1;
}
}
'grubosc' is the size of the sprite, 'krzywa' is the walls of the place where the sprites are walking. when a sprite hits the wall it 'bounces'; changes direction and keeps walking.
|
is dx the acceleration being added onto the x/y of the sprite? or is it the target location?
|
|
|
04-05-2012, 11:02 AM
|
#10
|
|
Member
Join Date: Mar 2012
Location: Newtown, PA
Posts: 86
|
It's neither. You can call it speed. It's the number of pixels the object will move during each execution of the code.
__________________
__________
- Predi
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 02:31 PM.
///
|
|