 # Thread: First steps in Movement development

1. ## First steps in Movement development

Hi all! I'm moving my first steps in MMF2 Movement development. At the moment I'm trying to make a simple movement that moves an object in 360 directions, but what I've written so far gives a strange behaviour. Here it is my Move() function:
PHP Code:
``` BOOL CRunMvt1::Move(LPHO pHo){    LPRO pRo = (LPRO)pHo;    if (pRo->roc.rcSpeed != 0)    {        pHo->hoX = pHo->hoX + pRo->roc.rcSpeed * cos(m_dir * PI / 180); //m_dir is not the 32 directions mask, it's the angle between 0 and 359        pHo->hoY = pHo->hoY - pRo->roc.rcSpeed * sin(m_dir * PI / 180); //used the minus (-) because I want 90 degrees direction to be upwards, and not the opposite    }    callAnimations(pHo, ANIMID_WALK);    callCollisions(pHo);    return TRUE;}  ```
If the direction is 315 degrees, the object moves correctly, but if the direction is 45, the object seems to move with angle a little greater than 45 degrees.
Also, with direction 100, it moves straigt upwards exactly like with 90 degrees.
What's wrong with my code?
Thank you in advanced for your help!   Reply With Quote

2. Sorry to bump in this.

but the problem seems to be a mix between integer and double, in order to obtain precision you should use double variable X, Y, angle.

double m_X = pHo->hoX;
double m_Y = pHo->hoY ;

m_X = m_X + pRo->roc.rcSpeed * cos(m_dir * PI / 180); //m_dir is not the 32 directions mask, it's the angle between 0 and 359
m_Y = m_Y - pRo->roc.rcSpeed * sin(m_dir * PI / 180); //used the minus (-) because I want 90 degrees direction to be upwards, and not the opposite

and after convert both value to the corresponding integer.

pHo->hoX=(short)m_X;
pHo->hoY=(short)m_Y;

check the pinball example

hope it helps  Reply With Quote

3. You need to keep track of your position with double member variables in your movement class, because with FVivolo's code you would get over-time rounding errors, that is, the little rounding errors adding up over time. If you keep double member variables in the movement class then you can maintain precision at all times.  Reply With Quote

4. Yes,

LB is right, that why is good idea you take a look to the pinball example.  Reply With Quote

5. Thank you for your answers! I'm learning from the Pinball example like you suggested me. If I need further help I'll ask here.   Reply With Quote

6. ## Bounce function

I don't understand some parts of the Bounce function in the Pinball example:
Code:
```    ...
// Finds the shape of the obstacle
double a;
double aFound=-1000;
for (a=0.0; a<2.0*_PI; a+=_PI/32.0)
{
double xVector=16*cos(angle+a);
double yVector=-16*sin(angle+a);
double x=m_X+xVector;
double y=m_Y+yVector;

if (callTestPosition(pHo, (int)x, (int)y, 0, CM_TEST_PLATFORM))
{
aFound=a;
break;
}
}
...```
From what I've understood (correct me if I'm wrong) it checks for collision in positions around a circle. But I don't understand:
1) Why aFound starts from -1000?
2) Why, in the for loop, a is incremented by _PI/32.0?
3) Why cos and sin are multiplied by 16?

Thank you again for your help!   Reply With Quote

7. 1. In case it never finds an obstacle, -1000 is the sentry value. It could have been anything that wouldn't be in the range returned by the search.
2 & 3. I think this has to do with either MMF2 using 32 directions at most, or just a way to scale precision.  Reply With Quote

8. <SortaCore> magic upvote over-commenting
* Magic upvoted over-commenting to 9001 on behalf of SortaCore  Reply With Quote

9. Originally Posted by LB 1. In case it never finds an obstacle, -1000 is the sentry value. It could have been anything that wouldn't be in the range returned by the search.
2 & 3. I think this has to do with either MMF2 using 32 directions at most, or just a way to scale precision.
Thank you for your answer, now I understood point 1. I'm still not sure about why they used that 32.0 and 16. I also thought 32.0 could be related to the 32 directions of MMF, but the Pinball movement is 360 degrees, so I'm not sure that's the reason. Also, does
double xVector = 16 * cos(angle+a)
mean it will check for collision in next 16 pixels? Should I use this value in my movement too?
Thank you again!   Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•