commit 903adef5948cffef23e6eba58bebaa546e6a5c77 Author: Stefano Rossi Date: Thu Jul 10 03:52:16 2025 +0200 first commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7464918 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6cb31a2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4002995 --- /dev/null +++ b/LICENSE @@ -0,0 +1,26 @@ +# DON'T BE A DICK PUBLIC LICENSE + +> Version 1.1, December 2016 + +> Copyright (C) 2024 Rossi Stefano + +Everyone is permitted to copy and distribute verbatim or modified +copies of this license document. + +> DON'T BE A DICK PUBLIC LICENSE +> TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +1. Do whatever you like with the original work, just don't be a dick. + + Being a dick includes - but is not limited to - the following instances: + + 1a. Outright copyright infringement - Don't just copy this and change the name. + 1b. Selling the unmodified original with no work done what-so-ever, that's REALLY being a dick. + 1c. Modifying the original work to contain hidden harmful content. That would make you a PROPER dick. + +2. If you become rich through modifications, related works/services, or supporting the original work, +share the love. Only a dick would make loads off this work and not buy the original work's +creator(s) a pint. + +3. Code is provided with no warranty. Using somebody else's code and bitching when it goes wrong makes +you a DONKEY dick. Fix the problem yourself. A non-dick would submit the fix back. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4fd2a65 --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +# Projectile Motion + +This is a 2D implementation of the physical formula for projectile motion. + +![img.png](img.png) + +$$ x(t) = x_0 + \frac{1}{2} a_x t^2 + v_{0x} t $$ + +Also, during the shock with the ground, the projectile will lose some energy. +This is simulated with a coefficient of restitution. + +Then, when not bouncing and only sliding on the ground, the projectile will lose energy due to friction. + +## Requirements + +- Java (6 to) 8 (max, recommended) +- NetBeans (highly recommended) + +Java Applets have been dropped with Java 9. So you need an older versione to run this project. +Java 8 is the preferred version. Until Java 6 should be ok. + +Problems running the project are expected, applets are deprecated and not supported by modern browsers.** + +This is a NetBeans Project. Using NetBeans isn't mandatory but recommended for the built-in applet runner. +You can run the GrigliaApplet.java class with netbeans. + +[NetBeans Instructions](https://netbeans.apache.org/tutorial/main/kb/docs/java/javase-jdk8/) + +Alternatively, you can use IntellijJ IDEA with the [Applet runner plugin](https://plugins.jetbrains.com/plugin/16682-applet-runner) (not tested). + +## Installation and run + +Clone the repo: + +```bash +git clone git@gitlab.com:stefanorossiti/projectiles.git +``` + +Run `AppletProiettile.java` with your preferred IDE (NetBeans is recommended) +using +java 8. + +## License + +All my work is released under [DBAD](https://www.dbad-license.org/) license. \ No newline at end of file diff --git a/applet.policy b/applet.policy new file mode 100644 index 0000000..53a9d40 --- /dev/null +++ b/applet.policy @@ -0,0 +1,3 @@ +grant { +permission java.security.AllPermission; +}; diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..2a4a06e --- /dev/null +++ b/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project proiettile. + + + diff --git a/build/AppletProiettile.html b/build/AppletProiettile.html new file mode 100644 index 0000000..3725fe7 --- /dev/null +++ b/build/AppletProiettile.html @@ -0,0 +1,28 @@ + + + Applet HTML Page + + + + + +


Applet HTML Page

+ +

+ +

