Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 900 times)
0 Members and 1 Guest are viewing this topic.
Offline 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
Offline Bassex96

Senior Devvie


Medals: 4



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

This
http://sacredsoftware.net/tutorials/Collisions/CollisionsPart1.html

http://zetcode.com/tutorials/javagamestutorial/collision/

http://beginwithjava.blogspot.com/2010/08/simple-collision-detection-in-java.html
Offline 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... Smiley)
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (6 views)
2014-12-27 04:03:04

TheDudeFromCI (12 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (94 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!