• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Asteroid Game

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I hope everyone is doing well.

I am making a game called asteriodGameClone in JavaScript using the P5.js library.
I have 11 steps to get my game running correctly, as expected.
I have successfully completed 3 of my steps.  

I need help with the fourth step, which is mentioned below.

Step 4: In sketch.js, complete the inside () function that takes the location of two circles and their diameters and returns true if they overlap, and false otherwise. You could check it works by creating a dummy circle around the mouse and checking if inside () returns true.

The problem with my code is it only returns false when the circles do not overlap, but when the circle overlaps, nothing is returned to my console.

My code and the image of my console are below.



Thank you
image-(9).png
image of my console
image of my console
 
Bartender
Posts: 5567
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi James,

are the arguments when you invoke 'isInside' in line with the parameterlist of the isInside-method?

And a P5vector has a 'dist' method, see the API.
 
Piet Souris
Bartender
Posts: 5567
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. I got confused. dummySize is a vector, and the radius in the isInside function is dummySize.x, so that should be correct.

What are the values of eartloc and eartsize.x? And do you draw anything in your draw function, to also visually inspect the outcome in your log?
 
Piet Souris
Bartender
Posts: 5567
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well,

had a final look at your post, and I do not know why the console only prints when there is no overlap. Maybe there is a mismatch between the distance and what you draw? I cannot check that, so I made a simple version myself. I use NetBeans as IDE: The problem I encountered was that I thought that the circle-command took a radius as third parameter, but it turned out that it was the diameter, and so I got errors too. Here goes:

index.html:

newjavascript.js

Asteroid.js
 
James McDaniels
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Sorry, let me post all my code.
Check my code below.

Asteriods Games Clone

Thank you
 
James McDaniels
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 4: In sketch.js, complete the inside () function that takes the location of two circles and their diameters and returns true if they overlap, and false otherwise. You could check it works by creating a dummy circle around the mouse and checking if inside () returns true.

Working on this step above.
JavaScript(P5.js library)
 
Piet Souris
Bartender
Posts: 5567
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James McDaniels wrote:Hello,

Sorry, let me post all my code.
Check my code below.

Asteriods Games Clone

Thank you


You're welcome!

Just looking at your code at CodePen, logged in via my Google-account. Is there a way to run the code from there, or must I copy it to my IDE?
 
James McDaniels
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't get it to run in CodePen for some reason.
So it's better to copy all the code into your IDE.
 
Piet Souris
Bartender
Posts: 5567
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi James,

took a little effort, but I got your game up and running. And the good news is: there is nothing wrong with your isInside-function!  While running your code, I noticed what I noticed myself last night: if you have the console on, and you see only a part of the output (at least on my limited-size monitor) then something strange happens to the coordinate system, such that you never get an overlap. As said, I got a similar result last night, and therefore, look at the code that I gave, I put a "div id='overlaps'" in the body, and I wrote the result of isInside to that div. Then you do not need the console.

And when you run the code without the console, you see that it works as it should!

To recap:

in the <body> I have

and in the file javascript.js,

and in the initieer-function

in the draw() function at the end:


Now, if I am right, the checkCollisions must do two things:

1) check whether the spaceship collides with an asteroid
2) check whether a bullet collides with an asteroid (and perhaps the spaceship itself?)

To make all that a little more readable, I would suggest to have also an Asteroid class and a Bullet-class, to ease the AsteroidSystem and the BulletSystem.

Let us know how you fare, And the look of the game is very nice indeed!


 
James McDaniels
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piet,

Step 4: In sketch.js complete the isInside() function that takes the location of two circles and their diameters and returns true if they overlap, false otherwise. You could check it works, by creating a dummy circle around the mouse and checking if isInside() returns true.


I need Step 4 to work in the sketch, js file only.
The three primary functions the code should be in are function setup(), function draw(), & function isInside(locA, sizeA, locB, sizeB) .






 
Piet Souris
Bartender
Posts: 5567
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi James,

there is this slight problem that I work in an IDE that I also use for java (NetBeans). I do not work with the p5-editor, so usually I set up my webpage in my index.html.

As I said, I do not trust the console for this matter, so I have a div on the webpage that I use to display the overlap.

You can do that from the p5-editor (sketch.js) as well. For instance, this would be my sketch

You see the overlap-div right of the canvas, nearly at the top, you can see that the isInside works.
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic