C# - Moving pictureboxes towards to another picturebox (NPC logic and Vectors)









up vote
0
down vote

favorite












I'm trying to move pictureboxes(NPCs) automatically towards to main character(pictureBox1), basically I want them to chase pB1. I'm very new to vectors. I couldn't understand the logic very well. First, let me show the codes.



 int pictx,picty,mcx,mcy;

private void Monster_drag_Tick(object sender, EventArgs e)

foreach (var pict in this.Controls.OfType<PictureBox>())

pictx = pict.Location.X;
picty = pict.Location.Y;
mcx = pictureBox1.Location.X;
mcy = pictureBox1.Location.Y;
if (pict.Name != "pictureBox1")

ChaseVector(mcx, mcy, 0.5f);





This was the timer event. Interval is 1000 by the way. Also I have a Struct that automatically creates pictureboxes. I'm pulling all pictureboxes that created with Struct by foreach as you can see.



And here is the Vector method;



public void ChaseVector(int x, int y, float speed)

float tx = x - pictx;
float ty = y - picty;
double length = Math.Sqrt(tx * tx + ty * ty);
if (length < speed)

// move towards the goal
pictx = (int)(pictx + speed * tx / length);
picty = (int)(picty + speed * ty / length);
foreach (var pict in this.Controls.OfType<PictureBox>())

if (pict.Name != "pictureBox1")

pict.Location = new Point(pictx, picty);



else

// already there
pictx = x;
picty = y;
foreach (var pict in this.Controls.OfType<PictureBox>())

if (pict.Name != "pictureBox1")

pict.Location = new Point(pictx, picty);







I found this code on the stackoverflow. I really don't know anything about vectors. This code makes pictureboxes jump to main character. Flash them basically. I want them to chase mc slowly, like walking I mean. As if there is no speed, they are just jumping over the mc every time I move the character.



Waiting for your helps and if there is a better way to use Vectors please tell. Thanks in advance.










share|improve this question





















  • The first thing you need to do is go read up on Vector movement, Google "Vector movement". Just grabbing some code without understanding it is a disservice to yourself. IMHO
    – Sorceri
    Nov 9 at 22:14










  • Then use a loop to run the piturebox' location until it goes to the destination.
    – TerribleDog
    Nov 10 at 0:57











  • How? and what do you mean by running the piturebox' location?@TerribleDog
    – Yunus Derici
    Nov 10 at 3:58














up vote
0
down vote

favorite












I'm trying to move pictureboxes(NPCs) automatically towards to main character(pictureBox1), basically I want them to chase pB1. I'm very new to vectors. I couldn't understand the logic very well. First, let me show the codes.



 int pictx,picty,mcx,mcy;

private void Monster_drag_Tick(object sender, EventArgs e)

foreach (var pict in this.Controls.OfType<PictureBox>())

pictx = pict.Location.X;
picty = pict.Location.Y;
mcx = pictureBox1.Location.X;
mcy = pictureBox1.Location.Y;
if (pict.Name != "pictureBox1")

ChaseVector(mcx, mcy, 0.5f);





This was the timer event. Interval is 1000 by the way. Also I have a Struct that automatically creates pictureboxes. I'm pulling all pictureboxes that created with Struct by foreach as you can see.



And here is the Vector method;



public void ChaseVector(int x, int y, float speed)

float tx = x - pictx;
float ty = y - picty;
double length = Math.Sqrt(tx * tx + ty * ty);
if (length < speed)

// move towards the goal
pictx = (int)(pictx + speed * tx / length);
picty = (int)(picty + speed * ty / length);
foreach (var pict in this.Controls.OfType<PictureBox>())

if (pict.Name != "pictureBox1")

pict.Location = new Point(pictx, picty);



else

// already there
pictx = x;
picty = y;
foreach (var pict in this.Controls.OfType<PictureBox>())

if (pict.Name != "pictureBox1")

pict.Location = new Point(pictx, picty);







I found this code on the stackoverflow. I really don't know anything about vectors. This code makes pictureboxes jump to main character. Flash them basically. I want them to chase mc slowly, like walking I mean. As if there is no speed, they are just jumping over the mc every time I move the character.



Waiting for your helps and if there is a better way to use Vectors please tell. Thanks in advance.










share|improve this question





















  • The first thing you need to do is go read up on Vector movement, Google "Vector movement". Just grabbing some code without understanding it is a disservice to yourself. IMHO
    – Sorceri
    Nov 9 at 22:14










  • Then use a loop to run the piturebox' location until it goes to the destination.
    – TerribleDog
    Nov 10 at 0:57











  • How? and what do you mean by running the piturebox' location?@TerribleDog
    – Yunus Derici
    Nov 10 at 3:58












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to move pictureboxes(NPCs) automatically towards to main character(pictureBox1), basically I want them to chase pB1. I'm very new to vectors. I couldn't understand the logic very well. First, let me show the codes.



 int pictx,picty,mcx,mcy;

private void Monster_drag_Tick(object sender, EventArgs e)

foreach (var pict in this.Controls.OfType<PictureBox>())

pictx = pict.Location.X;
picty = pict.Location.Y;
mcx = pictureBox1.Location.X;
mcy = pictureBox1.Location.Y;
if (pict.Name != "pictureBox1")

