开发者

Sprite to Line Collision

开发者 https://www.devze.com 2022-12-27 00:53 出处:网络
If I have a sprite, ho开发者_运维知识库w would I check collision between two points? For example, in a game I am making, I would like to draw multiple lines that my sprite collides against. I\'m think

If I have a sprite, ho开发者_运维知识库w would I check collision between two points? For example, in a game I am making, I would like to draw multiple lines that my sprite collides against. I'm thinking that this is more flexible than other collision systems if I had a lot of platforms.


Some simple logic can help reduce wasted calculation, ie if the top of the sprite is lower then both of the points, you can't have a collision. Sort of a simple bounding box collision check.

Once you have done that, I would suggest that you get a 'formula' for your line, then check which of the corners of your sprite lay above or below that line. If they do not all lie on the same side, you have a collision.

for instance, if your line was of y=x/2+2, starting at x=-20 ending at x=20 and you had square sprite 3 wide/high at (3,-1) then you have the four points of your spirte (3,-1)(3,2)(6,2)(6,-1). You work out the y value of the line at those two x positions, which give you y=3.5 at x=3 and y=5 at x=6 all of those y values are greater then the y values of the cube, thus the cube is below the line.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号