00:00
00:00
Newgrounds Background Image Theme

privateC2004 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Hit Test Doesnt Work Right

937 Views | 5 Replies
New Topic Respond to this Topic

Hit Test Doesnt Work Right 2009-04-07 01:12:33


obj1 is a block in the air.
obj2 is the ground.
speed is obvious anyway.

Code:

var speed:Number=0;

stage.addEventListener("enterFrame", fall);

function fall (Event) {
	speed++;
	if (obj1.hitTestObject(obj2)) {
		speed=0;
	}
	obj1.y+=speed;
}

Problem:
obj1 does stop when hitting the ground, however it "sinks" into the ground (obj2) more or less depending on height and it seems pretty random on the first look although it sinks always by the same amount if the height is the same. (It doesnt necessarily sink more if it drops from a higher point.)

Whole .fla file just in case: http://spamtheweb.com/ul/upload/070409/2 9477_Experimental.fla
How the .swf looks (reload page to restart, sorry): http://spamtheweb.com/ul/upload/070409/2 9529_Experimental.php

Thanks for any help.

Response to Hit Test Doesnt Work Right 2009-04-07 01:16:29


Hey, dude.

Try this:

var speed:Number=0;

stage.addEventListener("enterFrame", fall);

function fall (Event) {
	speed++;
	if (obj1.hitTestObject(obj2)) {
		speed=0;
	}else{
	obj1.y+=speed
}

That way obj1 will fall only when it is not touching the ground. Hope this helps :]

Cheers.

Response to Hit Test Doesnt Work Right 2009-04-07 01:17:56


Oops, made a tiny syntax error, I forgot the end curly brace to the 'else' statement.

Here's what I actually meant:

var speed:Number=0;

stage.addEventListener("enterFrame", fall);

function fall (Event) {
	speed++;
	if (obj1.hitTestObject(obj2)) {
		speed=0;
	}else{
	obj1.y+=speed;
}
}

There ya go.

Response to Hit Test Doesnt Work Right 2009-04-07 10:06:01


First of all thanks for the help.

But i can directly tell that thats not really the problem, just looking at your code.
I better try however...
...
*After trying...*
As i thought, it didnt change anything.

That would be because, hittest comes after the "speed++" and before the "obj1.y+=speed" thus if the hittest comes out as true, speed will be 0 when it reaches "obj1.y+=speed" anyway, and the "speed++" code "wont make it" to the latter parts of the code so to say. The code in the hittest will "override" anything that altered the speed variable before, as it clearly states that speed=0 and comes directly before "obj.y+=speed".

And by the way... I actually found the problem myself. =P
I was just thinking about it at school and suddenly realized the problem. LOL
I will describe it with an example:
Lets say the falling block (aka obj1) comes really close to the ground (aka obj2), and untill this frame, speed has increased so much that it is now say 30 thus in the next frame the block will JUMP 30 pixels (or whatever the smallest distance is) AT ONCE before anything else happens. If the distance between the block and the ground was say 10 pixels, then the block will now sink 20 pixels into the ground.

Also got to admit that i dont have a slightest clue about how to fix this in a simple way.
Maybe i really just have to change the whole code. I still read and try to understand the latter parts of that Basic Platformer tutorial...

Response to Hit Test Doesnt Work Right 2009-04-07 11:40:59


heh just make the position of the falling object equal to the top of the object its falling onto.

I cant think of a way to really fix this properly without writeing your own collision detection, since the only way the collision is detected with hittest is when they overlap. so it will always be within the second object before it detects.

i suggest writeing the collision detection yourself, and before moveing it check the values plus the speed (before adding the speed to the falling object), that way before they overlap you can detect if an object is about to collide. when you know its about to collide you can just set its value to the top of the object its falling onto and set the speed to 0.

Response to Hit Test Doesnt Work Right 2009-04-07 12:31:37


ColdLogic wrote:
"heh just make the position of the falling object equal to the top of the object its falling onto."

Thanks but i already thought of that. =D

Dont really know about the rest though. I dont know about real collision detection yet, at all.

And yes, sadly i see my object first sink into the other one (aka overlap) and then get on top it...

I still have yet to understand how exactly its done in the Basic Platformer tutorial.
It has much more code tied to it like jumping and such so the code is just totally different...

Well all i have to do is read, think and experiment, and then just think even more if theres a problem in the experiment! XD XD XD Ultimately ask here of course...

Well whatever. Just thanks for all help.
Any more tips and such are still welcome though, i will be checking this thread.