View Full Version : Hittest for 'walls' within Rotation Based Navigation?
Odysseus
October 4th, 2006, 01:50 PM
Hi, my name is Ody...:?)
...but really... I just started on creating a new flashgame. I used to create games mainly based on clickable buttons... but now I would realy like to embed one more in to scripting.
In this game my character is using rotation based navigation I took from flash examples and altered.
The movement part now looks something like this:
onClipEvent (load) {
// declare and set initial variables
thrust = 2;
decay = 0.00;
maxSpeed = 8.0;
}
onClipEvent (enterFrame) {
// rotate right or left
if (Key.isDown(Key.RIGHT)) {
_rotation += 9;
}
if (Key.isDown(Key.LEFT)) {
_rotation -= 9;
}
//
//
if (Key.isDown(Key.UP)) {
// calculate speed and trajectory based on rotation
xSpeed += thrust*Math.sin(_rotation*(Math.PI/180));
ySpeed += thrust*Math.cos(_rotation*(Math.PI/180));
} else if (Key.isDown(Key.DOWN)) {
// calculate speed and trajectory based on rotation
xSpeed -= thrust*Math.sin(_rotation*(Math.PI/180))/2;
ySpeed -= thrust*Math.cos(_rotation*(Math.PI/180))/2;
} else {
// deccelerate when Up Arrow key is released
xSpeed *= decay;
ySpeed *= decay;
}
//
// maintain speed limit
speed = Math.sqrt((xSpeed*xSpeed)+(ySpeed*ySpeed));
if (speed>maxSpeed) {
xSpeed *= maxSpeed/speed;
ySpeed *= maxSpeed/speed;
}
//
// move beetle based on calculations above
_y -= ySpeed;
_x += xSpeed;
}
As you can maybe see it not perfect, yet. For example I still have to put in a different way of walking backwards. :block:
My main concern is how to create walls? (/not permit diferend characters from walking through each another, and 'objects'.)
I would like to use "hittest" on surtent Instances to do this. I know how to do that, but can't seem to think of a code that sets up a "wall".
I already did some hittest experiments, creating things that set up:
moving floors,
sticky floors,
icey floors
kill-all-movement floors,
dubbel-your-speed floors,
magnetic floors,
turn-contols floor,
the normal-agian floors.
It's just in case of a wall (don't-walk-here-but-against-it) I can't seem to figger out.
If someone could point me in the right direction, that would really mean a lot to me.
btw this is my First Post @ kirupaForum. :geek:
Thanks for reading,
ody
Holmesc
October 4th, 2006, 02:44 PM
So you want a hitTest with walls...? A basic hitTest that should work (providing that I am understanding you correctly) is as follows:
onClipEvent(enterFrame) {
if(_root.character.hitTest(this)) {
_root.character._x += 5;
}
}
This script should be given to the wall, assuming that your character's instance name is character, and the orientation of the character and wall is as follows:
l
l <-The wall
l
l :yoshi: <-Your character
l
l
Hope that helps!
Odysseus
October 4th, 2006, 03:12 PM
So you want a hitTest with walls...? A basic hitTest that should work (providing that I am understanding you correctly) is as follows:
onClipEvent(enterFrame) {
if(_root.character.hitTest(this)) {
_root.character._x += 5;
}
} This script should be given to the wall, assuming that your character's instance name is character, and the orientation of the character and wall is as follows:
l
l <-The wall
l
l :yoshi: <-Your character
l
l
Hope that helps!
Aha, I will go and try that just now.
Until now I Always was adding the scripst to 'the character'. Maybe, this will get me on track... I'll be sure to let you know. '
Thanks.
Odysseus
October 4th, 2006, 04:11 PM
Basicly it's working... the way I'm using it right now it's still a bit bumpy... but so far so good...
I go and experiment now...
I'll share my findings, if there are any ofcource.
Maybe there will be more questions also.
:egg:
Thanks again Holmesc.
Odysseus
October 4th, 2006, 08:04 PM
I had my go with it...
http://i12.tinypic.com/30c26hv.gif
The thing is... the results still are a bit bouncy. When you keep on walking into the wall, or even when you are just letting it lead you -bouncy it is...
Maybe tomorrow I will find something to kill the bouncyness out of it.
I don't think it's because you can't do proper walls using hittest on instances? Or is there a better way to do it?
Odysseus
October 5th, 2006, 11:40 AM
Using the scripts above, this is what happens:
http://i12.tinypic.com/2gwcfeq.gif
Seems I can't get the wall guiding the character in a gentle manner.:-/
I don't know if someone over here could help me on my way.
It sure would help me a lot if I know how to do 'gentle walls'.:smirk:
Nich
October 5th, 2006, 04:48 PM
I'd like to say I that I was really impressed by the graphics in your second to last post. They look sharp, and look like the beginnings of a good game.
To solve your "bouncing" problem, I suggest making a temporary x and y variable to modify, and instead of using hitTest, make boundries.
For example, assume that the far wall's _x value is 1000
ActionScript Code:
if(_root.char.x > 1000) // note the use of x instead of _x
{
_root.char.x = 1000 // note the use of x instead of _x
}
_root.char._x = _root.char.x
Esentially you're modifying the character's x value before moving it, allowing you better control over where it goes before the user sees it.
This gets more complicated when hitTesting two moving, non line objects, but for simple walls this code will serve you better then hittests.
Odysseus
October 5th, 2006, 06:30 PM
Sure hope I will create a good game. I'm willing to put lots of time in to it.
I'll try your script firts thing tomorrow.
Hope it can set up inner walls and do diagonal walls two.
Thanks a lot.
Odysseus
October 6th, 2006, 11:20 AM
I don't seem to impliment your script the right way. Tried lots of different combinations but mostly it seems I end up defining char starting point(?) by using your script. Maybe you could explain where I should put it?:-/
_______________________________________________
I could paste this script on to the 'char's movement:
if (_y<20) {
_y = 20;
}
if (_y>232) {
_y = 232;
}
if (_x<20) {
_x = 20;
}
if (_x>520) {
_x = 520;
}
:jail:
This, in a sense, sets up outer walls. But this way I don't see how to do inner walls and diagonal walls. Wall types I really do need because the game is going to have this isometric thing going on.:block:
If someone knows something I could try please don't hesitate to tell me.
Nich
October 6th, 2006, 04:41 PM
A (reasonably) simple way to do that would be a tile based method. A tutorial on how to set this up is here:
http://www.tonypa.pri.ee/tbw/start.html
It's mostly 2d but there's a bit on isometric games in there.
The theory is create tiles that are either walkable or not, and use the tiles to make walls and such.
Odysseus
October 6th, 2006, 05:02 PM
A (reasonably) simple way to do that would be a tile based method. A tutorial on how to set this up is here:
http://www.tonypa.pri.ee/tbw/start.html
It's mostly 2d but there's a bit on isometric games in there.
The theory is create tiles that are either walkable or not, and use the tiles to make walls and such. Yes, I was hoping there would be a simpeler way. Thanks for the advice.
Btw I still did'nt figger out how to use you script correctly.
The isometric part is not something I'm worrying about. I'm sure when I finally have a wall in '2D' I can translate it to isometric view.
nathan99
October 6th, 2006, 08:44 PM
sorry but what's your current hitTest code?
Odysseus
October 6th, 2006, 10:04 PM
I did a wall by pasteting this script:
onClipEvent(enterFrame) {
if(_root.sch.hitTest(this)) {
_root.sch._x += 11;
}
} to the wall.
a diagonal wall now looks like this:
onClipEvent (enterFrame) {
if (_root.sch.hitTest(this)) {
_root.sch._x += 9;
_root.sch._y += 4.3;
}
}
I did quite some different numbers also. The results are bouncy never the less.:-/
SacrificialLamb
October 7th, 2006, 01:43 AM
if you use
while (_root.sch.hitTest(this)) {
_root.sch._x++;
}
it'll be smoother but using hitTest like that slows down the game (can be a lot with a big map). tonypa has a better way to do it if you don't mind making a tile game.
SacrificialLamb
October 7th, 2006, 02:06 AM
Other way's a i have looked doing hitTests with non tiles are to use an ordering code to put all unit’s in an array for lowest _y to highest, then use a for loop like
for (i=0; i<ar_namesy.length; i++) {
unitA = ar_namesy[i];
nmax = ar_namesy.length;
for (j=i+1; j<nmax; j++) {
//set unit B
unitB = ar_namesy[j];
oldDis = _root["uCom_"+i+"_"+j+"_dis"];
//work out diferuns for x and y
defx = unitA._x- unitB._x;
defy = unitA._y- unitB._y;
//work out distens
dis = Math.sqrt((defx*defx)+(defy*defy));
//
//see if it's movein away
if (dis>oldDis) {
//set new old distens
_root["uCom_"+i+"_"+j+"_dis"] = dis;
//skip the rest of the for loop
continue;
}
//your code would go here
while (_root.sch.hitTest(unitB)) {
//this would not work like this coz it might not
//be ++ you could use some trigonometry to
//work out which way to bump
_root.sch._x++;
}
_root["uCom_"+i+"_"+j+"_dis"] = dis;
}
}
I use this for unit/unit hit test but it works the same. Then I added some early exclusion things like if unit’s gap is increasing and are unit’s moving. Then if there _y is to low it’ll continue and if it’s to high it’ll reduce nmax so it stops the inner loop and dose not perform unnecessary calculations. I use the gap from the center points but that’s because the units being represented by circles is fine. But you could use some thing like this to reduce the number of hitTest’s that the computer has to do.
Odysseus
October 7th, 2006, 11:09 AM
if you use
ActionScript Code:
while (_root.sch.hitTest(this)) {
_root.sch._x++;
}
it'll be smoother but using hitTest like that slows down the game (can be a lot with a big map). tonypa has a better way to do it if you don't mind making a tile game. Thanks, already this script is working out fine! I'll try the other one later this day.
Odysseus
October 7th, 2006, 12:13 PM
onClipEvent (enterFrame) {
while (_root.sch.hitTest(this)) {
_root.sch._x++;
_root.sch._y++;
}
} This I'm using on diagonal walls, but instances seem always to be muts of a square. So I did quite a lot of little instances to set up this wall... The game ideed(!) gets muts slower after even getting down one diagonal wall.
Maybe I'll have to build a trigger to switch on/off the calculations in a surtent area? Or is this what you just where trying to tell me in the second part of your scripture? -edit-(I think it is...but don't have time to explore it right now)
Else maybe someone knows how to get rid off the "square" instances?
SacrificialLamb
October 7th, 2006, 04:35 PM
I understand that there is a push line command that can be used for diagonals or other odd shapes but I have not used it and subsequently do not know how it works, or even if it dose that. But if you have some time on your hands you could look into it.
I think you will find that the hardest part of the game will probably be getting it to run at a good frame rate, you should look at way’s to let the computer not do complicated equations if you can find out they are unnecessary early on. Flash is very CPU intensive on a PC and it’s much worse on a MAC, if you are intending for this to be a game that will be played on the web you should test in a web browser because it will run slower than a flash player
Odysseus
October 7th, 2006, 06:36 PM
Well it doesen't have to run on the internet. But I'm testing it at 40 fpm at this time. (Though, the animations are animated to be effective at about 16 fpm.) If there are any unregular framerate things showing at this time (doing walls only) I think I have a problem. So I'll be sure to google this "push line command''.
Thanks agian.
SacrificialLamb
October 7th, 2006, 09:14 PM
I had a look and I could not find any push line command but I know some one told me they used it but that might be some other language so I found an example of what I think it was.
http://www.tonypa.pri.ee/vectors/tut07.html
Sorry if you wasted any time looking.
Odysseus
October 8th, 2006, 04:13 PM
I had a look and I could not find any push line command but I know some one told me they used it but that might be some other language so I found an example of what I think it was.
http://www.tonypa.pri.ee/vectors/tut07.html
Sorry if you wasted any time looking. Thanks I''ll be trying to covert this in to something usefull. I did not find the time to look for this push line stuff.
Maybe, I should also illustrate my 'wall problems' again, so others might have some new input two.:)
Odysseus
October 8th, 2006, 08:15 PM
http://i12.tinypic.com/4gtagzm.gif
This way there can be lots of walls without the calculation time.
I did not created this in script. (I don't know how to do it.) In fack it's just my way of showing you what I would like to do.
Hopefully this makes things clear.
SacrificialLamb
October 8th, 2006, 10:48 PM
would it not spend as much time calculating for the trigger as it would for the wall. Making this just another unnecessary step. Unless you had some thing like a big grid and first test to see what grid the unit is in them test the unit/walls in that grid, the surrounding 9 or nearest 4
Odysseus
October 8th, 2006, 11:04 PM
would it not spend as much time calculating for the trigger as it would for the wall. Not, if the wall is build up out of multiple calculations. Also the trigger only has to detect a hit but not calculate angles or something.(?) Take in mind that every wall not activated is build up out of multiple instances...
Making this just another unnecessary step. Unless you had some thing like a big grid and first test to see what grid the unit is in them test the unit/walls in that grid, the surrounding 9 or nearest 4 I really don't know... my way looks simpler to me.
Odysseus
October 8th, 2006, 11:39 PM
http://i12.tinypic.com/2u61dhf.jpg
Lots of instances make up a wall... all of them "turned off" until you go near is a reduction of calculations that surely can't be a unnecessary step?
SacrificialLamb
October 8th, 2006, 11:42 PM
but i thought you only made the wall out of multiple instances after you not being able to get the hittest to see a diagonal only “muts of a square” (post #17). any way aslong as it works
[edit]I think I see what you are getting at now. I just expect on the size map’s that I have been working with all the triggers would be enough to overload the system
Odysseus
October 8th, 2006, 11:56 PM
but i thought you only made the wall out of multiple instances after you not being able to get the hittest to see a diagonal only “muts of a square” (post #17). any way aslong as it works Yes, I don't seem to have figured that out yet.
[edit]I think I see what you are getting at now. I just expect on the size map’s that I have been working with all the triggers would be enough to overload the system
Well, the maps that I will be creating don't have to be bigger than 6 times the size of my screen. Also: the outer walls will be done by 'movement restriction' not 'real walls'.:block:
...... all the triggers would be enough to overload the system Maybe I would have to have triggers on triggers than...(?):}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.