Author Topic: how to check balls out of view  (Read 2723 times)

0 Members and 1 Guest are viewing this topic.

RAIN CODE

  • Guest
how to check balls out of view
« on: June 28, 2012, 02:07:29 AM »
hi again, guys

Do you know how to determine if a block moving around the screen is out of the current view - let say the block move from the screen centre to 9 o'clock (left hand side) then disappear out of view. Is there any shortest way to determine if the block is out of view ?? with the use of lisp or setvar or limits ??

I am trying to add a boss level to the game. The boss will shoot out balls at pacman. But how to determine if the balls are out of the current view ? if the balls are out of view then I will discard those balls to speed up the game.

I could write to check if balls is within screen view by using -
      if balls insertion pt x is > bottom most left corner x AND insertion pt x is < top most right corner x ... and do the same for y axis
but it is too messy and long and it will very likely slow down the game since about 10 -15 small balls flying around the screen.

Thanks


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: how to check balls out of view
« Reply #1 on: June 28, 2012, 03:09:03 AM »
I'd usE VSMAX and VSMIN.

The X and Y of each will give you the edges of the viewport.
Then the INBOUNDS is just an OR check on the edges ...
if
  or
    ball X < left
    ball X > right
    ball Y < top
    ball Y < btm
  then the ball is out of bounds.

The OR will return True at the first test that passes, so it should be reasonably fast.

You may need to refresh the variables holding the edge values if you are allowing the viewport to  resize.


added:
I'd hold the edges value in the application scope and use a method that has the ballX and ballY as parameters.
.... that way you can use the same method to check all balls.

Regards
kdub
« Last Edit: June 28, 2012, 03:20:07 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: how to check balls out of view
« Reply #2 on: June 28, 2012, 03:19:48 AM »
This will return T if a point (pb) is out of visible screen.
Code: [Select]
(defun is_out (pb / cen scr h w)
  (setq cen (getvar 'viewctr)
        scr (getvar 'screensize)
        h (* 0.5 (getvar 'viewsize))
        w (/ (* (car scr) h) (cadr scr))
        )
  (vl-some '(lambda (a b c) (not (< a b c))) (mapcar '- cen (list w h)) pb (mapcar '+ cen (list w h)))
  )
Edit: switched w and h in (vl-some...
« Last Edit: June 29, 2012, 01:47:47 AM by Stefan »

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: how to check balls out of view
« Reply #3 on: June 28, 2012, 03:48:02 AM »
My version with grread function and it is considered just as an example ...  :-)

Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test (/ p1 p2 gr)
  2. ;;;;; Tharwat 28. June. 2012 ;;;;;
  3.   (if (and (setq p1 (getpoint "\n Specify point :"))
  4.            (setq p2 (getcorner p1 "\n Corner :"))
  5.       )
  6.     (while (eq (car (setq gr (grread t 15 1))) 5) (redraw)
  7.       (foreach n '((0.5 0.5 0.0) (-0.5 0.5 0.0)(-0.5 -0.5 0.0)(0.5 -0.5 0.0))
  8.         (grdraw
  9.           (cadr gr)
  10.           (mapcar '+ (mapcar (function (lambda (x) (* x (/ (getvar "VIEWSIZE") 25.))))
  11.               (trans n 1 0))
  12.             (cadr gr)
  13.           )
  14.           1
  15.           0
  16.         )
  17.       )
  18.  
  19.       (if
  20.         (and
  21.           (< (car p1) (car (cadr gr)))
  22.           (> (car p2) (car (cadr gr)))
  23.           (> (cadr p1) (cadr (cadr gr)))
  24.           (< (cadr p2) (cadr (cadr gr)))
  25.         )
  26.          (princ "\n inside ...... ")
  27.          (princ "\n outside ..... ")
  28.       )
  29.     )
  30.   )
  31.   (redraw)
  32.   (princ)
  33. )
  34.  

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: how to check balls out of view
« Reply #4 on: June 28, 2012, 07:14:45 AM »
A variation of Stefan's:

Code - Auto/Visual Lisp: [Select]
  1. (defun onscreen-p ( p / c h v )
  2.     (setq c (getvar 'viewctr)
  3.           h (/ (getvar 'viewsize) 2.0)
  4.           v (list (* h (apply '/ (getvar 'screensize))) h)
  5.     )
  6.     (apply 'and (mapcar '< (mapcar '- c v) p (mapcar '+ c v)))
  7. )

RAIN CODE

  • Guest
Re: how to check balls out of view
« Reply #5 on: June 29, 2012, 01:32:21 AM »
You guys are amazing. Surely you Lisp is much shorter than mine.
I am really impressed. I really don't understand much about mapcar and lambda.

Could anyone explains how your program works. I want to learn too. Please dont direct me to other web site for info.
I tried downloading lessons for mapcar and lambda but I could not grasp how it actually work.

Explaining here is better than telling me to read elsewhere.

Thanks.

« Last Edit: June 29, 2012, 01:46:36 AM by RAIN CODE »

BlackBox

  • King Gator
  • Posts: 3770
Re: how to check balls out of view
« Reply #6 on: June 29, 2012, 09:19:08 AM »
This Autodesk University course in particular helped me greatly when I first learned of functions such as Apply, Mapcar, and Lambda:

LISP: Advance Yourself Beyond a Casual Programmer

Direct link to course handout... Other resources available when logged in at the link above.

HTH
"How we think determines what we do, and what we do determines what we get."

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
« Last Edit: June 29, 2012, 10:12:27 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: how to check balls out of view
« Reply #8 on: June 29, 2012, 11:13:56 AM »
For what it's worth, I never let my balls out of my sight.  #outofcontext
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: how to check balls out of view
« Reply #9 on: June 29, 2012, 11:22:09 AM »

Thanks Matt, You just caused me to win $20.00.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

RAIN CODE

  • Guest
Re: how to check balls out of view
« Reply #10 on: July 05, 2012, 01:39:55 AM »

Thanks guy for the mapcar and lambda lessons or tutorial.

I need more sleep before I read that.
Have been programming into the wee hours when people are sleeping and dreaming.
So much so that my brain is telling me I need to stop programming for a while.  :blank:

something like a writer block, it refuse to think.  :blank:

Thanks again