[Solved] Making a entity stick to another upon collision


(Ethan) #1

I have an arrow entity I made that I would like to stick to a movable block entity when they collide. How can I accomplish this?

My attempt half works. The arrow will stick to the object but won’t budge when the other entity is moved until it is about to stop colliding.

Here is a sample of what I came up with. This is the update function in my Arrow class.

	override public function update():void
	{
		checkCollision();
		
		if (collided == false)
		{
			if (facing == "down")
				moveBy(0, speed);
			if (facing == "up")
				moveBy(0, -speed);
			if (facing == "right")
				moveBy(speed, 0);
			if (facing == "left")
				moveBy( -speed, 0);
		}
		else
		{
			x = collidedObj.x - distanceX;
			y = collidedObj.y - distanceY;
		}
			
			
		if (timer >= 500)
			world.remove(this);
		else
			timer++;
	}

and this

private function checkCollision():void
	{
		if (collide("solid", x , y))
		{
			collidedObj = collide("solid", x, y);
			
			collided = true;
			distanceX = collidedObj.x - x;
			distanceY = collidedObj.y - y;
			
		}	
	}

(Nate ) #2

Can you re-describe your issue? I am not sure what exactly you mean. Do you mean, the arrow sticks to the object, but since the object is moveable, once you move the object the arrow does not move with it?


(Ethan) #3

Yeah, that’s correct. Say I have a block that can be pushed by the player. When that block is hit by an arrow, I would like the arrow to stop and stay in that position stuck to the block. That way, it feels as though the arrows got launched into and it now stuck to the side of whatever entity it hits

. However, my solution has weird side effect where the arrow only moves once it is about to stop colliding with the block.

Here is an example of it of my code in action, it should be easier just to show you. When you load up the swf, just push the block to the right with the player a little bit and you will see arrows being launched down. You can see how the collision currently works with those two entities.

http://pages.iu.edu/~efetsko/Wake.swf


(Per_K) #4

So if I understand this correctly distanceX and distanceY are values that is supposed to keep the arrow x and y at a fixed distance from the box x and y when it has hit the box, those making the arrow to follow the movements of the box.

What seems to happen here is that the distanceX and distanceY is recalculated each loop as long as there is a collision between the arrow and the box. They get new values all the time from the values of box x and y and arrow x and y. This will keep the arrow in the same place on the screen, as long as there is a collision. When there no longer is a collision, the distanceX and distanceY will not change and the arrow will follow the box. So the point where the arrow starts to follow the box is when there is no collision any more.

So you need to stop the program from setting new values for distanceX and distanceY once the arrow has hit the box and the first values has been assigned.


(Ethan) #5

Yeah, that’s the conclusion I came to as well. I tried to get around this by setting up a variable in checkCollision() that takes the initial distance between the two entities so that it doesn’t update on every game loop. However, I think I just realized the problem with that thanks to you. It is still getting updated as well because checkCollision() gets called in the update function even if collision between two objects has already occurred.

I’ll try it out real quick.


(Ethan) #6

Ok, I stopped calling checkCollision() if a collision has already occurred and it works almost perfectly now. The only small issue I have now is that the arrow lags a frame behind the block object, so it doesn’t look as smooth as I would hope.


(Per_K) #7

That could be the order in which the box’ and the arrow’s position is updated within a game loop.

If you update the arrows position first (based on the box’ position at the start of the loop) and then update the box’s position, the arrow will be one frame behind in rendering.


(Zachary Lewis) #8

I was thinking about this for a bit and I made a thing. Click anywhere to shoot blocks from the center that stick.

sticky-blocks.swf (87.5 KB)

Here’s the source.


(Ethan) #9

Cool, that’s exactly what I was looking for! Thank you for putting the time in to put an example together.

I checked out the source code and I think I should be able to implement something similar without much trouble.


(Zachary Lewis) #10

It’s a pretty simple implementation. An entity that can be stuck to, Block, keeps a list of entities that are currently stuck to it, Vector.<Arrow>. Whenever Block.update() is called, it iterates through the Vector.<Arrow> and calls Arrow.updateStuck(). In Arrow.updateStuck(), it sets the location of the Arrow relative to an offset distance to Block.


Animating/Moving CHANGEABLE weapons/armor through code? (equipping items) - PAPERDOLLING -
(David Williams) #11

I love how your solutions to problems are always so logical, yet simple enough to understand, and I’m just over here like, “Well, somehow make the one thingy update before the other thingies.”


(Zachary Lewis) #12

Getting to “Well, somehow make the one thingy update before the other thingies” is the first step. The next step is, “How do I make the one thingy update before the other thingies?” That’s usually why my responses come late, because I’ll see a post here, think on it for a day, then answer (usually after people already solved the problem :confused:).