Sign in to follow this  
be17_17y

Movement math ?

Recommended Posts

be17_17y    128
Hallo, i want to ask a few question about moving object in directx + vc++. I use math deviding magnitude or distance (vector) to move my object to destinition pointed. dx=my_objextX - destX dy=my_objectY - destX mag = sqrt(dx*dx + dy*dy) new_posx = my_objectX - (dx/mag * speed) new_posy = my_objectY - (dy/mag * speed) my object are move to the direction i give, but sometimes in the point i'm give the math formula get no exactly result that make my object keep move near point and never stop. is there any math/formula or algorithm you can share to me ?.. please.. :) Second question, in case i used math above to move my object, how can i make if something (object) are in the way, my object will avoid it but keep moving to the last point, any suggestion ? "point" in here i mean where the object will move to. Sorry for my very bad english, Thank You

Share this post


Link to post
Share on other sites
Genjix    100
im not sure exactly what you want but is this what you mean?

#ifndef ENTITYCONTROLLER_H
#define ENTITYCONTROLLER_H

#include "point.h"

/** handles positioning and moving of a game entity */
class EntityController
{
public:
EntityController();
EntityController(const Point &pos , const Point &way , int s);
EntityController(const EntityController &entitycontroller);
~EntityController();

void operator=(const EntityController &entitycontroller);

void Move(const Point &p);

void Update();
void Draw();

void NewSpeed(int s);
void NewWayPoint(const Point &p);

const Point &Position();
const Point &WayPoint();

private:

Point position , waypoint;
int speed;
};

#endif





#include "entitycontroller.h"

int ComputeNextStep(int a , int b , int step);

EntityController::EntityController()
{
position = Point(0,0);
waypoint = Point(0,0);
speed = 0;
}
EntityController::EntityController(const Point &pos , const Point &way , int s)
{
position = pos;
waypoint = way;
speed = s;
}
EntityController::EntityController(const EntityController &entitycontroller)
{
operator=(entitycontroller);
}
EntityController::~EntityController()
{
}

void EntityController::operator=(const EntityController &entitycontroller)
{
position = entitycontroller.position;
waypoint = entitycontroller.waypoint;
speed = entitycontroller.speed;
}

void EntityController::Move(const Point &p)
{
if(p.x > 0)
waypoint.x = position.x + speed;
else if(p.x < 0)
waypoint.x = position.x - speed;
else
waypoint.x = 0;

if(p.y > 0)
waypoint.y = position.y + speed;
else if(p.y < 0)
waypoint.y = position.y - speed;
else
waypoint.y = 0;
}

void EntityController::Update()
{
if(position.x != waypoint.x)
position.x += ComputeNextStep(position.x , waypoint.x , speed);
if(position.y != waypoint.y)
position.y += ComputeNextStep(position.y , waypoint.y , speed);
}
void EntityController::Draw()
{
output << "drawing " << position << "\t" << waypoint << "\n";
}

void EntityController::NewSpeed(int s)
{
speed = s;
}
void EntityController::NewWayPoint(const Point &p)
{
waypoint = p;
}

const Point &EntityController::Position()
{
return position;
}
const Point &EntityController::WayPoint()
{
return waypoint;
}





int ComputeNextStep(int a , int b , int step)
{
int offset = b - a;

if(offset >= 0)
step = (offset > step) ? step : offset;
else
step = (offset < step) ? -step : offset;

return step;
}



(sorry but I couldn't make out what you was saying exactly).

Share this post


Link to post
Share on other sites
Illco    928
If the problem is that the object never precisely reaches the point, then include a small margin in your check; for example:

const float fEpsilon = 10e-10;

if ( fabs( Length( vPosition - vTargetPosition ) ) < fEpsilon )
{
// Reached the goal well enough
}
else
{
// No keep moving
}


Greetz,

Illco

Share this post


Link to post
Share on other sites
Genjix    100
oh, shaking... yes if you look at ComputeNextStep(int a , int b , int step) is another method of interpolating between 2 points in a fixed step without 'over-stepping' point b (then moving back again to accomadate this.

Share this post


Link to post
Share on other sites
be17_17y    128
Thanks, and how about avoiding thing above... ?
example : my character moving but there are object tree in front of him, and he change route so he will not smash the tree.

Share this post


Link to post
Share on other sites
TeMPOraL    136
Your object must store it's own path, and when you want it to avoid an obstacle, simply alter the path by adding waypoints that go around the obstacle.

A path is usually a list of waypoints, ie:
std::list<CWayPoint> path;
//---
class CWayPoint
{
public:
Vector2 position;
//other stuff
}


Your object follows all points stored in the path. Your pathfinding algorithm needs to find a group of points that will lead around obstacles, and add them to the path.

I'm not good in any pathfinding algorithms, so I won't write any example ;)

Share this post


Link to post
Share on other sites
Genjix    100
Quote:
Original post by be17_17y
Thanks, and how about avoiding thing above... ?
example : my character moving but there are object tree in front of him, and he change route so he will not smash the tree.


I'm not an AI guy, but I believe you should ask in the AI forum about the A* algorithm and resources (if they flame you blame it on me).

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