# Movement math ?

This topic is 4943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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 on other sites
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 on other sites
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 on other sites
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 on other sites
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 on other sites
Quote:
 Original post by be17_17yThanks, 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).

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633724
• Total Posts
3013556
×