Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperLicht
2D Collisions

pete
Registered User
Quote
2011-10-14 23:27:03

I am really confused. I have a tank moving in a 2D space. It can be controlled by w, a, s, d. It's inside a square and I want it to collide with the borders of the square.

So this is my code when the tank is moving (executed every frame):


tankbody.Pos.Z = nextZ;
if (collisioncheck())
{
tankbody.Pos.Z = lastZ;
}
tankbody.Pos.X = nextX;
if (collisioncheck())
{
tankbody.Pos.X = lastX;
}

So first I move the tank to the next position on the Z axis. If there is a collision it will be put back to the position of the last frame. Then I do the same thing for the X axis. Now the problem is that the tank completely stops at the wall but I want it to "slide" along the wall.

This is the code of my collision function:

function collisioncheck()
{
tankboundingbox = tankbody.getTransformedBoundingBox();
if ( tankboundingbox.intersectsWithLine(new CL3D.Vect3d(-5, 1,-5), new CL3D.Vect3d(-5, 1, 95))
|| tankboundingbox.intersectsWithLine(new CL3D.Vect3d(-5, 1,-5), new CL3D.Vect3d(95, 1, -5))
|| tankboundingbox.intersectsWithLine(new CL3D.Vect3d(95, 1,95), new CL3D.Vect3d(-5, 1, 95))
|| tankboundingbox.intersectsWithLine(new CL3D.Vect3d(95, 1,95), new CL3D.Vect3d(95, 1, -5)))
return true;
else
return false;
}

The functions checks if the tank's boundingbox collides with 1 of the 4 borders.

Please help, it's my first try with Javascript. I need to know how to fix the collisions.
Here is the demo if you don't understand what I mean (collisions only programmed for forward movement yet. Turning and moving backwards goes trough wall):
http://home.arcor.de/bownz/pete/


niko
Moderator
Quote
2011-10-15 07:01:41

Nice demo. For sliding, you need to calculate a sliding vector and move the tank along that then.
Alternatively, you also can just use a CL3D.AnimatorCollisionResponse, this should do anything for you automatically.


pete
Registered User
Quote
2011-10-15 19:29:42

Thank you niko. I found out how to fix it. I had to add tankbody.updateAbsolutePosition(); to the first line of my collisioncheck()-function. I don't really understand why because I don't know how getTransformedBoundingBox() is working in detail. It was just a guess. That took me like a whole week but it's working now.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internation?l" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact