Forum

> > CS2D > Scripts > Player Collision
Forums overviewCS2D overview Scripts overviewLog in to reply

English Player Collision

12 replies
To the start Previous 1 Next To the start

old Re: Player Collision

DannyDeth
User Off Offline

Quote
Simply check the distance between the two players, if it is less than approximately 28 pixels a collision has occurred.

old Re: Player Collision

DC
Admin Off Offline

Quote
The player collision size is 12 pixels around the center on the x and y axis. Not a circle, just a square. This makes things even easier.

So it works this way (just using cs2d lua cmd player, very basic math and math.abs which make all values positive = removes the minus if there is one)
1
2
3
4
5
6
7
8
9
10
11
function playerscollide(id1,id2)
	x1=player(id1,"x")
	y1=player(id1,"y")
	x2=player(id2,"x")
	y2=player(id2,"y")
	if (math.abs(x1-x2)<12) and (math.abs(y1-y2)<12) then
		return true -- players id1 and id2 collide
	else
		return false -- players id1 and id2 don't collide
	end
end

well.. this is the way to DETECT collisions. Making players actually collide (you would have to use cs2d cmd setpos in case of a collision) is a bad idea because the Lua code is executed on the server only so this will become very laggy und jerky when you try it in a net game. Also lag and high pings might allow players to "lag" through each other without collision. You would have to use a rather complex code to avoid this stuff.

old Re: Player Collision

DC
Admin Off Offline

Quote
It simply fits best. The player skin is 32x32 pixels, so 16 pixels around the center would make most sense at first glance. But not all the 32x32 pixels are filled. So 4 are subtracted on each side to make it fit better. This also allows you to go quite easily through narrow passages which have a size of just 1 tile (which is 32x32 too). Imagine the player collision would be 32x32 as well. It would be quite difficult to navigate the player through such narrow passages. This would be quite annoying and unplayable

old Re: Player Collision

Avo
User Off Offline

Quote
Ok, I'm just interesting. So player's collision area is square, not circle?

old Re: Player Collision

DC
Admin Off Offline

Quote
Please read what I wrote before asking... I already explained it
user DC has written
[...]Not a circle, just a square[...]

old Re: Player Collision

Apache uwu
User Off Offline

Quote
Player hit detection is indeed a square, but for hit detection, since the player's image is more so like a circle, we should really use the distance formula.

old Re: Player Collision

Apache uwu
User Off Offline

Quote
That's for bullet hit detection. The image of the ct/t is close to being a circle than a square.

If 2 players were to hit diagonally, they would stop short from colliding--looking unrealistic.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview