PDA

View Full Version : can someone help me with collision?



Icezizim
June 15th, 2007, 02:05 AM
well just within the last month iv picked up actionscript and im trying to make a simple platform game from scratch. well i couldnt get a floor to work. i was using hitTest but it never works right, like: if(hero.hitTest(wall)) {yspeed=0} . i have a static ground right now. but i want to take that out and just make the ground a collision thing that stops the hero MC. so can someone please edit this to make the mc "hero" stop of hitTest with another mc, just call it "wall". oh and this code was all written to work on the first frame of _root, i just think its easier to edit when its all there. thanks



gravity=2
ground=300
power=20
onEnterFrame=function(){
yspeed-=gravity
hero._y-=yspeed
if(hero._y>ground){
yspeed=0
hero._y=300
jumping=false
}
if(Key.isDown(Key.LEFT)){
hero._x-=5;
hero._xscale=-100;
hero.gotoAndStop("walkright");
}
else if(Key.isDown(Key.RIGHT)){
hero._x+=5;
hero._xscale=100;
hero.gotoAndStop("walkright");
}
else{
hero.gotoAndStop("herostop");
}
if(Key.isDown(Key.UP)){
if(!jumping){
yspeed=power
jumping=true
}
}

}

Charleh
June 15th, 2007, 04:39 AM
Ok this....

oh and this code was all written to work on the first frame of _root, i just think its easier to edit when its all there.

is where you are going wrong!

But anyway you need to test to see if the hero is going to hit the 'wall' clips before it actually hits them - you can do this with a bit of trigonometry, yes ... maths!

First of all you need to at least get all your 'wall' objects into some kind of array or something so that you can test against all of them (or at least ones that are close enough).

Then you just check to see if the players vector to their destination intersects a wall object! If it does then you find out the point at which it intersects and move them to there, if not you move them the full distance, and so on until you hit something (or the static ground)

Icezizim
June 15th, 2007, 10:57 AM
ok, i dont really know were to start with something like that. could you point me to a tutorial or something? im only 14 give me a break :huh:

and why is it bad to write it all in _root.

Charleh
June 15th, 2007, 11:26 AM
ok, i dont really know were to start with something like that. could you point me to a tutorial or something? im only 14 give me a break :huh:

and why is it bad to write it all in _root.

Well depends on the size of your app, but if you get a good few objects and functions moving about the place good chances are you will have quite a bit of code! It's a lot neater and easier to use classes/inheritance.

Anyway - first of all you want some way of testing against ALL your walls rather than just one object.

You can probably just bung your whole level into a parent movieclip and hittest against it with shapeFlag = true, but I prefer to use an array because I know it works and it's easy to implement

All you need is an array -



var Walls:Array;

Walls = new Array();


then for each of your 'wall' objects



Walls.push(SomeWallObject);

push it onto the array

then loop through this array every frame and test for intersection on all walls that are close to you.

I'll do a proper flash file with an example in a while when I get home, as I'm at work :P

Charleh
June 15th, 2007, 11:26 AM
oops double post!

Charleh
June 15th, 2007, 11:57 AM
oops double post!

Heres a quick example before I go home, press the space bar to set a new point for the start of your line and move the mouse around, you can see where the line intersects a line on the stage - this is basically what you need to start to get accurate collision working with platforms and should yield no dodgy bugs!

I'll explain more later

Icezizim
June 15th, 2007, 12:01 PM
yeah a example would be alsome.

Icezizim
June 16th, 2007, 05:31 AM
damn i still cant figure out how to get any collision to work. can someone please help. all the other newbies seem to get collision easy :(

Charleh
June 16th, 2007, 05:42 AM
damn i still cant figure out how to get any collision to work. can someone please help. all the other newbies seem to get collision easy :(

Have you not looked at the file that I posted yet? That should get you started

Icezizim
June 16th, 2007, 03:04 PM
yeah i did, and i looked in the script for comments or something, but i dont get what to do with it. could you please elaborate :?

pingnak
June 16th, 2007, 05:26 PM
I think 'hitTest' might be the portion of that example that you're looking for. Plug it into the help search and then play with it.

Sirisian
June 16th, 2007, 08:33 PM
Here's an optimized form of the SAT used for rectangle to rectangle collision. Use the minimum translation distance theorem for the rest.

if(object1.x < object2.x+object2.width){
if(object1.x+object1.width > object2.x){
if(object1.y < object2.y+object2.height){
if(object1.y+object1.height > object2.y){

}
}
}
}Here's an unoptimized version of what you may want: (AS2)
clicky (http://www.sirisian.templarian.com/flash/FlashPlatform.zip)

example (http://www.sirisian.templarian.com/flash/flashplatform.swf)

Icezizim
June 16th, 2007, 08:40 PM
Here's an optimized form of the SAT used for rectangle to rectangle collision. Use the minimum translation distance theorem for the rest.

if(object1.x < object2.x+object2.width){
if(object1.x+object1.width > object2.x){
if(object1.y < object2.y+object2.height){
if(object1.y+object1.height > object2.y){

}
}
}
}Here's an unoptimized version of what you may want: (AS2)
clicky (http://www.sirisian.templarian.com/flash/FlashPlatform.zip)

example (http://www.sirisian.templarian.com/flash/flashplatform.swf)

thank you :)

that example is exactly what i wanted.