Hit detection implementation for Processing JS - processing.js

I am having some trouble with programming hit detection in Processing.JS. I have tried to make a function that checks if something is touching an object and returns true and otherwise returns false. This is that here.
`Box.prototype.checkTouching = function(v){
if(v.position.x > this.position.x - this.width/2 && v.position <
this.position.x + this.width/2 && v.position.y > this.positon.y -
this.height/2 && v.position.y < this.position.y + this.height/2){
return true;
}else{
return false;
}
};`
I am implementing it by creating a new variable "b" in my draw function that holds the value the function returned then using an if statement to check if the value "b" is holding is true. Like so
var b = box3.checkTouching(mos);
if(b === true){
println("It works");
}
What should happen when the two objects touch is that a message saying "it works" gets printed in to the console. Unfortunately even when the object the function is running on is touching the object that is running it nothing happens. I have already checked to see if the logic works and it is valid so I know it has to be my implementation I just can not seem to find out what is wrong with my implementation. Can anyone tell what I am doing wrong? Full program here

You need to check whether the rectangles overlap. You'd do this by checking each side, like this:
if(rectOneRight > rectTwoLeft && rectOneLeft < rectTwoRight && rectOneBottom > rectTwoTop && rectOneTop < rectTwoBottom){
//collision
}
Shameless self-promotion: I've written a tutorial on collision detection in Processing (including rectangle-rectangle collision) available here.

To build on what Kevin posted, say I want to hover my mouse over a rectangle. processing has the built in variables mouseX, mouseY that return the coordinates of the mouse.
so I would check if the mouse X position was greater then the rect X pos, and less than the rect X pos + the rect width. the, do the same with the mouseY, rect Y and rect height
if (mouseX > rectXpos &&
mouseX < rectXpos + rectWidth &&
mouseY > rectYpos &&
mouseY < rectYpos + rectHeight) {
// the button is being hovered over
}

Related

Problem with simple rectangle collison in OpenGL

I try to implement collision detection and collision effects. This is my code for collision detection:
fun collisionDetection(objcect : Renderable?, object2 : Renderable?) : Boolean {
var collisionX : Boolean = objcect?.getPosition()?.x?.plus(quadSizeX)!! >= object2?.getPosition()?.x!! && object2?.getPosition()?.x?.plus(quadSizeX)!! >= objcect?.getPosition()?.x!!
var collisionY : Boolean = objcect?.getPosition()?.y?.plus(quadSizeY)!! >= object2?.getPosition()?.y!! && object2?.getPosition()?.y?.plus(quadSizeY)!! >= objcect?.getPosition()?.y!!
var collisionZ : Boolean = objcect?.getPosition()?.z?.plus(quadSizeZ)!! >= object2?.getPosition()?.z!! && object2?.getPosition()?.z?.plus(quadSizeZ)!! >= objcect?.getPosition()?.z!!
return collisionX && collisionY && collisionZ }
This seems to work, when used with overlapping objects set to overlapping values by transformations. This way
wall.translateLocal(Vector3f(0.0f, 1.0f, -8.0f))
and
wall2.translateLocal(Vector3f(0.0f, 2.0f, -8.0f))
collisionDetection is true.
When moving wall2 on the z-axis with a different starting point and speed, but x and y position are the same, the effect of the collision should be that wall2 stops moving, when z position of both objects is equal. I tried it this way in the main loop (and many other ways...), but it is still not working:
fun render(dt: Float, t: Float) {
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
staticShader.use()
camera.bind(staticShader)
wall.render(staticShader)
wall2.render(staticShader)
if (collisionDetection(wall, wall2) == true) {
wall2.getPosition()
}
else {wall2.translateLocal(Vector3f(0.0f, 0.0f, -t.toFloat()* 0.001f))}
}
I have no idea now, what is wrong and how to fix it. It seems like the collisionDetection function doesn't get information about the changing positions on the z-axis of the wall2. Or maybe I need a different approach.
Everything works fine now. Forgot to calculate QuadSize for the second object. This results in two objects on the same z.coordinate.Nothing has been wrong with the function for detecting collisions.

Hit Detection Implementation in Processing.JS

I am having some trouble with programming hit detection in Processing.JS. I have tried to make a function that checks if something is touching an object and returns true and otherwise returns false. This is that here.
Snake.prototype.checkTouching = function(v){
if(v.position.x > this.position.x - this.width/2 && v.position < this.position.x + this.width/2 && v.position.y > this.positon.y - this.height/2 && v.position.y < this.position.y + this.height/2){
return true;
}else{
return false;
}
};
I am implementing it by creating a new variable "b" in my draw function that holds the value the function returned then using an if statement to check if the value "b" is holding is true. Like so
var b = snakey.checkTouching(mos);
var restarts = 0;
if(b === true){
restarts += 1;
Program.restart();
}
unfortunately even when the object the function is running on is touching the object that is running it nothing happens. I have already checked to see if the logic works and it is valid so I know it has to be my implementation I just can not seem to find out what is wrong with my implementation. Can anyone tell what I am doing wrong? Full program here.Full Program

how to scale up and down a shape based on mouseX and mouseY

createjs noob here,
I am trying to make a class, that when you pass in a shape, it lets you scale an object up or down based on mouse position.
beeSyrup.Scaler = function(obj){
console.log("hi from scaler");
if (not(obj) || !obj.on) return;
obj.addEventListener("pressmove", function(e){
obj.cursor = "alias";
mouseX = e.stageX;
mouseY = e.stageY;
var lastMouseX = mouseX;
var lastMouseY = mouseY;
if (lastMouseX < mouseX && lastMouseY < mouseY){
obj.scaleX += .1;
obj.scaleY += .1;
}else{
obj.scaleX -= .1;
obj.scaleY -= .1;
}
stage.update();
});
}
Currently this will scale it down once, but once it scales down you can only scale up. How would the correct if statement logic work?
In your code, mouseX and lastMouseX are always equal, because you are setting lastMouseX = mouseX before comparing them (true of the Y values as well). As such, the else condition is always satisfied, so it will always scale down.
There are many other things you will likely want to change to make this work in an user-friendly manner, but this is your immediate problem.
Here's a very rough JSFiddle to get you started down the right path: http://jsfiddle.net/9zu9jpvx/

Actionscript 3: How to make movieclip variables work with variables on stage (Healthbar Help)

I am totally new at this whole programming thing, and I really need help. So basically, I'm trying to make a healthbar that will increase or decrease depending on what button is clicked. I made a healthbar movieclip with 101 frames (I included zero) and put this in the actionscript layer of the movieclip:
var health:Number = 0;
if(health == 0)
{
gotoAndStop("1")
}
if(health == 1)
{
gotoAndStop("2")
}
if(health == 2)
{
gotoAndStop("3")
}
and on and on like so. Basically, on the stage itself, I have a button called fortyfiveup_btn that is commanded to do this:
var health:Number = 0;
fortyfiveup_btn.addEventListener(MouseEvent.CLICK, fortyfiveupClick);
function fortyfiveupClick(event:MouseEvent):void{
health = health+45
}
I quickly realized that both health variables, the one for the button and the one for the healthbar will not interact. How can I make it so if the button is clicked, the health goes to the relevant frame or percentage?
Thanks for any answers, and I appreciate all the help I can get :)
If the answer == yes to my comment you should do this:
You need to give the movieclip an instancename (perhaps lifebar) and from stage you can access the health inside the "lifebar" with lifebar.health.
So you need this inside your stage:
//You can delete the var health:Number = 0;
fortyfiveup_btn.addEventListener(MouseEvent.CLICK, fortyfiveupClick);
function fortyfiveupClick(event:MouseEvent):void{
//You can write this, too: lifebar.health += 45;
lifebar.health = lifebar.health+45;
}
You can even optimize your lifebar script, don't use 101 times the if(health == x) you can use this, too:
gotoAndStop(health + 1);
(I think this is inside an ENTER_FRAME event?)
EDIT:
Some error countermeasures:
//Don't let health decrease below 0
if(health < 0) health = 0;
//And don't above 100
else if(health > 100) health = 100;
gotoAndStop(health + 1);
Use int instead of Number when you don't use decimal numbers and uint when you don't use negative integers (this bugs when the number can drop under 0, so for your health we take int):
health:int = 0;

Checking if tile exists without Assertion Failure?

[layer tileGIDAt:position]
If I give it a position outside the map, I get an Assertion Failure, which is just normal.
I need a way to know when a tile exists. Before running the above code so I don't get a an Assertion Failure and also to do other things in case the tile does not exist. But how can I? Is there not a method in the CCTMXLayer class to check that?
To test if a tile exists, test if the tile GID is 0. Before that you can test if the position is on the tilemap as such:
if (position.x < layer.size.width &&
position.y < layer.size.height &&
position.x >= 0 && position.y >= 0)
{
// position is within tilemap layer …
if ([layer tileGIDAt:position] != 0)
{
// tile at position exists …
}
}