মূল বিষয়বস্তু
কম্পিউটার প্রোগ্রামিং
Course: কম্পিউটার প্রোগ্রামিং > Unit 5
Lesson 4: ভেক্টর- ভেক্টর সম্পর্কে প্রাথমিক ধারণা
- চ্যালেঞ্জ: ভেক্টর চলা
- ভেক্টর সম্পর্কিত গাণিতিক সমস্যা
- চ্যালেঞ্জ: লাইটসেবার
- ভেক্টর বিস্তার এবং নরমালাইজেশন
- চ্যালেঞ্জ: মান দৃষ্টিগোচর করা
- ভেক্টর গতি
- চ্যালেঞ্জ: ব্রেক করা গাড়ি
- স্ট্যাটিক ফাংশন বনাম উদাহরণস্বরূপ পদ্ধতি
- চ্যালেঞ্জ: স্ট্যাটিক (static) ফাংশন
- ইন্টার্যাক্টিভ ভেক্টর গতি
- চ্যালেঞ্জ: মাউস অনুসরণকারী
- প্রকল্প: কম্পিউটেশোনাল প্রাণি
© 2023 Khan Academyব্যবহারের শর্তাদিগোপনীয়তার নীতিমালাকুকি নোটিশ
ইন্টার্যাক্টিভ ভেক্টর গতি
এই অনুশীলনীটি করার জন্য, আমরা কঠিন কিন্তু উপকারী একটি অনুশীলনী অনুশীলন করবো। আমরা অ্যালগোরিদম #3 এ উল্লেখিত নিয়মগুলো মেনে — একটি অবজেক্টের (বস্তুর) ত্বরণ (acceleration) হিসাব করে বের করবো যেখানে অবজেক্টটি মাউসের দিকে ত্বরান্বিত হয়।
Anytime we want to calculate a vector based on a rule or a formula, we need to compute two things: magnitude and direction. Let’s start with direction. We know the acceleration vector should point from the object’s position towards the mouse position. Let’s say the object is located at the point
(x,y)
and the mouse at (mouseX,mouseY)
.In that diagram, we see that we can get a vector
(dx,dy)
by subtracting the object’s position from the mouse’s position.:dx = mouseX - x
dy = mouseY - y
Let’s rewrite the above using
PVector
syntax. Assuming we are in the Mover
object definition and thus have access to the object’s PVector
position, we then have:var mouse = new PVector(mouseX, mouseY);
// Look! We’re using the static sub() because we want a completely new PVector
var dir = PVector.sub(mouse, position);
We now have a
PVector
that points from the mover’s position all the way to the mouse. If the object were to actually accelerate using that vector, it would appear instantaneously at the mouse position. This does not make for good animation, of course, and what we want to do now is decide how quickly that object should accelerate toward the mouse.আমাদের ত্বরণের PVector এর মান ঠিক করার জন্য (যেটাই হক না কেন) , প্রথমেই ___ দিক নির্দেশক ভেক্টর (direction vector) নিয়ে কাজ করতে হবে। আরেকটি বিষয় আছে। নরমালাইজ (Normalize)। আমরা যদি ভেক্টরটিকে সংকুচিত করে ইউনিট ভেক্টরে (unit vector) নিয়ে আসতে পারি তাহলে আমাদের কাছে এমন একটি ভেক্টর থাকবে যেটা দিক নির্দেশ করবে এবং এর মাধ্যমে আমরা যে কোন মান পরিমাপ করতে পারবো। এক কে যে কোন সংখ্যা দিয়ে গুণ করলে গুণফল সেই সংখ্যাই হবে।
var anything = ??;
dir.normalize();
dir.mult(anything);
বিষয়টিকে আমরা যদি সংক্ষিপ্তভাবে লিখতে চাই তাহলে নিচের ধাপগুলো অনুসরণ করতে পারিঃ
- Calculate a vector that points from the object to the target position (mouse)
- Normalize that vector (reducing its length to 1)
- Scale that vector to an appropriate value (by multiplying it by some value)
- Assign that vector to acceleration
ধাপগুলো সম্পূর্ণভাবে বাস্তবায়িত হওয়ার পর প্রোগ্রামটি দেখতে অনেকটা নিম্নরূপ হবে:
You may be wondering why the circle doesn’t stop when it reaches the target. It’s important to note that the object moving has no knowledge about trying to stop at a destination; it only knows where the destination is and tries to go there as quickly as possible. Going as quickly as possible means it will inevitably overshoot the position and have to turn around, again going as quickly as possible towards the destination, overshooting it again, and so on and so forth. Stay tuned; in later sections we’ll learn how to program an object to arrive at a position (slow down on approach).
This example is remarkably close to the concept of gravitational attraction (in which the object is attracted to the mouse position). Gravitational attraction will be covered in more detail in the next section. However, one thing missing here is that the strength of gravity (magnitude of acceleration) is inversely proportional to distance. This means that the closer the object is to the mouse, the faster it accelerates.
মুভারের একটি অ্যারেসহ, উদাহরণটি কেমন হবে সেটা দেখা যাক (একটির চেয়ে বেশি)।
এই "প্রাকৃতিক সিমুলেশন" কোর্সটি নেওয়া হয়েছে Daniel Shiffman (ড্যানিয়েল শিফম্যান) এর লেখা "The Nature of Code" (কোডের প্রকৃতি) থেকে এবং এটি ক্রিয়েটিভ কমন্সের এট্রিবিউশন-নন কমার্শিয়াল 3.0 আনপোরটেড লাইসেন্সের অধিনস্ত।
আলোচনায় অংশ নিতে চাও?
কোন আলাপচারিতা নেই।