• Content count

  • Joined

  • Last visited

Community Reputation

148 Neutral

About dabo

  • Rank
  1. Arrive steering behavior

    Thanks for the reply. It does decelerate nicely if I remove delta time from the acceleration calculation, but then I get the problem that my object reaches the max speed directly after just one call to Update().
  2. Arrive steering behavior

    I found a C# implementation of the same example from the book and it had no parenthesis around deceleration * decelerationTweaker like this: [CODE] double speed = distance / (double)deceleration * decelerationTweaker; [/CODE] When I changed this in my code it appeared to work even with deltaTime: [CODE] Velocity = Velocity + acceleration * deltaTime; [/CODE] I am really confused why I need to remove the parenthesis when all other implementations I have seen (except the one I mentioned above) uses them. I have also tested implementations not from the book and they also result in this oscillating behavior when I use it in my code.
  3. Arrive steering behavior

    Thank you for your reply. Yes I thought keeping deltaTime in the calculation was correct. I am just confused as to why the book author's code work but not mine, I don't see any difference and I have tried using the same default values for mass, max force, max speed.
  4. Arrive steering behavior

    [quote name='ankhd' timestamp='1353656302' post='5003402'] hello. Try playing with the DecelerationTweaker Im using 1.3 I only get that problem when the speed is to high. [/quote] Did you change something else too? Because if I only change the deceleration tweaker to 1.3 I still get the same problem at default max speed 1.6 for example.
  5. Arrive steering behavior

    Thank you for the tip. I noticed that when I change this line: [CODE] Velocity = Velocity + acceleration * deltaTime; [/CODE] into: [CODE] Velocity = Velocity + acceleration; [/CODE] It appears to work like I expect, it also fixes the seek steering behavior. Can anyone explain this? I thought deltaTime had to be included when calculating both velocity and position. Maybe there is still a problem hidden somewhere. EDIT: I should add, at higher speeds I need to increase the max force as well.
  6. Arrive steering behavior

    [quote name='Álvaro' timestamp='1353504674' post='5002910'] I guess it might be a matter of adjusting parameters correctly. The parametrization used in that code is a bit strange to me, but you essentially have a differential equation that looks like a [url="http://en.wikipedia.org/wiki/Harmonic_oscillator#Damped_harmonic_oscillator"]damped harmonic oscillator[/url]: force = -k * (position - target_position) - c * velocity You'll then compute acceleration as force/mass. The damping ratio is defined as c/(2*sqrt(m*k)). If your damping ratio is under 1, you'll have oscillations. You should aim for a damping ratio of 1 or just a little above. As usual, experimentation is the only way to tell what feels right in your situation. Of course you can cap the magnitude of the force to some maximum value. [/quote] Thanks I will look into that. I have created a minimal program showing the problem, now you can test it yourself. [url="http://www.dabostudios.net/Arrive.zip"]http://www.dabostudios.net/Arrive.zip[/url]
  7. Arrive steering behavior

    [quote name='Álvaro' timestamp='1353456575' post='5002803']Perhaps you can make a complete small program around the code you posted so we can run it on our own? [/quote] Ok I will do that when I get home from work.
  8. Arrive steering behavior

    This is a copy paste from the simple soccer example: [CODE] Vector2D SteeringBehavior::Arrive(Vector2D TargetPos, Deceleration deceleration) { Vector2D ToTarget = TargetPos - m_pVehicle->Pos(); //calculate the distance to the target double dist = ToTarget.Length(); if (dist > 0) { //because Deceleration is enumerated as an int, this value is required //to provide fine tweaking of the deceleration.. const double DecelerationTweaker = 0.3; //calculate the speed required to reach the target given the desired //deceleration double speed = dist / ((double)deceleration * DecelerationTweaker); //make sure the velocity does not exceed the max speed = min(speed, m_pVehicle->MaxSpeed()); //from here proceed just like Seek except we don't need to normalize //the ToTarget vector because we have already gone to the trouble //of calculating its length: dist. Vector2D DesiredVelocity = ToTarget * speed / dist; return (DesiredVelocity - m_pVehicle->Velocity()); } return Vector2D(0,0); } [/CODE]
  9. 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. [CODE] 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; } [/CODE] And this is how one of my game objects uses the steering behavior class: [CODE] 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; } [/CODE] 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.
  10. Thanks, I will see what I can do with this. Using a reflecting response for certain angles and sliding for others seem to make sense.
  11. Thanks but I would prefer to learn more about this and implement it myself. But a physics library for C# could be plan B.
  12. 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.
  13. 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.
  14. Thanks for your response. I will go with game loop 1 then and see how it goes. I assume the slightly different output between run 1 and 2 is not a problem then.