#best way of handling 2d movement?

1 messages · Page 1 of 1 (latest)

wooden lotus
#

in my last post i was looking for a way to fix issues i was having with implementing acceleration but i just don’t think i’m capable of fixing it with the movement system i’ve made so i’m going looking to remake it, i was using a 2d array the same dimensions as my window and just using either a 1 or 0 to decide whether an object would be drawn at a specific coordinate or not but i run into a lot of issues with out of bounds errors which i managed to sort but now have a strange bug which neither me nor chat gpt can fix 😂 , what is a simpler way of handling this movement?

coarse condorBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

stem I currently have. Can you provide more details about the specific issues you are facing with implementing acceleration in your 2D movement? This will help me provide a more tailored solution for your problem.

winged steppe
#

isnt it just storing:

  • position (x, y)
  • velocity (in x and y direction)
  • accelaration (in x and y direction)
    and then just update these all the time?
wooden lotus
winged steppe
#

there is no need for an array

#

why do you have an array in the first place?

wooden lotus
winged steppe
#

that doesnt explain why you have an array

#

do you have multiple objects?

wooden lotus
#

sorry i should’ve explained better yeh basically i’m working on a sand physics simulator and each grain an an object and i need to check each of there locations in relation to the other objects

red ivy
winged steppe
#

make a class

#

also you might get better help if you are sharing your actual code

wooden lotus
#

this is the class im currently using, im using libgdx for rendering

winged steppe
#

where is the gist message?

coarse condorBOT
winged steppe
#

ah so you say each pixel is a grain of sand?

#

so I would do it differently in the first place

#

I would create a class that represents a grain of sand

#

it has properties like:

  • position (x, y)
  • velocity
  • acceleration
#

and then you store a List<Sand> or smth

#

and handle the physics using that list

#

for drawing you just iterate through the list and draw them at their stored position

#

then you will need an update method to update all of their properties

wooden lotus
#

ahhh yeh that’s probably better lol

#

thank you i’m gonna try that now and il update if i get it working 👍

coarse condorBOT
#

@wooden lotus

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

wooden lotus
#

ive run into an issue again lol, sorry this took a while i was just trying to learn how to use arraylist and ended up nesting an array to store my values but ive got abit of an issue with rendering, im trying to increment through the array list and draw all the objects but it only seems to draw the most recent index, im not super familiar still with lists so it may be something trivial but ive been stuck on this for about an hour and a half.

coarse condorBOT
wooden lotus
winged steppe
#

your Grain class is pretty bad in the first place

#

a Grain should not store all grains itself using arrays, but instead should store x, y, velX, velY, accX, accY and then you later create a List<Grain> instead

ruby harbor
#

OOP is a basic abstraction for the outside world

#

I you call something a Grain, it should be a Grain, not a list of grains

#

That's a different abstraction

coarse condorBOT
#

SOLID is an acronym that stands for five principles of object-oriented programming and design. These principles are guidelines that help developers create software that is easy to understand, maintain, and extend. The five SOLID principles are:

  1. Single Responsibility Principle (SRP): A class should have only one reason to change. It means that a class should have only one responsibility or job and should not be responsible for multiple unrelated tasks.

  2. Open/Closed Principle (OCP): Software entities (classes, modules, functions) should be open for extension but closed for modification. This principle encourages the use of abstraction and inheritance to allow new functionality to be added without modifying existing code.

  3. Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types without affecting the correctness of the program. In other words, if a program is using a base class, it should be able to use any of its derived classes without knowing it.

  4. Interface Segregation Principle (ISP): Clients should not be forced to depend on interfaces they do not use. This principle states that interfaces should be specific and focused on the needs of the clients using them, rather than being large and general-purpose.

  5. Dependency Inversion Principle (DIP): High-level modules/classes should not depend on low-level modules/classes directly; both should depend on abstractions. This principle promotes loose coupling between classes by introducing abstractions and relying on dependency injection.

By following these SOLID principles, developers can create code that is more modular, flexible, and maintainable, making it easier to adapt to changing requirements or add new features without introducing bugs or breaking existing functionality.

ruby harbor
#

SOLID might be a bit much for this skill level tbh

#

But there are good books if you want to dive deeper

pale bone
#

Not something to directly focus on, but in this case S applies

ruby harbor
#

The S for solid is quite contentious, this would just be the basics

pale bone
#

Also random remark, but I'd like to see a coordinate record, that would make your constructor more descriptive.

wooden lotus
#

and yeh my class for grain is abit all over the place as i was kinda teaching myself as i was making it

pale bone
#

record Coordinate ( double x , double y ) {}

winged steppe
#

I would call it Position

#

and then a record for Velocity and Acceleration as well

#

but

#

probably not

#

all of them store x and y

#

but for different purposes

#

basically a vector peepo_think

wooden lotus
#

sorry am i replacing anything with the record code or just running it along with the rest of my program 😅

winged steppe
#

also I doubt a record makes sense here actually

#

depending on how you use it

#

because you cant set in a record

#

they are all final values

wooden lotus
#

oh okay

winged steppe
#

you could still use them to make your constructor better

pale bone
#

Or a Coordinate/position class. But I'd say you'd give an object a new position, and not change it. Since you're moving an object in the world and telling it it's new position.

wooden lotus
#

i fixed it lol i was clearing the screen after rendering an object so it would wipe all the previous objects

#

side note but has anyone got any course recommendations, i don't mind if its paid or like a yt course i just know that self teaching this isn't going to be as beneficial to me in the long run

coarse condorBOT
#

MOOC is a completely free introductory Java course created by the University of Helsinki, it is a great way to learn Java from the ground up.

It consists of two parts, one at beginner, and another at intermediate level. The end of the course is marked by creating your own Asteroids game clone!

Even though the instructions show how to configure and use NetBeans for the course, you can use IntelliJ. To use IntelliJ, simply install the TMC plugin by opening IntelliJ -> File -> Settings -> Plugins and searching for TMC. You will then be able to use IntelliJ to complete MOOC.

Visit MOOC here: https://java-programming.mooc.fi/
(the course is available in both English and Finnish)

About the course - Java Programming

pale bone
#

And if you want a book: head first java

wooden lotus