Sign in to follow this  
MPG

I need help with AI.

Recommended Posts

<source> if(adone.x <= 200) { adone.y = adone.y - 1; } if(adone.y <= 100) { adone.y = adone.y + 1; adone.x = adone.x + 3; } </source> When the image goes up and over right it stops and starts to shake at 200. Can someone tell me how to bypass this?

Share this post


Link to post
Share on other sites

adone.x = adone.x + 3;
Imagine adone.x = 197 then adone.x = 200 then adone.x = 203, at this point, this statement will always yield false:
adone.x <= 200
To prevent that, the simplest solution is to set adone.x = 200 whenever (adone.x <= 200) is false.

Hth

Edit: I feel like this is not quite helpful, do you want the image to bounce around the screen ?. I'm not sure about your code correctness.

Share this post


Link to post
Share on other sites

if(adone.x <= 200)
{
adone.y = adone.y - 1;//problem
//you are checking the X coordinate, but modify the Y coordinate
//either use if(adone.y<=200)
//or use adone.x -= 1;
}
if(adone.y <= 100)
{
adone.y = adone.y + 1;
adone.x = adone.x + 3;
}


Share this post


Link to post
Share on other sites
It won't bounce if the condition around the 200 point on OY only allows it to increase on OX coordinate. I don't really know what you are trying to do, because if you are trying to move an object in XOY then you need to deal with both axes in the same compound "if" statement.

Share this post


Link to post
Share on other sites
I don't know exactly what you want to do, but perhaps you could try using AND/OR statements, or even just an ELSE statement, for the second 'if'.

Try explaining in words what you want to happen, then you will get a much better answer.

Share this post


Link to post
Share on other sites
Just put the executable in a zip file then it will upload fine.

Here's what your code does right now:

--------------------
| | |
| 2 | 1 |
| | |
| | |
|--------|---------|
| | |
| 3 | 4 |
| | |
|________|_________|


Quadrant 1: x = 201+, y = 0-100
adone will move down 1 and right 3 until y enters quadrant 4 (y = 101)

Quadrant 2: x = 0-200, y = 0-100
adone will move up one, then down one (cancelling to 0) and x will move right 3 until it enters quadrant 1

Quadrant 3: x = 0-200, y = 101+
adone will move up 1 until it enters quadrant 2

Quadrant 4: x = 201+, y = 101+
adone will not move

So maybe you could say what you're trying to do?
ie. "I want adone to move in a square, from 100,100 to 200,200"
or, "I want adone to move right and zig-zag up and down"
something like that... because your executable will most likely just show us how it doesn't work in a different way than your code has, when we really need to know what effect you want.

As for your first explanation, the code you pasted wouldn't stop moving right and then shake at x=200, so perhaps you should paste more of your movement code, or else make sure there are no typo's in the code you already pasted.

Share this post


Link to post
Share on other sites
Quote:
Original post by MPG
I'm trying to make the image move in square shaped path ,but the image stops half way through the path.


Well, you said about "path", so why not create a polygon - a set of 4 vertices which form a square then make the object travels along each sides:

//Some thing like this:
//v1, v2 is 2 arbitrary neighbor vertices

float const fTimeToCompletelyMovedToANewVertex = ... ;
Vector v1, v2 ;

...
Vector velo = (v2 - v1) / fTimeToCompletelyMovedToANewVertex ;
...

void Update ()
{
pos += velo ;
}


I hope it is easier now.

Share this post


Link to post
Share on other sites
Heres the plane
S = stuck
B = beginning = adone.x = 550,adone.y = 179
x=200 reverse
S_________________________B
| |
| |
| |
| |
| |
| |
| |
| |
| |
|_________________________|
y = 400 x = 600

Share this post


Link to post
Share on other sites

if(adone.x <= 200)
{
adone.y = adone.y - 1;
}
if(adone.y <= 100)
{
adone.y = adone.y + 1;
adone.x = adone.x + 3;
}




B = beginning = adone.x = 550,adone.y = 179


I'm scratching my head to find out what you are really doing here ?. According to your line below: adone.x = 500, adone.y = 179 are initial values. They will invalidate these 'if' statements at the first place.

And how about create a real path then make the object travel along ? What's wrong with it ?. It's easier and you can mix and match a set of paths to form completely new paths, make it unable to predict.

Share this post


Link to post
Share on other sites
Quote:
Original post by Skeleton_V@T
*** Source Snippet Removed ***



And how about create a real path then make the object travel along ? What's wrong with it ?. It's easier and you can mix and match a set of paths to form completely new paths, make it unable to predict.


I'm new to AI so will you please explain what that means? :) Thanks

Share this post


Link to post
Share on other sites
Making objects travel along defined paths is not difficult to implement. Assume we've got a small struct called Vector2D (we only care about X, Y float members). Then we create a container class called Base_Polygon (kind of).

