0

i am about to code my own collision detection. So this is how it works:

  1. my object casts a move event.
  2. my world / collision manager listens for the move events of all movable objects and checks if they move.
  3. if they move i check for collision and resolve them

now the thing is that the movement speed of my object changes sometimes in the following code:

private void OnObjectMove(StaticObject MovingObject)
    {
        if(maskCollision(MovingObject))
        {
            Console.WriteLine("===============================");
            Console.WriteLine("movementX: " + MovingObject.movement_Y.Speed + " movementY  " + MovingObject.movement_Y.Speed);
            resolveMaskCollision(MovingObject);
        }
    }

and the beginning of my resolve collision method:

private void resolveMaskCollision(StaticObject collidingObject)
    {
        Console.WriteLine("movementX: "+collidingObject.movement_X.Speed + " movementY: " + collidingObject.movement_Y.Speed);
.
.
.

so and example output is:

movementX: -8,700003 movementY  -8,700003
movementX: 0 movementY: -8,700003

As you can see my movementX changed for some reason...

the output is most of the times the same but sometimes it is not and then all my methodes throw exeptions because i dont expect the speed to change there. Now my only explaination for the change of the objects speed is that the event runs on a seperate thread. Is this true? How can i wait for the event? And is it clever to wait for it if it is possible? Or would it slow down my code? (i could either call the methode from my movingObject somehow. But then it would have to save the world/collisionmanager -object)

crush3dice
  • 198
  • 1
  • 2
  • 15

4 Answers4

2

As seen in this answer, events are synchronous, unless you build them to be otherwise.

What was useful to me as a gamedev, and which may be useful to you, is the implementation of a 'dirty bit', which tells whether or not an object has changed (in position, etc), to avoid unnecessary calls to computationally expensive methods. This allows you to move your objects in serial, testing collision as you move each object. You may also find Quadtrees (or octrees, in a 3d system) to be particularly useful if you run into performance difficulties.

Community
  • 1
  • 1
SomeGuy
  • 485
  • 3
  • 12
  • Thanks for this usefull answer. But i do not ceompletly understand what you mean with that dirty bit. I understand that it will be true if the object changed, but how should i make use of it in my case? – crush3dice Feb 05 '14 at 21:27
1

@Adam Sears is right, there is your answer, see:

MovingObject.movement_Y.Speed

vs.

collidingObject.movement_X.Speed

you're comparing Y to X.

Those two methods are running in the same thread.

mikey
  • 5,090
  • 3
  • 24
  • 27
1

For the question whether events run asynchronously in C#, a similar answer applies as to so many questions: It is up to you (or whoever wrote the class whose events you want to consume). The default way to implement an event in C# is that events are fired in the same thread that caused the "event" that the event represents. However, you can just implement this part differently and implement asynchronous events.

The synchronous implementation looks like this:

protected virtual void OnFoo(EventArgs e)
{
    if (Foo != null) Foo(this, e);
}

The asynchronous implementation would look like this:

protected virtual void OnFooAsync(EventArgs e)
{
    if (Foo != null) Foo.BeginInvoke(this, e);
}
Georg
  • 5,626
  • 1
  • 23
  • 44
  • 1
    Your third option is in no way asynchronous. The `async` is effectively a lie. – Servy Feb 05 '14 at 21:15
  • You are right, deleted third option. The compiler even gives a warning that the method will be run synchronously – Georg Feb 05 '14 at 21:22
0

This case is completely synchronous events. Please put the code of Speed which is called.

There is a possibility that while getting speed "MovingObject.movement_Y.Speed" some dependent field would be changing due to some async call.

sunnytyra
  • 89
  • 1
  • 3
  • its simply return speed variable. Nothing more... get { return speed; } set { speed = value; } – crush3dice Feb 06 '14 at 13:55
  • who writes or changes the value to the speed variable ? – sunnytyra Feb 07 '14 at 10:43
  • 1
    my palyer listens for a key down event. If a key is down the speed will be reduced or raised (or if the maximum speed is reached nothing happens). And if no key is pressed this frame it will slow down a little bit (else my character would keep running if my key isnt pressed). The actualy error was that i casted my float speed into int and in some rare cases where my speed was between -1 and 1 the int was 0 and since i divided my slower direction by the faster one it could happen that i do [slow direction]/0 and then my slow direction step was the maximum negative value of int. – crush3dice Feb 07 '14 at 11:10