-
-
Notifications
You must be signed in to change notification settings - Fork 696
added new sample code to demonstrate mass #1472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Issykelly
wants to merge
2
commits into
AlmasB:dev
Choose a base branch
from
Issykelly:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| /* | ||
| * FXGL - JavaFX Game Library. The MIT License (MIT). | ||
| * Copyright (c) AlmasB (almaslvl@gmail.com). | ||
| * See LICENSE for details. | ||
| */ | ||
|
|
||
|
|
||
| package basics; | ||
|
|
||
|
|
||
| import com.almasb.fxgl.app.GameApplication; | ||
| import com.almasb.fxgl.app.GameSettings; | ||
| import com.almasb.fxgl.dsl.FXGL; | ||
| import com.almasb.fxgl.entity.Entity; | ||
| import com.almasb.fxgl.input.UserAction; | ||
| import com.almasb.fxgl.physics.BoundingShape; | ||
| import com.almasb.fxgl.physics.HitBox; | ||
| import com.almasb.fxgl.physics.PhysicsComponent; | ||
| import com.almasb.fxgl.physics.box2d.collision.shapes.MassData; | ||
| import com.almasb.fxgl.physics.box2d.dynamics.BodyType; | ||
| import com.almasb.fxgl.physics.box2d.dynamics.FixtureDef; | ||
| import javafx.geometry.Point2D; | ||
| import javafx.scene.input.KeyCode; | ||
| import javafx.scene.input.MouseButton; | ||
| import javafx.scene.paint.Color; | ||
| import javafx.scene.shape.Circle; | ||
| import javafx.scene.shape.Rectangle; | ||
| import javafx.scene.text.Font; | ||
| import javafx.scene.text.Text; | ||
|
|
||
|
|
||
| import static com.almasb.fxgl.dsl.FXGL.*; | ||
|
|
||
|
|
||
| /** | ||
| * Shows how to change mass within a body directly | ||
| * and the affect this has on general physics. | ||
| * | ||
| * demonstrated using a seesaw, you can see | ||
| * how objects of different masses interact with | ||
| * their environment differently. | ||
| * | ||
| * @author Issy Kelly @IssyKelly | ||
| */ | ||
| public class MassSample extends GameApplication { | ||
|
|
||
|
|
||
| int mass = 1; | ||
| Text uiText; | ||
|
|
||
|
|
||
| @Override | ||
| protected void initSettings(GameSettings settings) { | ||
| settings.setWidth(1280); | ||
| settings.setHeight(720); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| protected void initInput() { | ||
| // click to add a new ball of the shown (on screen) mass | ||
| getInput().addAction(new UserAction("LMB") { | ||
| private double x; | ||
| private double y; | ||
|
|
||
|
|
||
| @Override | ||
| protected void onActionBegin() { | ||
| x = getInput().getMouseXWorld(); | ||
| y = getInput().getMouseYWorld(); | ||
| newBall(mass, x, y); | ||
| } | ||
| }, MouseButton.PRIMARY); | ||
|
|
||
|
|
||
| // press E to increase the mass of new balls by 1 | ||
| onKeyDown(KeyCode.E, () -> { | ||
| mass += 1; | ||
| uiText.setText(mass + " mass"); | ||
| }); | ||
|
|
||
|
|
||
| // press P to increase the mass of new balls by 10 | ||
| onKeyDown(KeyCode.P, () -> { | ||
| mass += 10; | ||
| uiText.setText(mass + " mass"); | ||
| }); | ||
|
|
||
|
|
||
| // press Q to decrease the mass of new balls by 1 | ||
| onKeyDown(KeyCode.Q, () -> { | ||
| mass -= 1; | ||
| uiText.setText(mass + " mass"); | ||
| }); | ||
|
|
||
|
|
||
| // press O to decrease the mass of new balls by 10 | ||
| onKeyDown(KeyCode.O, () -> { | ||
| mass -= 10; | ||
| uiText.setText(mass + " mass"); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| protected void initGame() { | ||
| getGameScene().setBackgroundColor(Color.LIGHTGRAY); | ||
|
|
||
|
|
||
| uiText = new Text(mass + " mass"); | ||
| Font uifont = new Font(40); | ||
| uiText.setFont(uifont); | ||
| FXGL.addUINode(uiText, 600, 100); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| //floor | ||
| Entity floor = entityBuilder() | ||
| .at(0, 700) | ||
| .viewWithBBox(new Rectangle(1450, 10, Color.BLACK)) | ||
| .with(new PhysicsComponent()) | ||
| .buildAndAttach(); | ||
|
|
||
|
|
||
| // balls for right hand seesaw | ||
| newBall(100, 1050, 300); | ||
| newBall(10, 700,300); | ||
|
|
||
|
|
||
| // balls for left hand side seesaw | ||
| newBall(20, 170, 300); | ||
| newBall(10, 300,300); | ||
|
|
||
|
|
||
| newSeesaw(250,600, 1); | ||
| newSeesaw(900,550, 1.4); | ||
| } | ||
|
|
||
|
|
||
| public void newSeesaw(int x, int y, double size){ | ||
| int circum = (int) (10 * size); | ||
| int rectx = (int) (400 * size); | ||
|
|
||
|
|
||
| Entity circle = entityBuilder() | ||
| .at(x, y) | ||
| .viewWithBBox(new Circle(circum, Color.BLACK)) | ||
| .with(new PhysicsComponent()) | ||
| .buildAndAttach(); | ||
|
|
||
|
|
||
| circle.getComponent(PhysicsComponent.class) | ||
| .setBodyType(BodyType.STATIC); | ||
|
|
||
|
|
||
| PhysicsComponent plankPhysics = new PhysicsComponent(); | ||
| plankPhysics.setBodyType(BodyType.DYNAMIC); | ||
| FixtureDef fd = new FixtureDef(); | ||
| fd.setDensity(5.0f); | ||
| plankPhysics.setFixtureDef(fd); | ||
|
|
||
|
|
||
| Entity plank = entityBuilder() | ||
| .at(x, y) | ||
| .viewWithBBox(new Rectangle(rectx, 20, Color.BROWN)) | ||
| .with(plankPhysics) | ||
| .buildAndAttach(); | ||
|
|
||
|
|
||
| Point2D anchor = new Point2D((circum/2), (circum/2)); | ||
| Point2D anchor2 = new Point2D((rectx/2), (20/2)); | ||
|
|
||
|
|
||
| getPhysicsWorld().addRevoluteJoint( | ||
| circle, | ||
| plank, | ||
| anchor, | ||
| anchor2 | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| public void newBall(int mass, double x, double y){ | ||
| var physics = new PhysicsComponent(); | ||
| physics.setFixtureDef(new FixtureDef().density(25.5f).restitution(0.5f)); | ||
| physics.setBodyType(BodyType.DYNAMIC); | ||
|
|
||
|
|
||
| MassData massValue = new MassData(); | ||
| // the code to directly change the mass of a body | ||
| massValue.mass = mass; | ||
| physics.setOnPhysicsInitialized(() -> physics.getBody().setMassData(massValue)); | ||
|
|
||
|
|
||
| entityBuilder() | ||
| .at(x, y) | ||
| .bbox(new HitBox(BoundingShape.circle(450 / 15.0 / 2.0))) | ||
| .view(texture("ball.png", 450 / 15.0, 449 / 15.0).multiplyColor(Color.BLUE)) | ||
| .with(physics) | ||
| .buildAndAttach(); | ||
| } | ||
|
|
||
|
|
||
| static void main(String[] args) { | ||
| launch(args); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems your IDE is adding an extra new line for each empty line, please can you check. Standard practice is 1 empty line, like at Line 38.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the response! I've gone ahead and fixed this. I'll be mindful to ensure I don't make the same formatting error again :)