All of this you've probably guessed, I'm just filling the post with already-known stuff[wink].

Base_Polygon manages a set of vectors that you can use in 2D collision detection system (afterward), right now it is considered a bunch of vertices grouped together and has position.

To make an arbitrary object travels along your defined polygon, you may create another base class that handles all the moving stuff. I call it PolygonHelper (don't laught, I know this is not a very descriptive name[smile]).

PolygonHelper consists of a Base_Polygon and a relevant Move ()method that moves the polygon along the path.
Every objects that benefit from this inherit from PolygonHelper class, then calls this method each time the derived object's Live () method is called.

When we want an object to move along, just assigns a set of vertices to PolygonHelper.BasePolygon and PolygonHelper.Move () will do the rest. I hope you'll take the big picture.

For example:

struct Vector2D
{ //Keep it as simple as possible
float X, Y ;
}


class Base_Polygon
{
public:
//Put constructor/destructor here
Base_Polygon (struct Vector2D const *psVertices, unsigned int uiNVertices)
//Do memory allocation and copy operations to member variables
m_psVertices ((...) malloc (...)),
m_uiNVertices (uiNVertices)
{
//Check and Copy...
}
//Put getters and setters here also
struct Vector2D const *GetVertices () {return m_psVertices ;}
private:
struct Vector2D *m_psVertices ;
unsigned int m_uiNVertices ;
}



class PolygonHelper
{
public:
//Put constructor/destructor here
//Assigns a new path to the object
void SetPath (class Base_Polygon const *pPoly, unsigned int uiNVertices)
{
//Check for doubled SetPath () and some memory errors as well
m_pPoly = new class BasePolygon (pPoly, uiNVertices) ;
m_uiCurrentVert = 1 ;

//Set the position to the first vertex
struct Vector const sTemp = *(pPoly -> GetVertices ()) ;
m_pPoly -> SetPos (sTemp) ;
}
void Move () ; //More on this later
private:
class Base_Polygon *m_pPoly ;
unsigned int m_uiCurrentVert ;
static float const m_fStepSize = 0.01f ; //Some small value
}



class Derived : public PolygonHelper
{
public:
void Live () ;
private:
//...
}


So, what does the trick is Move (), this method will be called by DerivedObj.Live (), the Live () method will be called per frame, as your original 'if' statements.


void PolygonHelper::Move ()
{
//Compare the two vectors
if (m_pPoly -> GetPos () != m_psVertices[uiCurrentVert + 1])
{
struct Vector2D sTemp = m_pPoly -> GetPos () ;
sTemp += m_fStepSize ;
m_pPoly -> SetPos (sTemp) ;
}
else ++uiCurrentVert ;
}


void Derived::Live ()
{
PolygonHelper::Move () ;

//Do some derived class specific here
}




I've tried to keep thing as simple as possible, the result of which might be a very bad design structure. I hope you will grasp this idea, let things go on as you wish, good luck. I'm waiting for you new game[smile].

Quote:

you can mix and match a set of paths to form completely new paths, make it unable to predict.

Things should quickly become trivial when you want to set a new path for the object, or choose them randomly. This method can be use in scripting as well.

Share this post


Link to post
Share on other sites
To be more clarified, this method has nothing to do with graphics. Base_Polygon just consists a set of vertices which aren't visible to the user.
You get the vertices data then interpolate (not sure if it's the right word) your object's position between two vertices. The simplest example is between 2 vertices:

A---------------------------------B

The distance between A and B is determined, the velocity (m_fStepSize) is determined also, given a formular:
s = v * t
---> t = s / v

You translate object's position until the calculated time has passed:
[code]if (fAccumulatedTime <= fTimeNeeddedToMove)
{
m_sPos += m_fStepSize ;
fAccumulatedTime += fTimeHasPassedSinceTheLastFrame ;
}
else STOP

Or with my original Move (): If object's initial position is A, after each frame it moves a small distance to B, then if object's position = B then STOP.

Expand it to a polygon, the story is the same, if Object's position = B then object's new destination = C, after that continues to move from B to C as usual until it has reached the last vertex.

void PolygonHelper::Move ()
{
//Compare the two vectors
if (m_pPoly -> GetPos () != m_psVertices[uiCurrentVert + 1])
{
struct Vector2D sTemp = m_pPoly -> GetPos () ;
sTemp += m_fStepSize ;
m_pPoly -> SetPos (sTemp) ;
}
else ++uiCurrentVert ;
}

The value uiCurrentVert + 1 is the index to next destination vertex, the above code says if your object has reached the next destination then increase uiCurrentVert to make it move to the next of the next destination :-p (my code lacks of checking for the last destination and some possible errors though).

I hope this helps.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this