JavaFX 1.0 is out and is absolutely amazing. You guys did really a great work on it.
As I really need a working SDK on Linux to continue to study and I don’t have any Windows/Mac near me, I’m using the Weiqi Gao’s workaround. I tried to simplify a little bit more the process for those who need JavaFX SDK working on Linux right now.
Now you can call javafx, javafxc, javafxdoc and javafxpackager from your terminal. Don’t forget that you need Java 1.6 or greater installed.
Here’s a video showing the SDK working, I’m compiling and running two sample applications. Remeber that as a temporary unofficial port for Linux, there’s not native video support nor hardware acceleration.
// returning 0 means collision
int collision(int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy){
return ((ax > dx)||(bx < cx)||(ay > dy)||(by < cy));
}
I’ll show here a little demo about how implement simple rectangular collisions on JavaFX.
First I created a movable rectangle using the same idea of draggable nodes I already had posted before.
You can use rectangular collisions to create bounding boxes to handle collisions in more complex shapes or sprites. Is a common approach in 2d games to avoid more expensive calculations.
There are space for optimizations.
In this case I’m using only two objects. Some problems raises when I have N objects to handle.
This way we can pass just two bounding boxes to hitnode and easily check collision of a node against a list of bounding boxes nodes.
Using the same approach I also wrote this function to test if a Node is inside another Node:
function inside (ax, ay, bx, by, cx, cy, dx, dy):Boolean{return((ax > cx) and (bx < dx) and (ay > cy) and (by < dy));}
function insidenode(a:Node,b:Node):Boolean{return(inside(
a.getBoundsX(), a.getBoundsY(),
a.getBoundsX()+ a.getWidth(), a.getBoundsY()+ a.getHeight(),
b.getBoundsX(), b.getBoundsY(),
b.getBoundsX()+ b.getWidth(), b.getBoundsY()+ b.getHeight()));}
function inside (ax, ay, bx, by, cx, cy, dx, dy):Boolean{
return ((ax > cx) and (bx < dx) and (ay > cy) and (by < dy));
}
function insidenode(a:Node,b:Node):Boolean{
return (inside(
a.getBoundsX(), a.getBoundsY(),
a.getBoundsX() + a.getWidth(), a.getBoundsY() + a.getHeight(),
b.getBoundsX(), b.getBoundsY(),
b.getBoundsX() + b.getWidth(), b.getBoundsY() + b.getHeight()
));
}
Soon I’ll post game examples showing how to use this method and others collission detection methods.
Two simple JavaFX code handling onMouseDragged event.
import javafx.ui.*;
import javafx.ui.canvas.*;
Canvas {
content: Circle {
var x = 50
var y = 50
transform: bind translate(x, y)
radius: 30
fill: red
onMouseDragged: operation(e) {
x += e.localDragTranslation.x;
y += e.localDragTranslation.y;
}
}
}
import javafx.ui.*;
import javafx.ui.canvas.*;
Canvas {
content: Circle {
var x = 50
var y = 50
var radius = 30
transform: bind translate(x, y)
radius: bind radius
fill: red
onMouseDragged: operation(e) {
if (e.button == 1){
x += e.localDragTranslation.x;
y += e.localDragTranslation.y;
}
if (e.button == 3) {
radius += e.localDragTranslation.x;
}
}
}
}
import javafx.ui.*;
import javafx.ui.canvas.*;
Canvas {
content: [
Rect {x: 50, y: 50, width: 50, height: 50, fill: orange },
Circle {
var x = 50
var y = 50
var radius = 30
var color = red:Color
transform: bind translate(x, y)
radius: bind radius
fill: bind color
onMouseDragged: operation(e) {
if (e.button == 1){
x += e.localDragTranslation.x;
y += e.localDragTranslation.y;
}
if (e.button == 3) {
radius += e.localDragTranslation.x;
}
}
onMousePressed: operation(e){
color = Color {blue: 0.0, green: 0.0, red: 1.0, opacity: 0.5};
}
onMouseReleased: operation(e){
color = red:Color;
}
}]
}