Can some1 explain me this Problem!

Provide the implementation of the Update function that moves the object from his position to the destination with a speed of 2 meters per second.

1
2
3
4
5
6
7
8
9
10
11
12
13
 struct Point
    {
        float x;
        float y;
    }

    class Object
    {
        Point position;
        Point destination;

        void Update(float dt);
    }


dt = time in seconds
Last edited on
Help !
Up!
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cmath>

struct point { double x = 0 ; double y = 0 ; };

std::ostream& operator<< ( std::ostream& stm, point p )
{ return stm << '(' << p.x << ',' << p.y << ')' ; }

double euclidean_distance( point a, point b )
{
    const double dx = a.x - b.x ;
    const double dy = a.y - b.y ;
    return std::sqrt( dx*dx + dy*dy ) ;
}

struct object
{
    point current_position ; // co-ordinates are in metres

    // return time taken (in seconds) to move to the destination
    // assumes that the object can move in the straight-line path to destination
    double move_to( point destination ) // co-ordinates are in metres
    {
        const double distance = euclidean_distance( current_position, destination ) ;
        current_position = destination ; // move to destination: update the current position
        return distance / velocity ; // return the time taken to move
    }

    static constexpr double velocity = 2.0 ; // metres per second
};

int main()
{
    object x ;

    for( point destination : { point{1,2}, point{24,6}, point{9,22}, point{0,0} } )
    {
        std::cout << "moved from " << x.current_position << " to " << destination ;
        std::cout << " in " << x.move_to(destination) << " seconds.\n" ;
    }
}

Last edited on
help me please.. my code keeps giving me this error about expected initializer and i don't know how to make it stop..


# include <iostream>
# include <string>

using namespace std;
struct gradeBook {
string Name;
float grade1, grade2;
string lGrade2;
}

st[3]
void gradeclc(gradeBook st)

int main()
{
float avg;
avg=(st. grade1 + st grade2)/2;
if (avg>50)
{
st.lGrade="Pass";
}
else
{
st lGrade="fail";
}
return st lGrade;
}
void printmsg(gradeBook st, string F)
{
cout<<st.Name<<"\t\t"<<st.grade1<<"\t\t"
<<st.grade<<"\t\t"<<F<<endl;
}

for(int i=0, i<3, i++);
cout<<"Enter name of student "<<i+1;
cin>>>st[i].sName;
cout<<"Enter grades of students"i+1;
cin>>st[i] grade;
cout<<endl
cin>>st[i].grade2;
}
cout<<"student\t\t Grade_1\t\t Grade.\t\t status\n";
for int i=0; i<3, i++;
{
printmsg(st[i] grade clc (st [i]);
}
any1 with more simple solution pls help!
Since you seem desperate for an answer, I'd advise you to use code tags to make your code more readable:

http://www.cplusplus.com/articles/jEywvCM9/

The easier it is for people to read your code, the more likely they are to look at it and figure out the problem.

Also, it helps to be more specific about the error message you're getting. In particularly, the full message will include the line number on which the error is occurring. Giving us the full information that your compiler is giving you, will make it easier for us to help you.
simple solution? some1?
What is it that you find hard to understand in this code?
http://www.cplusplus.com/forum/general/209791/#msg985953
lets pretend that point 1 is 0,0 in the xy plane.
then x = x2-x1 is how far you plan to move in x, either + or - (left/ right) from where you are now.

t = x / velocity is how long it will take you to get there.

at any given time, your x position then is time*(x/t), right?
or
current = x1 + time*(x/t);

if you do the same in y, you effectively ride the slope, rise over run per time unit, at any give time you are on the line between the two.

Hopefully I got that right. Ill double check it in an hour or two when I get time and a piece of paper handy.

seems ok.
so now just loop...
for(dt = 0; dt < t; dt+= increment)
{
currentx = x1 + time*(x/t);
currenty = y1 + time*(y/t);
}

oh, and if its not obvious by now, dist = 1/2att+vot but you don't have any accelerations. so current dist = velocity * current time.


Last edited on
JLBorges, your code gives the time needed to move from A to B, but the OP's function should give the distance walked in a specific amount of time.


compute the (B-A) vector, normalise it and multiply it by the velocity.
that will give you the delta that you'll move, so multiply it by the time and add it to the current position.
As I said, you don't need even that much.
because you have the velocity, all you need is D = VT if you just want the distance. With no accelerations, its that simple. You are re-computing V, but it was a given :) Here, V is 2 (provided) and T is any time > 0 (start).

Last edited on
you have |v|, not v.
you know how fast you are going, but not in what direction.

given the destination point you need to compute v_x and v_y
Last edited on
The direction is irrelevant to the total distance at any given time. You are out in space ... moving 10 km/sec. How far do you go in 10 seconds? The answer is the same no matter what direction you are going.

You need the components to get the current position at a time, though. That is a different question, which I also answered, cheating a little to get there, but it works.


Last edited on
ah, my mistake. don't know why I said that it should give the distance walked, it should give the position after an specific t.


By the way, in your code you don't use `dt' inside the loop, just compte the same operations several times.
this is true, i called it time, its sort of pseudo code. the important thing was the technique to boil it down to a simple iteration without any distance formula. It certainly won't compile or anything like that.

time should have been dt, I suppose.

Last edited on
Topic archived. No new replies allowed.