A-Frame Trigger javascript function on collide with camera
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 |
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 |
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 Dec 3 '18 at 22:45
Jack Bashford
6,91031336
6,91031336
asked Nov 11 '18 at 4:36
TYCTYC
9618
9618
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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 '18 at 11:18
glad i could help!
– Piotr Adam Milewski
Nov 11 '18 at 11:31
add a comment |
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 '18 at 9:39
Look at the above answer
– Jack Bashford
Nov 11 '18 at 20:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 11:18
glad i could help!
– Piotr Adam Milewski
Nov 11 '18 at 11:31
add a comment |
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 '18 at 11:18
glad i could help!
– Piotr Adam Milewski
Nov 11 '18 at 11:31
add a comment |
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 '18 at 11:31
answered Nov 11 '18 at 10:41
Piotr Adam MilewskiPiotr Adam Milewski
5,38021226
5,38021226
nice,Ngo Kevins is what i am looking for
– TYC
Nov 11 '18 at 11:18
glad i could help!
– Piotr Adam Milewski
Nov 11 '18 at 11:31
add a comment |
nice,Ngo Kevins is what i am looking for
– TYC
Nov 11 '18 at 11:18
glad i could help!
– Piotr Adam Milewski
Nov 11 '18 at 11:31
nice,Ngo Kevins is what i am looking for
– TYC
Nov 11 '18 at 11:18
nice,Ngo Kevins is what i am looking for
– TYC
Nov 11 '18 at 11:18
glad i could help!
– Piotr Adam Milewski
Nov 11 '18 at 11:31
glad i could help!
– Piotr Adam Milewski
Nov 11 '18 at 11:31
add a comment |
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 '18 at 9:39
Look at the above answer
– Jack Bashford
Nov 11 '18 at 20:16
add a comment |
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 '18 at 9:39
Look at the above answer
– Jack Bashford
Nov 11 '18 at 20:16
add a comment |
Just do this:
score++;
$("#score").attr("value", score);
Just do this:
score++;
$("#score").attr("value", score);
answered Nov 11 '18 at 5:00
Jack BashfordJack Bashford
6,91031336
6,91031336
how do i detect the collision or touch with the object, so i can call the function afterward
– TYC
Nov 11 '18 at 9:39
Look at the above answer
– Jack Bashford
Nov 11 '18 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 '18 at 9:39
Look at the above answer
– Jack Bashford
Nov 11 '18 at 20:16
how do i detect the collision or touch with the object, so i can call the function afterward
– TYC
Nov 11 '18 at 9:39
how do i detect the collision or touch with the object, so i can call the function afterward
– TYC
Nov 11 '18 at 9:39
Look at the above answer
– Jack Bashford
Nov 11 '18 at 20:16
Look at the above answer
– Jack Bashford
Nov 11 '18 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.
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