ChaseVector(mcx, mcy, 0.5f);





This was the timer event. Interval is 1000 by the way. Also I have a Struct that automatically creates pictureboxes. I'm pulling all pictureboxes that created with Struct by foreach as you can see.



And here is the Vector method;



public void ChaseVector(int x, int y, float speed)

float tx = x - pictx;
float ty = y - picty;
double length = Math.Sqrt(tx * tx + ty * ty);
if (length < speed)

// move towards the goal
pictx = (int)(pictx + speed * tx / length);
picty = (int)(picty + speed * ty / length);
foreach (var pict in this.Controls.OfType<PictureBox>())

if (pict.Name != "pictureBox1")

pict.Location = new Point(pictx, picty);



else

// already there
pictx = x;
picty = y;
foreach (var pict in this.Controls.OfType<PictureBox>())

if (pict.Name != "pictureBox1")

pict.Location = new Point(pictx, picty);







I found this code on the stackoverflow. I really don't know anything about vectors. This code makes pictureboxes jump to main character. Flash them basically. I want them to chase mc slowly, like walking I mean. As if there is no speed, they are just jumping over the mc every time I move the character.



Waiting for your helps and if there is a better way to use Vectors please tell. Thanks in advance.










share|improve this question













I'm trying to move pictureboxes(NPCs) automatically towards to main character(pictureBox1), basically I want them to chase pB1. I'm very new to vectors. I couldn't understand the logic very well. First, let me show the codes.



 int pictx,picty,mcx,mcy;

private void Monster_drag_Tick(object sender, EventArgs e)

foreach (var pict in this.Controls.OfType<PictureBox>())

pictx = pict.Location.X;
picty = pict.Location.Y;
mcx = pictureBox1.Location.X;
mcy = pictureBox1.Location.Y;
if (pict.Name != "pictureBox1")

ChaseVector(mcx, mcy, 0.5f);





This was the timer event. Interval is 1000 by the way. Also I have a Struct that automatically creates pictureboxes. I'm pulling all pictureboxes that created with Struct by foreach as you can see.



And here is the Vector method;



public void ChaseVector(int x, int y, float speed)

float tx = x - pictx;
float ty = y - picty;
double length = Math.Sqrt(tx * tx + ty * ty);
if (length < speed)

// move towards the goal
pictx = (int)(pictx + speed * tx / length);
picty = (int)(picty + speed * ty / length);
foreach (var pict in this.Controls.OfType<PictureBox>())

if (pict.Name != "pictureBox1")

pict.Location = new Point(pictx, picty);



else

// already there
pictx = x;
picty = y;
foreach (var pict in this.Controls.OfType<PictureBox>())

if (pict.Name != "pictureBox1")

pict.Location = new Point(pictx, picty);







I found this code on the stackoverflow. I really don't know anything about vectors. This code makes pictureboxes jump to main character. Flash them basically. I want them to chase mc slowly, like walking I mean. As if there is no speed, they are just jumping over the mc every time I move the character.



Waiting for your helps and if there is a better way to use Vectors please tell. Thanks in advance.







c# vector game-physics picturebox






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 21:37









Yunus Derici

205




205











  • The first thing you need to do is go read up on Vector movement, Google "Vector movement". Just grabbing some code without understanding it is a disservice to yourself. IMHO
    – Sorceri
    Nov 9 at 22:14










  • Then use a loop to run the piturebox' location until it goes to the destination.
    – TerribleDog
    Nov 10 at 0:57











  • How? and what do you mean by running the piturebox' location?@TerribleDog
    – Yunus Derici
    Nov 10 at 3:58
















  • The first thing you need to do is go read up on Vector movement, Google "Vector movement". Just grabbing some code without understanding it is a disservice to yourself. IMHO
    – Sorceri
    Nov 9 at 22:14










  • Then use a loop to run the piturebox' location until it goes to the destination.
    – TerribleDog
    Nov 10 at 0:57











  • How? and what do you mean by running the piturebox' location?@TerribleDog
    – Yunus Derici
    Nov 10 at 3:58















The first thing you need to do is go read up on Vector movement, Google "Vector movement". Just grabbing some code without understanding it is a disservice to yourself. IMHO
– Sorceri
Nov 9 at 22:14




The first thing you need to do is go read up on Vector movement, Google "Vector movement". Just grabbing some code without understanding it is a disservice to yourself. IMHO
– Sorceri
Nov 9 at 22:14












Then use a loop to run the piturebox' location until it goes to the destination.
– TerribleDog
Nov 10 at 0:57





Then use a loop to run the piturebox' location until it goes to the destination.
– TerribleDog
Nov 10 at 0:57













How? and what do you mean by running the piturebox' location?@TerribleDog
– Yunus Derici
Nov 10 at 3:58




How? and what do you mean by running the piturebox' location?@TerribleDog
– Yunus Derici
Nov 10 at 3:58

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233564%2fc-sharp-moving-pictureboxes-towards-to-another-picturebox-npc-logic-and-vecto%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233564%2fc-sharp-moving-pictureboxes-towards-to-another-picturebox-npc-logic-and-vecto%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo