Jump to content

  • Log In with Google      Sign In   
  • Create Account

dabo

Member Since 15 Dec 2007
Offline Last Active Feb 03 2013 01:01 PM

Topics I've Started

Arrive steering behavior

20 November 2012 - 11:20 AM

Hello everyone,

I am trying to implement the arrive steering behavior that can be found in the book "Programming Game AI by Example". My project is in C# so I had to rewrite the code that came with the book. Below is the relevant part of my steering behavior class.


		public Vector2 SteeringForce { get; private set; }
		public Vector2 Target { get; set; }
		private enum Deceleration
		{
			Fast = 1,
			Normal = 2,
			Slow = 3
		}
		private Vector2 Arrive(Vector2 target, Deceleration deceleration)
		{
			Vector2 toTarget = target - Position;
			double distance = toTarget.Length();
			if (distance > 0)
			{
				//because Deceleration is enumerated as an int, this value is required
				//to provide fine tweaking of the deceleration..
				double decelerationTweaker = 0.3;
				double speed = distance / ((double)deceleration * decelerationTweaker);
				speed = Math.Min(speed, MaxSpeed);
				Vector2 desiredVelocity = toTarget * speed / distance;
				return desiredVelocity - Velocity;
			}
			return new Vector2();
		}
		private Vector2 SumForces()
		{
			Vector2 force = new Vector2();
			if (Activated(BehaviorTypes.Arrive))
			{
				force += Arrive(Target, Deceleration.Fast);
				if (!AccumulateForce(force))
					return SteeringForce;
			}
			return SteeringForce;
		}
		private bool AccumulateForce(Vector2 forceToAdd)
		{
			double magnitudeRemaining = MaxForce - SteeringForce.Length();
			if (magnitudeRemaining <= 0)
				return false;
			double magnitudeToAdd = forceToAdd.Length();
			if (magnitudeToAdd > magnitudeRemaining)
				magnitudeToAdd = magnitudeRemaining;
			SteeringForce += Vector2.Normalize(forceToAdd) * magnitudeToAdd;
			return true;
		}
		public Vector2 Calculate()
		{
			SteeringForce.Zero();
			SteeringForce = SumForces();
			SteeringForce.Truncate(MaxForce);
			return SteeringForce;
		}

And this is how one of my game objects uses the steering behavior class:
		public override void Update(double deltaTime)
		{
			Vector2 steeringForce = SteeringBehaviors.Calculate();
			Vector2 acceleration = steeringForce / Mass;
			Velocity = Velocity + acceleration * deltaTime;
			Velocity.Truncate(MaxSpeed);
			Position = Position + Velocity * deltaTime;
		}

The problem is that I get an oscillating behavior around the target position, the object oscillates less and less and after awhile it stops at the target position. Can anyone see anything in the code I have posted that would cause this oscillating behavior? I fail to see what I have done wrong when I rewrote the code into C# from C++. The samples accompanying the book obviously work so I don't expect any errors in the original code.

I appreciate any help.

Collision detection and response along a circle

18 May 2012 - 09:20 AM

Hello,

For my 2d hockey game I am trying to figure out how to handle collisions with the puck and the boards. This is not one of my areas of expertise so I could really use your help, especially when it comes to the rounded corners. This is what I do now:

The short and long sides of the boards are each represented by a line and the rounded corners by a circle each. The puck is really just a point in the game but when doing collision checking I use a line with a starting point of the previous position of the puck and an end point of the current position + a value (to avoid the risk of missing the collision). Then I use a line/line intersection test (short or long side) or line/circle intersection test (corners) to get the intersection point. When the puck's current position is on the back side of the board's plane (outside the rink) it collided with I move the puck back to the intersection point and reflect the velocity vector based on the normal of the intersection point. Finally the distance that the puck traveled outside the rink is added to the current position but along the direction of the new velocity instead.

While this seems to work for keeping the puck inside the rink the puck travels far from smooth through the corners. For example if I shoot the puck right along one of the long sides of the rink I want the puck to slide along the corner and exit parallell to the short side; this doesn't happen, it exits at different angles depending on the speed of the puck.

Any idea what I am doing wrong? Is it possible to get smooth movement along a circle when reflecting the velocity? I appreciate any help.

Normal of circle intersection point

12 May 2012 - 01:48 PM

Hello,

I am creating a simple hockey game and I am trying to figure out how to handle puck collisions with the boards. It is pretty easy with the short and long sides since each one have only one normal but I have problems with the rounded corners, each will have lots of normals.I don't know how to calculate the normal of the intersection point between the puck and the corner. How does one usually go about doing this? I appreciate any help. I do 2d line intersection tests for the short and long sides and it works well, is it possible to do something similar between a line and a circle? And then from the intersection point calculate the normal in that point?

Thanks in advance.

Simulation (game) loop in WPF

03 May 2012 - 10:14 AM

Hi,

I am working on a simulation software for basic physics using WPF. In my software I want to be able to show a simulation based on the input the user has provided (some shapes moving around). I need to implement some kind of simulation (game) loop but I need help figuring out how to do this properly. These are my requirements:
  • Run with the same speed independently of the hardware.
  • Multiple runs of the simulation, given the same input, shall yield the same visual output.
  • Ability to start, stop, pause and resume at any time.
Not required but would be great:
  • Adjustable simulation speed: 0.5x real-time, 2x real-time, 10x real-time for example.
I guess this can be split up into two questions: How do I create a simulation loop which meets my requirements? and how do I integrate it into my WPF software? Any help would be greatly appreciated.

How to determine velocity after a collision?

22 April 2012 - 01:03 PM

Hi,

I am working on a simple hockey game and I have come to the stage where I need to handle collisions between the puck and the ice, boards, posts etc. What I need help with is how do I determine the velocity after a collision? The velocity is a vector and I have a method for reflecting it which means the puck will bounce. However right now when a puck is shot high in the air and eventually drops down onto the ice again it will bounce and reach approximately the same height on each bounce (if I ignore air resistance). As anyone who watches hockey knows the puck hardly bounces at all (for simplicity I will assume the puck always lands flat) when it hits the ice. How do I calculate this loss of velocity? Do I need to calculate energy loss or something? Any help would be appreciated, not sure what to google for. If I need to provide more information let me know.

PARTNERS