+ +
Generated by NetBeans IDE + + diff --git a/build/classes/.netbeans_automatic_build b/build/classes/.netbeans_automatic_build new file mode 100644 index 0000000..e69de29 diff --git a/build/classes/.netbeans_update_resources b/build/classes/.netbeans_update_resources new file mode 100644 index 0000000..e69de29 diff --git a/build/classes/img/bgCampo.png b/build/classes/img/bgCampo.png new file mode 100644 index 0000000..df64a32 Binary files /dev/null and b/build/classes/img/bgCampo.png differ diff --git a/build/classes/img/cann.png b/build/classes/img/cann.png new file mode 100644 index 0000000..bbf4be2 Binary files /dev/null and b/build/classes/img/cann.png differ diff --git a/build/classes/img/cannEmpty.png b/build/classes/img/cannEmpty.png new file mode 100644 index 0000000..e130c60 Binary files /dev/null and b/build/classes/img/cannEmpty.png differ diff --git a/build/classes/proiettile/AppletProiettile.class b/build/classes/proiettile/AppletProiettile.class new file mode 100644 index 0000000..1042932 Binary files /dev/null and b/build/classes/proiettile/AppletProiettile.class differ diff --git a/build/classes/proiettile/ListenerProiettile.class b/build/classes/proiettile/ListenerProiettile.class new file mode 100644 index 0000000..898b670 Binary files /dev/null and b/build/classes/proiettile/ListenerProiettile.class differ diff --git a/build/classes/proiettile/Proiettile.class b/build/classes/proiettile/Proiettile.class new file mode 100644 index 0000000..a9ff90e Binary files /dev/null and b/build/classes/proiettile/Proiettile.class differ diff --git a/img.png b/img.png new file mode 100644 index 0000000..a89164a Binary files /dev/null and b/img.png differ diff --git a/manifest.mf b/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..7259431 --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1799 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set platform.home + Must set platform.bootcp + Must set platform.java + Must set platform.javac + + The J2SE Platform is not correctly set up. + Your active platform is: ${platform.active}, but the corresponding property "platforms.${platform.active}.home" is not found in the project's properties files. + Either open the project in the IDE and setup the Platform with the same name or add it manually. + For example like this: + ant -Duser.properties.file=<path_to_property_file> jar (where you put the property "platforms.${platform.active}.home" in a .properties file) + or ant -Dplatforms.${platform.active}.home=<path_to_JDK_home> jar (where no properties file is used) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + ${platform.java} -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..1c5595a --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=e815d6db +build.xml.script.CRC32=8c58e42b +build.xml.stylesheet.CRC32=f85dc8f2@1.111.0.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=e815d6db +nbproject/build-impl.xml.script.CRC32=62dc778f +nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.111.0.48 diff --git a/nbproject/private/config.properties b/nbproject/private/config.properties new file mode 100644 index 0000000..e69de29 diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties new file mode 100644 index 0000000..a8e158b --- /dev/null +++ b/nbproject/private/private.properties @@ -0,0 +1,8 @@ +compile.on.save=true +do.depend=false +do.jar=true +do.jlink=false +javac.debug=true +javadoc.preview=true +jlink.strip=false +user.properties.file=C:\\Users\\Administrator\\AppData\\Roaming\\NetBeans\\22\\build.properties diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml new file mode 100644 index 0000000..aa5c8a3 --- /dev/null +++ b/nbproject/private/private.xml @@ -0,0 +1,10 @@ + + + + + + file:/D:/Projects/Archive/proiettile/src/proiettile/AppletProiettile.java + file:/D:/Projects/Archive/proiettile/src/proiettile/ListenerProiettile.java + + + diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..28a90fd --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,89 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=proiettile +application.vendor=Administrator +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.modulepath=\ + ${run.modulepath} +debug.test.classpath=\ + ${run.test.classpath} +debug.test.modulepath=\ + ${run.test.modulepath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/proiettile.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.external.vm=false +javac.modulepath= +javac.processormodulepath= +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.modulepath=\ + ${javac.modulepath} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.html5=false +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +jlink.launcher=false +jlink.launcher.name=proiettile +main.class=proiettile.Proiettile +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=Zulu_8.0.412_8 +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs=-Djava.security.policy=applet.policy +run.modulepath=\ + ${javac.modulepath} +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +run.test.modulepath=\ + ${javac.test.modulepath} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..b93de0f --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,16 @@ + + + org.netbeans.modules.java.j2seproject + + + proiettile + + + + + + + + + + diff --git a/proiettile.iml b/proiettile.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/proiettile.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/img/cann.png b/src/img/cann.png new file mode 100644 index 0000000..bbf4be2 Binary files /dev/null and b/src/img/cann.png differ diff --git a/src/proiettile/AppletProiettile.java b/src/proiettile/AppletProiettile.java new file mode 100644 index 0000000..00b01b5 --- /dev/null +++ b/src/proiettile/AppletProiettile.java @@ -0,0 +1,187 @@ +package proiettile; + +/************************************************************ +* Autore: Rossi Stefano +* Nome: AppletProiettile +* Descrizione: simula la fisica del lancio di un proiettile +* tramite i parametri inseriti +************************************************************/ +import java.applet.Applet; +import java.awt.Graphics; +import java.awt.Image; +import java.awt.Point; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; + +public class AppletProiettile extends Applet implements Runnable, MouseMotionListener, MouseListener, ListenerProiettile{ + Thread thAgg = new Thread(this); + Image imgBuff; //dichiarazione oggetti per doppio buffering + Graphics grafBuff; + Point puntaV = new Point(); // punta del vettore velocita + Point piedeV = new Point(); // piede del vettore velocita + Point originAx = new Point(); //origine del diagramma cartesiano + double intV; + double angV; + int heightP; //altezza e larghezza del proiettile + int widthP; + Proiettile[] p = new Proiettile[62]; //max proiettili + int pCNT; //conteggio dei proiettii + double lungCampo, altCampo; //altezza e lunghezza campo in pixel + Image imgCann; + Image imgBgCamp; + + public void init(){ + this.setSize(1200, 600); + + imgBuff = createImage(1200, 600); + grafBuff = imgBuff.getGraphics(); + + imgCann = getImage(getCodeBase(), "img/cann.png"); + imgBgCamp = getImage(getCodeBase(), "img/bgCampo.png"); + //System.out.println(getCodeBase()); + + this.addMouseMotionListener(this); + this.addMouseListener(this); + + originAx.setLocation(50, 550); + + heightP = 13; + widthP = 13; + + resetV(); + + lungCampo = 1100; + altCampo = 500; + + pCNT = 0; + + //solo se il repaint e gestito dall applet + //thAgg.start(); + } + + @Override + public void update(Graphics g){ + paint(g); + } + + @Override + public void paint(Graphics g){ + //cancello tutto in grafbuff + grafBuff.clearRect(0, 0,imgBuff.getWidth(this), imgBuff.getHeight(this)); + + //disegno un quadrato per i bordi + grafBuff.drawRect((int)originAx.getX(), (int)(originAx.getY() - altCampo), (int)lungCampo, (int) altCampo); + + //disegno i proiettili rimanenti in basso + for(int i = 1; i < p.length - pCNT; i++){ + grafBuff.fillOval((int)originAx.getX() + 5*(i-1) + widthP*(i-1) + 5,(int)originAx.getY() + 8, widthP, heightP); + } + + //disegno l immagine del cannone e dello sfondo del campo + grafBuff.drawImage(imgCann, (int)originAx.getX() - imgCann.getWidth(this), (int)originAx.getY() - imgCann.getHeight(this)/2, this); + grafBuff.drawImage(imgBgCamp, (int)originAx.getX(), (int)(originAx.getY() - altCampo), this); + + //disegno i proiettile sparati nel campo + for(int i = 0; i < pCNT && i < p.length && p[i] != null; i++){ + grafBuff.fillOval((int)originAx.getX() + p[i].getX() - 5, (int)originAx.getY() - p[i].getY() - heightP, widthP, heightP); + } + //disegno il vettore V con i suoi parametri se possibile + if(piedeV.getX() != -1){ + grafBuff.drawLine((int)piedeV.getX(), (int)piedeV.getY(), (int)puntaV.getX(), (int)puntaV.getY()); + grafBuff.drawString("|V| = " + String.valueOf(Math.rint(intV*100)/100) + "px/sec", (int)(puntaV.getX()/2 - piedeV.getX()/2 + piedeV.getX()), (int)(puntaV.getY()/2 - piedeV.getY()/2 + piedeV.getY() + 10)); + grafBuff.drawString("angV = " + String.valueOf(Math.rint(Math.toDegrees(angV)*100)/100) + "°", (int)(puntaV.getX()/2 - piedeV.getX()/2 + piedeV.getX()), (int)(puntaV.getY()/2 - piedeV.getY()/2 + piedeV.getY() + 25)); + } + + g.drawImage(imgBuff, 0, 0, this); + } + + @Override + public void run(){ + while(this.thAgg.isAlive()){ + repaint(); + System.out.println("repaint"); + try{ + Thread.sleep(1000); + }catch(Exception e){ + System.out.println("thread exception"); + } + } + } + + //azzero i valori di V + private void resetV(){ + piedeV.setLocation(-1,-1); + puntaV.setLocation(-1,-1); + intV = 0; + angV = 0; + } + //in questo metodo calcolo il vettore n coordinate polari di V + private void computeV(){ + double vx = puntaV.getX() - piedeV.getX(); //componente orizzontale x di V + double vy = piedeV.getY() - puntaV.getY(); //componente orizzontale y di V x0 e in alto + + intV = Math.sqrt(vx*vx + vy*vy); + angV = Math.atan(vy/vx); //radianti + } + + @Override + public void mousePressed(MouseEvent e) { + if(e.getButton() == MouseEvent.BUTTON1){ + puntaV.setLocation(e.getPoint()); + } + } + + @Override + public void mouseDragged(MouseEvent e) { + if(e.getButton() == MouseEvent.BUTTON1){ + piedeV.setLocation(e.getPoint()); + computeV(); + repaint(); + } + } + + @Override + public void mouseReleased(MouseEvent e) { + //istanzio un nuovo proiettile e incremento la conta dei proiettili se possibile + if(pCNT < p.length){ + p[pCNT] = new Proiettile(0, 0, widthP, heightP, lungCampo, altCampo, intV, angV, 9.81, 40, 5, this); + pCNT++; + + //se ho sparato tutti i proiettili cambio l immagine del cannone con quella vuota + if(pCNT == p.length){ + imgCann = getImage(getCodeBase(), "img/cannEmpty.png"); + } + } + + //reimposto le coordinate dei punti del vettore fuori dal contesto grafico + resetV(); + repaint(); + } + + @Override + public void mouseMoved(MouseEvent e) { + //throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void mouseClicked(MouseEvent e) { + //throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void mouseEntered(MouseEvent e) { + //throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void mouseExited(MouseEvent e) { + //throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void hasMoved(Point newPos) { + //System.out.println("mosso"); + repaint(); + } +} \ No newline at end of file diff --git a/src/proiettile/ListenerProiettile.java b/src/proiettile/ListenerProiettile.java new file mode 100644 index 0000000..1dc6535 --- /dev/null +++ b/src/proiettile/ListenerProiettile.java @@ -0,0 +1,6 @@ +package proiettile; +import java.awt.Point; + +public interface ListenerProiettile{ + public void hasMoved(Point newPos); +} diff --git a/src/proiettile/Proiettile.java b/src/proiettile/Proiettile.java new file mode 100644 index 0000000..682776f --- /dev/null +++ b/src/proiettile/Proiettile.java @@ -0,0 +1,182 @@ +package proiettile; +import java.awt.Point; + +public class Proiettile implements Runnable{ + Point position; //posizione del proiettile + int widthP, heightP; + double maxX, maxY; //quanto in alto e in lungo puo andare il priettile prima di rimbalzare + private ListenerProiettile lp; + Thread t; + double intV; //la velocita del priettile in pixel al secondo + double angV; //angolo della velocita del proiettile + double g; //pixel al secondo + double atrTerreno; //attrito dinamico terreno deceleraz pixel/sec + double enrgAssPerc; //percentuale energia cinetica ssorbita dopo l impatto con una parete + + + public Proiettile(double x, double y, int widthP, int heightP, double maxX, double maxY, double vProj, double angvProj, double gravity, double enrgAssPerc, double atrTerreno, ListenerProiettile lp){ + this.lp = lp; + position = new Point((int)x, (int)y); + this.maxX = maxX; + this.maxY = maxY; + this.g = gravity; + this.widthP = widthP; + this.heightP = heightP; + + //controllo se la percentuale di energia assorbita e compresa tra 0 e 100 se no la imposto correttamente + if(enrgAssPerc >= 0 && enrgAssPerc <= 100){ + this.enrgAssPerc = enrgAssPerc/100; //percentuale in decimale + }else if(enrgAssPerc < 0){ + this.enrgAssPerc = 0; + }else{ + this.enrgAssPerc = 100; + } + + this.atrTerreno = atrTerreno; + + t = new Thread(this); + this.intV = vProj/5; + this.angV = angvProj; + t.start(); + } + + private void newPos(){ + //se la veloci del proiettie non e 0 allora calcolo la nuova posizione + if(intV != 0){ + double time = 0.1; //ricalcolo la posizione dopo 0.1 dec di secondo + + //componente x della velocita + double vx = this.intV * Math.cos(angV); + //componente y della velocita + double vy = this.intV * Math.sin(angV); + + //calcolo la nuova posizione + // MRU Sf = si + v*t + double nextX = this.position.getX() + vx * time; + // MRUA + double nextY = this.position.getY() + vy * time - 0.5 * g * time * time; + //System.out.println("vx: " + vx + " vy: " + vy); + + //salvo la nuova positione + this.position.setLocation(nextX, nextY); + + //calcolo la nuova velocita + vy = vy - g * time; //calcolo di quanto g fa diminuore la velocita verticale + + //se la pallina tocca l terra rimbalza + //se il modulo della sua velocita e minore della gravita si ferma in vy + //altrimenti controlla se la pallina tocca il cielo + if(this.position.getY() < 0 || this.position.getY() + heightP > maxY){ + vy = invertVY(vy); + + //se l intensita della velocita y e min di quanto puo essere decell da g in questo lasso di tempo viene settata a 0 esatto + if(Math.abs(vx) < g * time*time){ + vy = 0; + } + } + + //se la pallina tocca il muro destro rimbalza + //se il modulo della sua velocita e minore della gravita si ferma in vy + //altrimenti se tocca quello di sx + if(this.position.getX() + widthP > maxX || this.position.getX() < 0){ + vx = invertVX(vx); + } + + //faccio in modo di rallentare leggermente vx se necessario + vx = slowVX(vx, vy, time); + //System.out.println(vx); + + //rivalvolo angolo e intensita con la nuova vy + this.intV = Math.sqrt(vy*vy + vx*vx); + this.angV = Math.atan(vy/vx); + + //per imprecisione della trigonometria l angolo perde il segno con la tangente quindi bisogna calcolarlo manualmente + //questo vuol dire invertire il segno o diminuire/aumentare l angolo di PI radianti + if(vx < 0){ + this.angV -= Math.PI; + } + + if(vx==0 && vy == 0){ + this.intV = 0; + this.angV = 0; + } + + //System.out.println(vx); + } + } + + public int getX(){ + return (int)this.position.getX(); + } + + public int getY(){ + return (int)this.position.getY(); + } + + private double invertVX(double vx){ + //se l oggetto sta andando in avanti + if(vx > 0) { + this.position.setLocation(maxX - widthP, this.position.getY()); + }else if(vx < 0){ + this.position.setLocation(0, this.position.getY()); + } + return vx *= (-1 * (1- enrgAssPerc)); + } + + private double invertVY(double vy){ + //sel oggetto sta cadendo + if(vy < 0) { + this.position.setLocation(this.position.getX(), 0); + } + //se l oggetto sta salendo + else if (vy > 0){ + this.position.setLocation(this.position.getX(), maxY - heightP); + } + return vy *= (-1 * (1 - enrgAssPerc)); + } + + private double slowVX(double vx, double vy, double time){ + //se il proiettile tocca il bordo alto o basso + if(this.position.getY() == 0 || this.position.getY() - heightP == maxY){ + //se la velocita del poiettile e minore di quanto verra decrementata dall attrito allora si ferma + if(Math.abs(vx) < atrTerreno * time){ + vx = 0; + } + + //se va in aavanti + if(vx > 0){ + return vx - atrTerreno * time; + //se va indietro + }else if (vx < 0){ + return vx + atrTerreno * time; + //se e fermo + }else{ + return 0; + } + } + else{ + return vx; + } + } + + @Override + public void run(){ + boolean flag = true; + + while(flag){ + try{ + Thread.sleep(20); //pausa + newPos(); // calcolo la nuova posizione dopo la pausa + lp.hasMoved(this.position); + + //fermo il thread se il poiettile e fermo + if(intV == 0 && angV == 0){ + this.t.interrupt(); + } + }catch(InterruptedException e){ + flag = false; + } + } + //System.out.println("stopped"); + } +} \ No newline at end of file