Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (780) Games in Android Showcase (233) games submitted by our members Games in WIP (857) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Corner collision detection. [Need Help]  (Read 2940 times) 0 Members and 1 Guest are viewing this topic.
GlennBrann
 « Posted 2013-04-26 18:19:50 »

Hey Everyone,

I know some of you are thinking i hate these posts. but i just can't find any good posts teaching how to do corner Collision. Basically I'm taking part in Ludum dare tonight and i know basic enough collision but i want to hopefully make a plat former of the sort. but the problem is. I dont know how to go about doing proper collision. when i move my player against a certain tile i want the player to stop moving. How would i implement this in a some what simple form. I'm using no libraries. IF ANYONE can just give me some code to represent my idea could you please help. I'm having alot of trouble.

I try to stop the players velocity when it interects with a tile but the player keeps jumping slowly through the tile. I am trying to move away from .intersects(). Could you please help Thanks

- GlennBrann
Bassex96

Senior Devvie

Medals: 5

 « Reply #1 - Posted 2013-04-26 20:15:22 »

Axeman

Senior Devvie

Medals: 7

 « Reply #2 - Posted 2013-04-27 01:31:56 »

If you want to do a "AABB vs AABB" test here´s a vector based way to do it.

You need some vectors for the boxes: center, half the width and half the height.
You also need a vector to store the result: minimum translation vector.

 1  2  3  4  5 minimumTranslationVector.x = Math.abs(this.center.x - other.center.x) - (this.halfWidth + other.halfWidth);minimumTranslationVector.y = Math.abs(this.center.y - other.center.y) - (this.halfHeight + other.halfHeight);if(minimumTranslationVector.x < 0 && minimumTranslationVector.y < 0)       //Collision

What this mean is that you test one axis at a time for overlap. The absolut value ( = positive value) of "(this.center - other.center)" puts the center position close to zero and then you add half the size of the box. If the "subtracted position" is greater than the size of the boxes, you get a positive value and there is no overlap. If the "subtracted position" is smaller than the size of the boxes, you get a negative value.

That is all if we just want to test intersection, but what is the "minimum translation vector" (I´m just gonna call it by it´s abbreviation "mtv" from now on)? This is a little bonus: it is the smallest amount you have to push back an object for it to NOT be intersecting. This is often a problem in collision detection, that you get stuck too deep in a collision so you can´t get out. So how do you use the mtv? One way is to see what axis is has the smallest intersection and push it back that way. Let´s say you´re testing collision between a Player and a Wall:

1) Get the vector from the center of the player to the center of the wall: Wall.center - Player.center.
2) Find out on which axis has the smallest intersection (let´s just say in this case it is x)
3) player.position.x += mtv.x.

The result is that the player is positioned precisely outside the collision every collision check.

I hope it helps.

(any errors I blame on the very, very late hour over here... )
Pages: [1]
 ignore  |  Print

 hadezbladez (736 views) 2018-11-16 13:46:03 hadezbladez (372 views) 2018-11-16 13:41:33 hadezbladez (726 views) 2018-11-16 13:35:35 hadezbladez (187 views) 2018-11-16 13:32:03 EgonOlsen (2385 views) 2018-06-10 19:43:48 EgonOlsen (2539 views) 2018-06-10 19:43:44 EgonOlsen (1473 views) 2018-06-10 19:43:20 DesertCoockie (2137 views) 2018-05-13 18:23:11 nelsongames (1934 views) 2018-04-24 18:15:36 nelsongames (2615 views) 2018-04-24 18:14:32
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org