1 Replies - 990 Views - Last Post: 20 April 2013 - 10:44 AM

#1 Wils  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-April 13

XNA Collision Response Needed For Stopping Objects From Moving

Posted 17 April 2013 - 06:04 AM

Currently I'm having troubles of halting my Player's movement as well as Enemy's movement when either one or both collides with Wall. My current collision detection method is to calculate the distance between the source and target, if source is smaller than target, collision occurs.

I was able to deal with the enemy if it collides with a Destroyable Wall, but when it comes to non-destroyable Wall, I have no idea. I tried to set Player's Position as Player's previous Position but it doesn't work.

Got the code from SixOfEleven ( i think ) 's Asteroid Clone and changed some and the rest of it is mine
private static bool collidedWith(GameComponents source, GameComponents wall)
        {
            Vector2 pos1 = source.pos;
            Vector2 pos2 = wall.pos;

            if (distance(pos1, pos2) < source.area.Width)
            {
                return true;
            }

            return false;
        }

        private static float distance(Vector2 pos1, Vector2 pos2)
        {
            float Cathetus1 = Math.Abs(pos1.X - pos2.X);
            float Cathetus2 = Math.Abs(pos1.Y - pos2.Y);

            Cathetus1 *= Cathetus1;
            Cathetus2 *= Cathetus2;

            return (float)Math.Sqrt(Cathetus1 + Cathetus2);
        }

private static void CollisionDetectionAndResponse()
        {
            foreach (Enemy e in Factory.enemyList)
            {
                foreach (Wall w in Factory.wallList)
                {
                    foreach (Player p in Factory.playerList)
                    {
                        // if Enemy collides with Destroyable Wall
                        if (w.spawned && e.spawned && w.bCanDestroy
                            && collidedWith(e, w))
                        {
                            e.speed = 0;

                            if (w.hp > 0)
                            {
                                w.Damage(1);
                            }
                            else
                            {
                                e.speed = 1;
                                w.Kill();
                            }
                        }

                        // if Enemy collides with Un-Destroyable Wall
                        if (w.spawned && e.spawned && !w.bCanDestroy
                            && collidedWith(e, w))
                        {
                            // do something
                        }

			if (w.spawned && p.spawned
                            && collidedWith(p, w))
                        {
                            // do something
                        }
                    }
                }
            }
        }




Is This A Good Question/Topic? 0
  • +

Replies To: XNA Collision Response Needed For Stopping Objects From Moving

#2 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 567
  • View blog
  • Posts: 1,263
  • Joined: 24-April 12

Re: XNA Collision Response Needed For Stopping Objects From Moving

Posted 20 April 2013 - 10:44 AM

Basically, what I did was design a routine that asks "Can I move x distance during this frame?" If the distance to the surface is greater then the requested move, the move is granted. If the distance to the surface is less then the requested move, the distance to the surface (minus any additional space you want to maintain between them) is sent back as the allowable distance to move.

So, it's not so much a question of "Can I move without collision?" as much as it is "How far can I move without collision?"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1