A-Frame Trigger javascript function on collide with camera
up vote
3
down vote
favorite
I am trying to trigger my score function whenever the camera collide or touch an object like:
<a-entity id="rock" static-body obj-model="obj:models/rock_mesh.obj;mtl:images/rock_mesh.mtl" rotation="0 90 0" position="7.30242379045994 0.3 0">
</a-entity>
I equip my a score text on my camera:
<a-text id="score" value="0" position="-0.2 -0.5 -1" color="red" width="5" anchor="left"></a-text>
And trying to trigger a function like this:
let score = 0;
score = score + 1
$("#score").setAttribute('text','value','Score '+score)
This is just a draft up code, I am still new to javascript
How can i do this? Incrementing the score on the screen whenever my camera touches this "rock" object?
How can i detect the collision or touch with the object and my camera?
Thanks in advance.
javascript jquery html aframe
add a comment |
up vote
3
down vote
favorite
I am trying to trigger my score function whenever the camera collide or touch an object like:
<a-entity id="rock" static-body obj-model="obj:models/rock_mesh.obj;mtl:images/rock_mesh.mtl" rotation="0 90 0" position="7.30242379045994 0.3 0">
</a-entity>
I equip my a score text on my camera:
<a-text id="score" value="0" position="-0.2 -0.5 -1" color="red" width="5" anchor="left"></a-text>
And trying to trigger a function like this:
let score = 0;
score = score + 1
$("#score").setAttribute('text','value','Score '+score)
This is just a draft up code, I am still new to javascript
How can i do this? Incrementing the score on the screen whenever my camera touches this "rock" object?
How can i detect the collision or touch with the object and my camera?
Thanks in advance.
javascript jquery html aframe
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am trying to trigger my score function whenever the camera collide or touch an object like:
<a-entity id="rock" static-body obj-model="obj:models/rock_mesh.obj;mtl:images/rock_mesh.mtl" rotation="0 90 0" position="7.30242379045994 0.3 0">
</a-entity>
I equip my a score text on my camera:
<a-text id="score" value="0" position="-0.2 -0.5 -1" color="red" width="5" anchor="left"></a-text>
And trying to trigger a function like this:
let score = 0;
score = score + 1
$("#score").setAttribute('text','value','Score '+score)
This is just a draft up code, I am still new to javascript
How can i do this? Incrementing the score on the screen whenever my camera touches this "rock" object?
How can i detect the collision or touch with the object and my camera?
Thanks in advance.
javascript jquery html aframe
I am trying to trigger my score function whenever the camera collide or touch an object like:
<a-entity id="rock" static-body obj-model="obj:models/rock_mesh.obj;mtl:images/rock_mesh.mtl" rotation="0 90 0" position="7.30242379045994 0.3 0">
</a-entity>
I equip my a score text on my camera:
<a-text id="score" value="0" position="-0.2 -0.5 -1" color="red" width="5" anchor="left"></a-text>
And trying to trigger a function like this:
let score = 0;
score = score + 1
$("#score").setAttribute('text','value','Score '+score)
This is just a draft up code, I am still new to javascript
How can i do this? Incrementing the score on the screen whenever my camera touches this "rock" object?
How can i detect the collision or touch with the object and my camera?
Thanks in advance.
javascript jquery html aframe
javascript jquery html aframe
edited 6 hours ago
Jack Bashford
3,44631033
3,44631033
asked Nov 11 at 4:36
TYC
7618
7618
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
The simplest way to detect collisions would be detecting if the THREE bounding boxes are overlapping
You can use Ngo Kevins aabb-collider which emits hitstart
upon collision. Remember though, the camera does not have it's own geometry:
<a-camera foo geometry="primitive: box" aabb-collider="objects: a-box"></a-camera>
<a-box scale="2 2 2" class="box" color="blue" position="0 1.6 -5" ></a-box>
with foo being a simple event listener for hitstart
.
AFRAME.registerComponent("foo", {
init: function() {
this.el.addEventListener("hitstart", (e)=>{
// Collision ! increment the score
})
}
})
Fiddle here.
If possible, I wouldn't detect collisions with your model, but create some collision boxes.
It's also worth mentioning, if You'd want to use a physics engine in your project, Don McCurdys Physics System also enables collision detection. Instead of hitstart
, You'd need to listen for collision
.
nice,Ngo Kevins is what i am looking for
– TYC
Nov 11 at 11:18
glad i could help!
– Piotr Adam Milewski
Nov 11 at 11:31
add a comment |
up vote
1
down vote
Just do this:
score++;
$("#score").attr("value", score);
how do i detect the collision or touch with the object, so i can call the function afterward
– TYC
Nov 11 at 9:39
Look at the above answer
– Jack Bashford
Nov 11 at 20:16
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
The simplest way to detect collisions would be detecting if the THREE bounding boxes are overlapping
You can use Ngo Kevins aabb-collider which emits hitstart
upon collision. Remember though, the camera does not have it's own geometry:
<a-camera foo geometry="primitive: box" aabb-collider="objects: a-box"></a-camera>
<a-box scale="2 2 2" class="box" color="blue" position="0 1.6 -5" ></a-box>
with foo being a simple event listener for hitstart
.
AFRAME.registerComponent("foo", {
init: function() {
this.el.addEventListener("hitstart", (e)=>{
// Collision ! increment the score
})
}
})
Fiddle here.
If possible, I wouldn't detect collisions with your model, but create some collision boxes.
It's also worth mentioning, if You'd want to use a physics engine in your project, Don McCurdys Physics System also enables collision detection. Instead of hitstart
, You'd need to listen for collision
.
nice,Ngo Kevins is what i am looking for
– TYC
Nov 11 at 11:18
glad i could help!
– Piotr Adam Milewski
Nov 11 at 11:31
add a comment |
up vote
3
down vote
accepted
The simplest way to detect collisions would be detecting if the THREE bounding boxes are overlapping
You can use Ngo Kevins aabb-collider which emits hitstart
upon collision. Remember though, the camera does not have it's own geometry:
<a-camera foo geometry="primitive: box" aabb-collider="objects: a-box"></a-camera>
<a-box scale="2 2 2" class="box" color="blue" position="0 1.6 -5" ></a-box>
with foo being a simple event listener for hitstart
.
AFRAME.registerComponent("foo", {
init: function() {
this.el.addEventListener("hitstart", (e)=>{
// Collision ! increment the score
})
}
})
Fiddle here.
If possible, I wouldn't detect collisions with your model, but create some collision boxes.
It's also worth mentioning, if You'd want to use a physics engine in your project, Don McCurdys Physics System also enables collision detection. Instead of hitstart
, You'd need to listen for collision
.
nice,Ngo Kevins is what i am looking for
– TYC
Nov 11 at 11:18
glad i could help!
– Piotr Adam Milewski
Nov 11 at 11:31
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
The simplest way to detect collisions would be detecting if the THREE bounding boxes are overlapping
You can use Ngo Kevins aabb-collider which emits hitstart
upon collision. Remember though, the camera does not have it's own geometry:
<a-camera foo geometry="primitive: box" aabb-collider="objects: a-box"></a-camera>
<a-box scale="2 2 2" class="box" color="blue" position="0 1.6 -5" ></a-box>
with foo being a simple event listener for hitstart
.
AFRAME.registerComponent("foo", {
init: function() {
this.el.addEventListener("hitstart", (e)=>{
// Collision ! increment the score
})
}
})
Fiddle here.
If possible, I wouldn't detect collisions with your model, but create some collision boxes.
It's also worth mentioning, if You'd want to use a physics engine in your project, Don McCurdys Physics System also enables collision detection. Instead of hitstart
, You'd need to listen for collision
.
The simplest way to detect collisions would be detecting if the THREE bounding boxes are overlapping
You can use Ngo Kevins aabb-collider which emits hitstart
upon collision. Remember though, the camera does not have it's own geometry:
<a-camera foo geometry="primitive: box" aabb-collider="objects: a-box"></a-camera>
<a-box scale="2 2 2" class="box" color="blue" position="0 1.6 -5" ></a-box>
with foo being a simple event listener for hitstart
.
AFRAME.registerComponent("foo", {
init: function() {
this.el.addEventListener("hitstart", (e)=>{
// Collision ! increment the score
})
}
})
Fiddle here.
If possible, I wouldn't detect collisions with your model, but create some collision boxes.
It's also worth mentioning, if You'd want to use a physics engine in your project, Don McCurdys Physics System also enables collision detection. Instead of hitstart
, You'd need to listen for collision
.
edited Nov 11 at 11:31
answered Nov 11 at 10:41
Piotr Adam Milewski
5,07221025
5,07221025
nice,Ngo Kevins is what i am looking for
– TYC
Nov 11 at 11:18
glad i could help!
– Piotr Adam Milewski
Nov 11 at 11:31
add a comment |
nice,Ngo Kevins is what i am looking for
– TYC
Nov 11 at 11:18
glad i could help!
– Piotr Adam Milewski
Nov 11 at 11:31
nice,Ngo Kevins is what i am looking for
– TYC
Nov 11 at 11:18
nice,Ngo Kevins is what i am looking for
– TYC
Nov 11 at 11:18
glad i could help!
– Piotr Adam Milewski
Nov 11 at 11:31
glad i could help!
– Piotr Adam Milewski
Nov 11 at 11:31
add a comment |
up vote
1
down vote
Just do this:
score++;
$("#score").attr("value", score);
how do i detect the collision or touch with the object, so i can call the function afterward
– TYC
Nov 11 at 9:39
Look at the above answer
– Jack Bashford
Nov 11 at 20:16
add a comment |
up vote
1
down vote
Just do this:
score++;
$("#score").attr("value", score);
how do i detect the collision or touch with the object, so i can call the function afterward
– TYC
Nov 11 at 9:39
Look at the above answer
– Jack Bashford
Nov 11 at 20:16
add a comment |
up vote
1
down vote
up vote
1
down vote
Just do this:
score++;
$("#score").attr("value", score);
Just do this:
score++;
$("#score").attr("value", score);
answered Nov 11 at 5:00
Jack Bashford
3,44631033
3,44631033
how do i detect the collision or touch with the object, so i can call the function afterward
– TYC
Nov 11 at 9:39
Look at the above answer
– Jack Bashford
Nov 11 at 20:16
add a comment |
how do i detect the collision or touch with the object, so i can call the function afterward
– TYC
Nov 11 at 9:39
Look at the above answer
– Jack Bashford
Nov 11 at 20:16
how do i detect the collision or touch with the object, so i can call the function afterward
– TYC
Nov 11 at 9:39
how do i detect the collision or touch with the object, so i can call the function afterward
– TYC
Nov 11 at 9:39
Look at the above answer
– Jack Bashford
Nov 11 at 20:16
Look at the above answer
– Jack Bashford
Nov 11 at 20:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245876%2fa-frame-trigger-javascript-function-on-collide-with-camera%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown