Hey dudes and dudettes.
High-school Comp Sci student reporting in.
Right now we're doing some work with plotting and polygons, and one of the challenges is to make it move across the screen in a variety of ways. (left to right, etc.) See, this would be no issue if I knew the language or had the means to study it at a basic level. But our teacher just hands us some environment and some code to copy and paste and look off of, none of which really helps in the long run, except for the most basic stuff.
So, my friends, I can make a polygon through plotting points as shown here:
int xPoints[]={ xP*684/1000,xP*706/1000, xP*661/1000, xP*687/1000,
xP*735/1000,xP*760/1000, xP*723/1000, xP*713/1000,
xP*698/1000,xP*686/1000, xP*669/1000, xP*653/1000,
xP*639/1000,xP*639/1000, xP*641/1000, xP*648/1000};
int yPoints[]={ yP*354/1000, yP*354/1000, yP*472/1000, yP*472/1000,
yP*354/1000, yP*354/1000, yP*452/1000, yP*471/1000,
yP*487/1000, yP*498/1000, yP*503/1000, yP*504/1000,
yP*492/1000, yP*473/1000, yP*455/1000, yP*440/1000};
int numPoints=16;
But I cannot figure out for the life of me what kind of specific input or code is required to make it move. (by that I mean translate across the the screen)
Any hints to get me on the right track would be greatly appreciated. I don't exactly have the means of figuring this out on my own.
Edit: Whoops, yep it's Java.
Welcome to SO! You need to break this down into small parts that you need to figure out how to do. Some that come to mind are
Create a window to draw in.
Draw a line in that window.
Draw a polygon in the window.
Move a point from (x, y) in a given direction.
Each of these can likely be broken down even further. Often solving a programming problem starts with this kind of thinking. As you try to do each of these, and any others that you come up with, please feel free to come back and ask more questions.
I don't know IBM VisualAge but if you have any draw method/loop, you should increase xP to move to right, decrease it to move left etc. with a for loop, for example.
Maybe a for loop?
for(int i=0;i<xPoints.length;i++)
xPoints[i]+=1;// shift 1 unit
for(int i=0;i<yPoints.length;i++)
yPoints[i]+=1;// shift 1 unit
Assuming that it's a java.awt.Polygon, have you looked at
Polygon.translate(int dx, int dy); ???
If it is some other kind of Polygon, check to see if it has a similar method.
Related
I wanted to make a circle that has a ripple-effect on the edges, kind of like in the game agar.io. I am kind of lost on how to implement it. Obviously I can't just g.fillOval() because that would draw a solid circle with no movement on the edges.
I'm not asking anyone to write any code for me (but if you really want to, I don't mind :D), but if you could point me in the right direction with some methods I should use. I am using Slick-2D library for java, if that helps.
I also tried analyzing the javascript source from the agar.io website to try to understand how they implemented it in javascript, but I was unsuccessful because the code was obfuscated; all the methods and variables were just single letters.
The only way I can imagine doing this currently is to have each circle be composed of a number of points, and let each point have it's own physics, and it can be affected by other points. If anyone who has insight into this problem, I would greatly appreciate some help. Thank you!
I'm not sure you can do this with Slick2D. It is quite high level and gives a lot built-in classes. What you want to do is really specific. As Slick development has stopped you will not get new features. You should probably look at lwjgl which is the base of Slick. It is more low-level but can be more precise with the form you need.
You can look at this project to have some drawing cool stuff. And for another example of manipulating circle you have this one
I am developing an AI simulation of predator and prey. I would like to simulate the AI hiding behind obstacles, if it is being chased. But I am still trying to figure out the best way to implement this.
I was thinking along the lines of checking on which side of the obstacle the predator is on and trying to go on the opposite side. Maybe using the A* path finding algorithm to ensure that it gets there using the shortest path.
Now the main reason I am writing is in case somebody is able to point me in the right direction of implementing this (maybe somebody has done this before) or have any other good ideas how to implement it. I have never done anything like this before in terms of programming AI or making any game.
All the obstacles are either horizontal or vertical squares/rectangles.
Please note that the circle in red is the predator while the circle in green is the prey being chased.
I can't give any code off the top of my head, but I can tell you a few things:
First, you need to define the goal of this program. For this case, it is to get the AI to hide behind an obstacle, keeping the user and the AI on opposite sides whenever possible.
Next, you need to decide what needs to be done inside the code (without writing any real code) to accomplish this goal. For instance:
We need to determine what "zone" of the scene is considered "behind the obstacle"
Next, we need to determine a path for the AI to get to that zone without going through the obstacle.
Lastly, we need to add some sort of delay to this so the AI doesn't constantly change its mind for every pixel the user moves across the screen
This isn't per se an easy problem, but it is certainly achievable without breaking too much of a sweat. I'd recommend you find a way, even if it is slow and requires a ton of code, then write the code for it, and lastly refine. If you worry about refinement, then you never get any of the problem solved.
HINT: Determine a vector that points from the player to the middle of the obstacle. Then, multiply the vector by 2 and add it to the position of the player and that gives you a point on the other side of the obstacle (assuming it is a rectangle). Apply a Math.min() or Math.max() restrictor to the x and y values you get to keep the AI as close or far from the obstacle as you wish. That should be a decent start! :)
Update -- I decided to add some code!
// This assumes a few variables:
int obstacleCenterX, obstacleCenterY;
int aiX, aiY, aiWalkSpeed;
int predatorX, predatorY;
private void updateAIMovement() {
int slope_x = obstacleCenterX - predatorX;
int slope_y = obstacleCenterY - predatorY;
int destination_x = predatorX + (slope_x * 2);
int destination_y = predatorY + (slope_y * 2);
if(aiX != destination_x){
aiX += (slope_x / Math.abs(slope_x)) * aiWalkSpeed;
}
if(aiY != destination_y){
aiY += (slope_y / Math.abs(slope_y)) * aiWalkSpeed;
}
}
I have not tested anything at all, but I think this might be somewhat of a right path to take. I could have done A LOT to improve just that little code snippet, but I didn't (such as some trig to make sure the player moves at a true speed when going diagonally, etc...)
Hopefully this helps a little!
I would check if there is something crossing the direct line between x and each observer. If it is, x is hidden.
First of all my english is rubbish, so please understand.
I have been asked to do a little simulation/animation in java.
I have a few cog on board one of the cogs rotates all the time on its axis. when I move any other cog close to the rotating one it has to start rotating and teeth can not overlap each other.
I think this link describes it quite well
http://www.csharphelper.com/howto_animate_gear.gif
I don't know how to make this cogs interact together, i will be really happy if some can show some code example
Many thanks for any help
If you know how to rotate the kog, you could cheat a bit, and hard-code the timing so that they rotate at the right time :P
This is a classic LCM (Least Common Multiple) problem: try to formulate a solution from the dimensions (radius or diameter or circumference) of the cogs.
I'm currently working on my 2D game project (Java), but so far any kind of game logic or AI has been crudely implemented. For instance, say I need to randomly position a bunch of sprites a long the top of the screen, I'd be using the Random class to do this. I'd simply use Random.nextInt( size of x axis on which to spawn ); Although this does work I'd be interested to hear how I should really be going about this kind of thing.
As a second scenario (this is why I put AI in the title, although it's not really AI), say I want to have my characters randomly blink in a life-like fashion. What I'd do here is use the Random class to calculate a % (say 20% chance) of blinking and call it every second.
Any suggestions on how I should really be going about this would be greatly appreciated.
Google for a paper titled "Steering Behaviors" by Craig Reynolds. It address just this and you'll find great ideas to start with specifically some nice ideas for giving groups of sprites the appearance of 'intelligent' movement. The key for him in his different behaviors, i.e. flocking, etc. is making properties of any given sprite dependent on those of some other sprite. You could even go so far as to say, like -- any given sprite will blink only if it's two neighbors just have blinked. Something or other along those lines.
Hope this helps!
Are you using an OOP (Object-Oriented approach)? If not, you should definitely look into it. It's really simple with java and can speed up your development time and neaten your code.
I would make a sprite class, and give them a function, say actionSpawn, or actionMove (I like to start my "action" functions with the word action so they are easily identifiable). In this function you would encapsulate the Random.nextInt function, to set the sprite's x and/or y position.
You could use the same approach to make them blink.
I have just started learning basics about Graphics2D class, So far I am able to draw different objects and implements ActionListener to actually move them on screen by onKeyPress. So far so good, While I thought of doing something more complicated. I want to give a path to my object and animate it on that particular path only.
Something like, I will draw a line on sky and a plane should stick with that drawn line and keep it self to fly on that particular line. Now is it possible?
I don't need any sort of code, But few different method or ideas will let me get started working on this. A visualise elaboration of my idea is as below.
Start Point :
End Point :
Now as shown above, my yellow box(in future plane) should stick with given path while animating(path grey line)
My research so far,
I have searched my buzz words such as path in java, and found Path2D and GeneralPath classes, Does anyone know if I can use that to solve this.
Thanks
Great !
It reminds me of my first steps in IT. How much I enjoyed all this simple math stuff but that make things move on screen. :)
What you need is actually a linear interpolation . There are other sorts of interpolation and some api offer a nice encapsulation for the concept but here is the main idea, and you will quite often need this stuff :
you must rewrite your path
y = f (x )
as a function of time :
at time 0 the item will be on start position, at time 1 it will reach the end. And then you increment time (t) as you wish (0.001 every ms for instance).
So here is the formula for a simple linear path :
x = xstart + (xend-xstart) * t
y = ystart + (yend-ystart) * t
when t varies, you object will just move linearly along the path, linearly has speed will be constant on all the path. You could imagine some kind of gravtity attraction at end for instance, this would be modeled by a quadratic acceleration (t^2 instead of t) ...
Regards,
Stephane
First, make the ability to move from point a to point b. This is done with simple algebra.
Second, make the ability to take a path and translate it into points. Then when you're going to do curves, you're really just moving from point to point to point along that curve.
This is the most elementary way to do it and works for most instances.
What your talking about is simple 2D graphics and sprites. If that is all you need then for Java take a look at Java 2D Sprites If your leaning more towards or will eventually go with camera perspectives and wanting to view the animation from diferent angles the go with Java 3D from the OpenSource Java 3D.org. Either way what you want is a simple translating of the object along a line, pretty simple in either 2D or 3D.
You can try going trough the code of my open source college project - LANSim. It's code is available in Code menu. It does similar to what you are trying to do.